
Recherche avancée
Autres articles (81)
-
Emballe médias : à quoi cela sert ?
4 février 2011, parCe plugin vise à gérer des sites de mise en ligne de documents de tous types.
Il crée des "médias", à savoir : un "média" est un article au sens SPIP créé automatiquement lors du téléversement d’un document qu’il soit audio, vidéo, image ou textuel ; un seul document ne peut être lié à un article dit "média" ; -
Gestion des droits de création et d’édition des objets
8 février 2011, parPar défaut, beaucoup de fonctionnalités sont limitées aux administrateurs mais restent configurables indépendamment pour modifier leur statut minimal d’utilisation notamment : la rédaction de contenus sur le site modifiables dans la gestion des templates de formulaires ; l’ajout de notes aux articles ; l’ajout de légendes et d’annotations sur les images ;
-
Supporting all media types
13 avril 2011, parUnlike most software and media-sharing platforms, MediaSPIP aims to manage as many different media types as possible. The following are just a few examples from an ever-expanding list of supported formats : images : png, gif, jpg, bmp and more audio : MP3, Ogg, Wav and more video : AVI, MP4, OGV, mpg, mov, wmv and more text, code and other data : OpenOffice, Microsoft Office (Word, PowerPoint, Excel), web (html, CSS), LaTeX, Google Earth and (...)
Sur d’autres sites (3406)
-
ffmpeg massive error spamming from FritzBox rtsp stream
19 février 2019, par itzaiiroIm trying to offer a rtsp live TV stream via rtmp to my family, since the Fritz Box (which is offering the stream) has only 4 tuners -> at most 4 streams can be watched simultaneously.
Im using ffmpeg to prepare the stream as dash stream and send it to my rtmp nginx. When i run ffmpeg im experiencing heavy image and audio artifacts in the final stream and error spams of doom in the console. I couldn’t find anything specific to my case on google. I read on the internet that AVM barely implemented the rtsp protocoll enough to get it to work with vlc mediaplayer.launch param :
ffmpeg -i "rtsp://192.168.178.1:554/?avm=1&freq=114&bw=8&msys=dvbc&mtype=256qam&sr=6900&specinv=1&pids=0,16,17,18,20,260,543,544,546,548,1621" -sn -vcodec libx264 -vprofile baseline -acodec aac -strict -2 -b:v 500k -minrate 500k -maxrate 500k -bufsize 1000k -g 60 -s 640x360 -f flv rtmp://192.168.178.15/dash/pro_sieben_low -sn -vcodec libx264 -vprofile baseline -acodec aac -strict -2 -b:v 1500k -minrate 1500k -maxrate 1500k -bufsize 3000k -g 60 -s 1280x720 -f flv rtmp://192.168.178.15/dash/pro_sieben_med -sn -vcodec libx264 -vprofile baseline -acodec aac -strict -2 -b:v 5000k -minrate 5000k -maxrate 5000k -bufsize 10000k -g 60 -s 1920x1080 -f flv rtmp://192.168.178.15/dash/pro_sieben_high
ffmpeg output (windows) :
stored in pastebin
https://pastebin.com/p4HAyBi5Is there anyway to get this under control ? The original stream is running good with vlc, but its unwatchable after its out of ffmpeg.
Edit :
I was running/testing this on my windows machine, but my target for this task is a ubuntu 16.04 so here ffmpeg on target with pthread support :ffmpeg -i "rtsp://192.168.178.1:554/?avm=1&freq=114&bw=8&msys=dvbc&mtype=256qam&sr=6900&specinv=1&pids=0,16,17,18,20,260,543,544,546,548,1621" -sn -vcodec libx264 -vprofile baseline -acodec aac -strict -2 -b:v 500k -minrate 500k -maxrate 500k -bufsize 1000k -g 60 -s 640x360 -f flv rtmp://192.168.178.15/dash/pro_sieben_low -sn -vcodec libx264 -vprofile baseline -acodec aac -strict -2 -b:v 1500k -minrate 1500k -maxrate 1500k -bufsize 3000k -g 60 -s 1280x720 -f flv rtmp://192.168.178.15/dash/pro_sieben_med -sn -vcodec libx264 -vprofile baseline -acodec aac -strict -2 -b:v 5000k -minrate 5000k -maxrate 5000k -bufsize 10000k -g 60 -s 1920x1080 -f flv rtmp://192.168.178.15/dash/pro_sieben_high
ffmpeg version 2.8.15-0ubuntu0.16.04.1 Copyright (c) 2000-2018 the FFmpeg developers
built with gcc 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.10) 20160609
configuration: --prefix=/usr --extra-version=0ubuntu0.16.04.1 --build-suffix=-ffmpeg --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --cc=cc --cxx=g++ --enable-gpl --enable-shared --disable-stripping --disable-decoder=libopenjpeg --disable-decoder=libschroedinger --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-librtmp --enable-libschroedinger --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxvid --enable-libzvbi --enable-openal --enable-opengl --enable-x11grab --enable-libdc1394 --enable-libiec61883 --enable-libzmq --enable-frei0r --enable-libx264 --enable-libopencv
libavutil 54. 31.100 / 54. 31.100
libavcodec 56. 60.100 / 56. 60.100
libavformat 56. 40.101 / 56. 40.101
libavdevice 56. 4.100 / 56. 4.100
libavfilter 5. 40.101 / 5. 40.101
libavresample 2. 1. 0 / 2. 1. 0
libswscale 3. 1.101 / 3. 1.101
libswresample 1. 2.101 / 1. 2.101
libpostproc 53. 3.100 / 53. 3.100
[mpeg2video @ 0x167cde0] Invalid frame dimensions 0x0.
Last message repeated 10 times
[rtsp @ 0x1627c20] Could not find codec parameters for stream 4 (Unknown: none ([5][0][0][0] / 0x0005)): unknown codec
Consider increasing the value for the 'analyzeduration' and 'probesize' options
Input #0, rtsp, from 'rtsp://192.168.178.1:554/?avm=1&freq=114&bw=8&msys=dvbc&mtype=256qam&sr=6900&specinv=1&pids=0,16,17,18,20,260,543,544,546,548,1621':
Metadata:
title : SatIPServer:1 0,0,4
Duration: N/A, start: 33786.528778, bitrate: N/A
Program 12101
Metadata:
service_name : ?▒RTL▒ Television
service_provider: ?Unitymedia
Program 12102
Metadata:
service_name : ?SAT.1
service_provider: ?Unitymedia
Program 12103
Metadata:
service_name : ?ProSieben
service_provider: ?Unitymedia
Stream #0:3: Video: mpeg2video (Main) ([2][0][0][0] / 0x0002), yuv420p(tv), 720x576 [SAR 64:45 DAR 16:9], max. 15000 kb/s, 25 fps, 25 tbr, 90k tbn, 50 tbc
Stream #0:2(deu): Audio: mp2 ([3][0][0][0] / 0x0003), 48000 Hz, stereo, s16p, 192 kb/s (clean effects)
Stream #0:0(deu): Audio: ac3 ([6][0][0][0] / 0x0006), 48000 Hz, 5.1(side), fltp, 384 kb/s (clean effects)
Stream #0:1(deu,deu): Subtitle: dvb_teletext ([6][0][0][0] / 0x0006), 492x250
Stream #0:4: Unknown: none ([5][0][0][0] / 0x0005)
Program 12104
Metadata:
service_name : ?VOX
service_provider: ?Unitymedia
Program 12105
Metadata:
service_name : ?RTL2
service_provider: ?Unitymedia
Program 12106
Metadata:
service_name : ?kabel eins
service_provider: ?Unitymedia
Program 12107
Metadata:
service_name : ?▒S▒uper▒ RTL▒
service_provider: ?Unitymedia
Program 12109
Metadata:
service_name : ?ntv
service_provider: ?Unitymedia
Program 12113
Metadata:
service_name : ?ProSieben MAXX
service_provider: ?Unitymedia
Program 20116
Metadata:
service_name : ?SIXX
service_provider: ?Unitymedia
[libx264 @ 0x182b140] using SAR=1/1
[libx264 @ 0x182b140] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 AVX2 LZCNT BMI2
[libx264 @ 0x182b140] profile Constrained Baseline, level 3.0
[libx264 @ 0x182b140] 264 - core 148 r2643 5c65704 - H.264/MPEG-4 AVC codec - Copyleft 2003-2015 - http://www.videolan.org/x264.html - options: cabac=0 ref=3 deblock=1:0:0 analyse=0x1:0x111 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=3 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=0 keyint=60 keyint_min=6 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=cbr mbtree=1 bitrate=500 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 vbv_maxrate=500 vbv_bufsize=1000 nal_hrd=none filler=0 ip_ratio=1.40 aq=1:1.00
[libx264 @ 0x16e03c0] using SAR=1/1
[libx264 @ 0x16e03c0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 AVX2 LZCNT BMI2
[libx264 @ 0x16e03c0] profile Constrained Baseline, level 3.1
[libx264 @ 0x16e03c0] 264 - core 148 r2643 5c65704 - H.264/MPEG-4 AVC codec - Copyleft 2003-2015 - http://www.videolan.org/x264.html - options: cabac=0 ref=3 deblock=1:0:0 analyse=0x1:0x111 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=3 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=0 keyint=60 keyint_min=6 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=cbr mbtree=1 bitrate=1500 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 vbv_maxrate=1500 vbv_bufsize=3000 nal_hrd=none filler=0 ip_ratio=1.40 aq=1:1.00
[libx264 @ 0x16cc880] using SAR=1/1
[libx264 @ 0x16cc880] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 AVX2 LZCNT BMI2
[libx264 @ 0x16cc880] profile Constrained Baseline, level 4.0
[libx264 @ 0x16cc880] 264 - core 148 r2643 5c65704 - H.264/MPEG-4 AVC codec - Copyleft 2003-2015 - http://www.videolan.org/x264.html - options: cabac=0 ref=3 deblock=1:0:0 analyse=0x1:0x111 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=3 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=0 keyint=60 keyint_min=6 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=cbr mbtree=1 bitrate=5000 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 vbv_maxrate=5000 vbv_bufsize=10000 nal_hrd=none filler=0 ip_ratio=1.40 aq=1:1.00
Output #0, flv, to 'rtmp://192.168.178.15/dash/pro_sieben_low':
Metadata:
title : SatIPServer:1 0,0,4
encoder : Lavf56.40.101
Stream #0:0: Video: h264 (libx264) ([7][0][0][0] / 0x0007), yuv420p, 640x360 [SAR 1:1 DAR 16:9], q=-1--1, 500 kb/s, 25 fps, 1k tbn, 25 tbc
Metadata:
encoder : Lavc56.60.100 libx264
Stream #0:1(deu): Audio: aac ([10][0][0][0] / 0x000A), 48000 Hz, 5.1(side), fltp, 128 kb/s (clean effects)
Metadata:
encoder : Lavc56.60.100 aac
Output #1, flv, to 'rtmp://192.168.178.15/dash/pro_sieben_med':
Metadata:
title : SatIPServer:1 0,0,4
encoder : Lavf56.40.101
Stream #1:0: Video: h264 (libx264) ([7][0][0][0] / 0x0007), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], q=-1--1, 1500 kb/s, 25 fps, 1k tbn, 25 tbc
Metadata:
encoder : Lavc56.60.100 libx264
Stream #1:1(deu): Audio: aac ([10][0][0][0] / 0x000A), 48000 Hz, 5.1(side), fltp, 128 kb/s (clean effects)
Metadata:
encoder : Lavc56.60.100 aac
Output #2, flv, to 'rtmp://192.168.178.15/dash/pro_sieben_high':
Metadata:
title : SatIPServer:1 0,0,4
encoder : Lavf56.40.101
Stream #2:0: Video: h264 (libx264) ([7][0][0][0] / 0x0007), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], q=-1--1, 5000 kb/s, 25 fps, 1k tbn, 25 tbc
Metadata:
encoder : Lavc56.60.100 libx264
Stream #2:1(deu): Audio: aac ([10][0][0][0] / 0x000A), 48000 Hz, 5.1(side), fltp, 128 kb/s (clean effects)
Metadata:
encoder : Lavc56.60.100 aac
Stream mapping:
Stream #0:3 -> #0:0 (mpeg2video (native) -> h264 (libx264))
Stream #0:0 -> #0:1 (ac3 (native) -> aac (native))
Stream #0:3 -> #1:0 (mpeg2video (native) -> h264 (libx264))
Stream #0:0 -> #1:1 (ac3 (native) -> aac (native))
Stream #0:3 -> #2:0 (mpeg2video (native) -> h264 (libx264))
Stream #0:0 -> #2:1 (ac3 (native) -> aac (native))
Press [q] to stop, [?] for help
RTP: missed 2137 packets.0 q=26.0 q=23.0 size= 238kB time=00:00:04.91 bitrate= 397.3kbits/s
[rtsp @ 0x1627c20] PES packet size mismatch
[mpeg2video @ 0x16d72c0] invalid mb type in I Frame at 0 16
[mpeg2video @ 0x16d72c0] invalid mb type in I Frame at 0 17
[mpeg2video @ 0x16d72c0] invalid mb type in I Frame at 0 18
[mpeg2video @ 0x16d72c0] invalid mb type in I Frame at 0 19
[mpeg2video @ 0x16d72c0] ac-tex damaged at 0 20
[mpeg2video @ 0x16d72c0] ac-tex damaged at 0 21
[mpeg2video @ 0x16d72c0] ac-tex damaged at 0 22
[mpeg2video @ 0x16d72c0] ac-tex damaged at 0 23
[mpeg2video @ 0x16d72c0] ac-tex damaged at 17 11
[mpeg2video @ 0x16d72c0] ac-tex damaged at 0 24
[mpeg2video @ 0x16d72c0] ac-tex damaged at 0 25
[mpeg2video @ 0x16d72c0] ac-tex damaged at 0 26
[mpeg2video @ 0x16d72c0] ac-tex damaged at 0 27
[mpeg2video @ 0x16d72c0] invalid mb type in I Frame at 0 31
[mpeg2video @ 0x16d72c0] invalid mb type in I Frame at 0 32
[mpeg2video @ 0x16d72c0] invalid mb type in I Frame at 0 33
[mpeg2video @ 0x16d72c0] invalid mb type in I Frame at 0 34
[mpeg2video @ 0x16d72c0] invalid mb type in I Frame at 0 35
[mpeg2video @ 0x16d72c0] Warning MVs not available
[mpeg2video @ 0x16d72c0] concealing 1125 DC, 1125 AC, 1125 MV errors in I frame
RTP: missed 11 packets
RTP: missed 37 packets
[ac3 @ 0x1676bc0] exponent out-of-range
[ac3 @ 0x1676bc0] error decoding the audio block
[ac3 @ 0x1676bc0] frame sync error
Error while decoding stream #0:0: Invalid data found when processing input
[ac3 @ 0x1676bc0] exponent out-of-range
[ac3 @ 0x1676bc0] error decoding the audio block
RTP: missed 21 packets
RTP: missed 32 packets
[mpeg2video @ 0x16d72c0] ac-tex damaged at 1 4
[mpeg2video @ 0x16d72c0] Warning MVs not available
[mpeg2video @ 0x16d72c0] concealing 1080 DC, 1080 AC, 1080 MV errors in B frame
[mpeg2video @ 0x16d72c0] 00 motion_type at 21 27
[mpeg2video @ 0x16d72c0] 00 motion_type at 2 26
[mpeg2video @ 0x16d72c0] 00 motion_type at 0 27
[mpeg2video @ 0x16d72c0] invalid cbp -1 at 2 1
[mpeg2video @ 0x16d72c0] 00 motion_type at 5 2
[mpeg2video @ 0x16d72c0] skip with previntra
[mpeg2video @ 0x16d72c0] 00 motion_type at 2 4
[mpeg2video @ 0x16d72c0] slice mismatch
[mpeg2video @ 0x16d72c0] 00 motion_type at 1 6
[mpeg2video @ 0x16d72c0] ac-tex damaged at 3 7
[mpeg2video @ 0x16d72c0] slice mismatch
[mpeg2video @ 0x16d72c0] 00 motion_type at 37 9
[mpeg2video @ 0x16d72c0] 00 motion_type at 1 10
[mpeg2video @ 0x16d72c0] mb incr damaged
[mpeg2video @ 0x16d72c0] 00 motion_type at 1 31
[mpeg2video @ 0x16d72c0] 00 motion_type at 5 32
[mpeg2video @ 0x16d72c0] 00 motion_type at 1 33
[mpeg2video @ 0x16d72c0] mb incr damaged
[mpeg2video @ 0x16d72c0] ac-tex damaged at 26 35
[mpeg2video @ 0x16d72c0] Warning MVs not available
[mpeg2video @ 0x16d72c0] concealing 945 DC, 945 AC, 945 MV errors in B frame
[rtsp @ 0x1627c20] PES packet size mismatchze= 294kB time=00:00:05.27 bitrate= 457.1kbits/s
[ac3 @ 0x1676bc0] frame sync error
Error while decoding stream #0:0: Invalid data found when processing input
[ac3 @ 0x1676bc0] exponent out-of-range
[ac3 @ 0x1676bc0] error decoding the audio block
[ac3 @ 0x1676bc0] frame sync error
Error while decoding stream #0:0: Invalid data found when processing input
RTP: missed 38 packets25.0 q=26.0 q=22.0 size= 320kB time=00:00:11.18 bitrate= 234.3kbits/s
RTP: missed 18 packets
RTP: missed 9 packets
RTP: missed 21 packets
RTP: missed 9 packets
[rtsp @ 0x1627c20] PES packet size mismatch
[ac3 @ 0x1676bc0] exponent out-of-range
[ac3 @ 0x1676bc0] error decoding the audio block
[ac3 @ 0x1676bc0] frame sync error
Error while decoding stream #0:0: Invalid data found when processing input
[ac3 @ 0x1676bc0] bandwidth code = 63 > 60
[ac3 @ 0x1676bc0] error decoding the audio block
RTP: missed 13 packets
[mpeg2video @ 0x16d72c0] mb incr damaged
[mpeg2video @ 0x16d72c0] ac-tex damaged at 3 6
[mpeg2video @ 0x16d72c0] 00 motion_type at 1 16
[mpeg2video @ 0x16d72c0] ac-tex damaged at 0 17
[mpeg2video @ 0x16d72c0] 00 motion_type at 5 21
[mpeg2video @ 0x16d72c0] 00 motion_type at 3 27
[mpeg2video @ 0x16d72c0] invalid cbp -1 at 7 32
[mpeg2video @ 0x16d72c0] ac-tex damaged at 8 32
[mpeg2video @ 0x16d72c0] slice mismatch
Last message repeated 2 times
[mpeg2video @ 0x16d72c0] Warning MVs not available
[mpeg2video @ 0x16d72c0] concealing 810 DC, 810 AC, 810 MV errors in P frame
RTP: missed 44 packets
[rtsp @ 0x1627c20] PES packet size mismatch
[ac3 @ 0x1676bc0] frame sync error
Error while decoding stream #0:0: Invalid data found when processing input
[ac3 @ 0x1676bc0] exponent out-of-range0 size= 338kB time=00:00:11.43 bitrate= 242.2kbits/s
[ac3 @ 0x1676bc0] error decoding the audio block
RTP: missed 35 packets
Last message repeated 1 times
RTP: missed 31 packets
[ac3 @ 0x1676bc0] frame sync error
Error while decoding stream #0:0: Invalid data found when processing input
[ac3 @ 0x1676bc0] exponent out-of-range
[ac3 @ 0x1676bc0] error decoding the audio block
[ac3 @ 0x1676bc0] frame sync error
Error while decoding stream #0:0: Invalid data found when processing input
RTP: missed 48 packets
[mpeg2video @ 0x16d72c0] ac-tex damaged at 5 23
[mpeg2video @ 0x16d72c0] mb incr damaged
[mpeg2video @ 0x16d72c0] 00 motion_type at 17 24
[mpeg2video @ 0x16d72c0] slice mismatch
Last message repeated 1 times
[mpeg2video @ 0x16d72c0] ac-tex damaged at 5 27
[mpeg2video @ 0x16d72c0] skip with previntra
[mpeg2video @ 0x16d72c0] 00 motion_type at 15 29
[mpeg2video @ 0x16d72c0] 00 motion_type at 8 31
[mpeg2video @ 0x16d72c0] 00 motion_type at 13 32
[mpeg2video @ 0x16d72c0] 00 motion_type at 22 33
[mpeg2video @ 0x16d72c0] 00 motion_type at 20 34
[mpeg2video @ 0x16d72c0] 00 motion_type at 17 35
[mpeg2video @ 0x16d72c0] Warning MVs not available
[mpeg2video @ 0x16d72c0] concealing 543 DC, 543 AC, 543 MV errors in B frame
[mpeg2video @ 0x16d72c0] 00 motion_type at 16 1
[mpeg2video @ 0x16d72c0] ac-tex damaged at 11 13
[mpeg2video @ 0x16d72c0] invalid mb type in P Frame at 4 7
[mpeg2video @ 0x16d72c0] 00 motion_type at 2 8
[mpeg2video @ 0x16d72c0] invalid cbp 0 at 0 19
[mpeg2video @ 0x16d72c0] invalid cbp 0 at 0 20
[mpeg2video @ 0x16d72c0] 00 motion_type at 9 21
[mpeg2video @ 0x16d72c0] ac-tex damaged at 2 22
[mpeg2video @ 0x16d72c0] 00 motion_type at 10 23
[mpeg2video @ 0x16d72c0] slice mismatch
[mpeg2video @ 0x16d72c0] invalid mb type in P Frame at 1 25
[mpeg2video @ 0x16d72c0] invalid cbp 0 at 38 26
[mpeg2video @ 0x16d72c0] invalid mb type in P Frame at 3 27
[mpeg2video @ 0x16d72c0] invalid mb type in P Frame at 22 28
[mpeg2video @ 0x16d72c0] 00 motion_type at 16 29
[mpeg2video @ 0x16d72c0] ac-tex damaged at 5 30
[mpeg2video @ 0x16d72c0] 00 motion_type at 14 31
[mpeg2video @ 0x16d72c0] ac-tex damaged at 2 32
[mpeg2video @ 0x16d72c0] invalid mb type in P Frame at 19 9
[mpeg2video @ 0x16d72c0] invalid mb type in P Frame at 11 10
[mpeg2video @ 0x16d72c0] ac-tex damaged at 13 11
[mpeg2video @ 0x16d72c0] ac-tex damaged at 8 12
[mpeg2video @ 0x16d72c0] 00 motion_type at 33 13
[mpeg2video @ 0x16d72c0] ac-tex damaged at 8 14
[mpeg2video @ 0x16d72c0] invalid cbp -1 at 6 15
[mpeg2video @ 0x16d72c0] invalid cbp -1 at 15 19
[mpeg2video @ 0x16d72c0] 00 motion_type at 9 18
[mpeg2video @ 0x16d72c0] mb incr damaged
[mpeg2video @ 0x16d72c0] invalid cbp -1 at 15 21
[mpeg2video @ 0x16d72c0] 00 motion_type at 13 21
[mpeg2video @ 0x16d72c0] invalid cbp 0 at 16 22
[mpeg2video @ 0x16d72c0] ac-tex damaged at 5 23
[mpeg2video @ 0x16d72c0] ac-tex damaged at 4 24
[mpeg2video @ 0x16d72c0] mb incr damaged
[mpeg2video @ 0x16d72c0] 00 motion_type at 7 26
[mpeg2video @ 0x16d72c0] slice mismatch
[mpeg2video @ 0x16d72c0] 00 motion_type at 9 13
[mpeg2video @ 0x16d72c0] ac-tex damaged at 44 14
[mpeg2video @ 0x16d72c0] invalid cbp 0 at 2 15
[mpeg2video @ 0x16d72c0] mb incr damaged
Last message repeated 1 times
[mpeg2video @ 0x16d72c0] 00 motion_type at 34 18
[mpeg2video @ 0x16d72c0] 00 motion_type at 15 19
[mpeg2video @ 0x16d72c0] mb incr damaged
[mpeg2video @ 0x16d72c0] 00 motion_type at 8 21
[mpeg2video @ 0x16d72c0] ac-tex damaged at 14 22
[mpeg2video @ 0x16d72c0] invalid cbp -1 at 9 23
[mpeg2video @ 0x16d72c0] invalid cbp 0 at 4 24
[mpeg2video @ 0x16d72c0] mb incr damaged
[mpeg2video @ 0x16d72c0] slice mismatch
Last message repeated 1 times
[mpeg2video @ 0x16d72c0] mb incr damaged
[mpeg2video @ 0x16d72c0] ac-tex damaged at 25 29
[mpeg2video @ 0x16d72c0] invalid cbp 0 at 14 30
[mpeg2video @ 0x16d72c0] mb incr damaged
Last message repeated 1 times
[mpeg2video @ 0x16d72c0] 00 motion_type at 35 33
[mpeg2video @ 0x16d72c0] slice mismatch
[mpeg2video @ 0x16d72c0] Warning MVs not available
[mpeg2video @ 0x16d72c0] concealing 1350 DC, 1350 AC, 1350 MV errors in P frame
[flv @ 0x16d7c40] Failed to update header with correct duration.:00:12.12 bitrate= 241.4kbits/s
[flv @ 0x16d7c40] Failed to update header with correct filesize.
[flv @ 0x16df5a0] Failed to update header with correct duration.
[flv @ 0x16df5a0] Failed to update header with correct filesize.
[flv @ 0x16cbe00] Failed to update header with correct duration.
[flv @ 0x16cbe00] Failed to update header with correct filesize.
frame= 136 fps= 12 q=-1.0 Lq=-1.0 q=-1.0 size= 633kB time=00:00:13.24 bitrate= 391.8kbits/s
video:7049kB audio:272kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
[libx264 @ 0x182b140] frame I:4 Avg QP:19.84 size: 33269
[libx264 @ 0x182b140] frame P:132 Avg QP:21.93 size: 3136
[libx264 @ 0x182b140] mb I I16..4: 6.9% 0.0% 93.1%
[libx264 @ 0x182b140] mb P I16..4: 0.1% 0.0% 0.7% P16..4: 32.0% 11.6% 4.4% 0.0% 0.0% skip:51.1%
[libx264 @ 0x182b140] coded y,uvDC,uvAC intra: 91.1% 88.6% 67.5% inter: 14.8% 20.2% 1.5%
[libx264 @ 0x182b140] i16 v,h,dc,p: 35% 15% 3% 47%
[libx264 @ 0x182b140] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 31% 19% 9% 6% 7% 8% 7% 7% 7%
[libx264 @ 0x182b140] i8c dc,h,v,p: 45% 20% 28% 7%
[libx264 @ 0x182b140] ref P L0: 80.5% 11.2% 8.4%
[libx264 @ 0x182b140] kb/s:369.61
[libx264 @ 0x16e03c0] frame I:4 Avg QP:19.81 size: 77367
[libx264 @ 0x16e03c0] frame P:132 Avg QP:21.64 size: 9825
[libx264 @ 0x16e03c0] mb I I16..4: 16.2% 0.0% 83.8%
[libx264 @ 0x16e03c0] mb P I16..4: 0.6% 0.0% 1.2% P16..4: 34.1% 9.5% 2.9% 0.0% 0.0% skip:51.8%
[libx264 @ 0x16e03c0] coded y,uvDC,uvAC intra: 76.8% 77.3% 41.8% inter: 11.9% 20.4% 0.7%
[libx264 @ 0x16e03c0] i16 v,h,dc,p: 38% 18% 7% 37%
[libx264 @ 0x16e03c0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 30% 22% 10% 5% 7% 7% 7% 6% 6%
[libx264 @ 0x16e03c0] i8c dc,h,v,p: 47% 21% 26% 6%
[libx264 @ 0x16e03c0] ref P L0: 80.8% 12.1% 7.1%
[libx264 @ 0x16e03c0] kb/s:1085.42
[libx264 @ 0x16cc880] frame I:4 Avg QP:15.79 size:181630
[libx264 @ 0x16cc880] frame P:132 Avg QP:18.21 size: 32848
[libx264 @ 0x16cc880] mb I I16..4: 13.7% 0.0% 86.3%
[libx264 @ 0x16cc880] mb P I16..4: 1.3% 0.0% 2.7% P16..4: 36.0% 14.2% 4.4% 0.0% 0.0% skip:41.4%
[libx264 @ 0x16cc880] coded y,uvDC,uvAC intra: 72.4% 70.6% 41.8% inter: 17.3% 24.9% 1.5%
[libx264 @ 0x16cc880] i16 v,h,dc,p: 33% 21% 6% 39%
[libx264 @ 0x16cc880] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 30% 23% 9% 5% 8% 7% 7% 6% 5%
[libx264 @ 0x16cc880] i8c dc,h,v,p: 44% 21% 27% 8%
[libx264 @ 0x16cc880] ref P L0: 80.3% 12.7% 7.0%
[libx264 @ 0x16cc880] kb/s:3420.59 -
ffmpeg record video plays too fast
29 avril 2023, par Kris XiaI'm a college student and I am studying FFmpeg now.



I have wrote a software that can record desktops and audio('virtual-audio-capturer') with FFmpeg.And I am now writing Audio and Video Synchronization.
I met some problems that video recording plays too fast.



When I look for audio and video synchronization help on the Internet,I find a formula for calculating PTS :



pts = n * ((1 / timbase)/ fps)



When I use this formula,I find a phenomenon.



1.The higher frame rate is,the faster the video playback speed.



2.The slower the frame rate, the faster the video playback.



Also I find while the framerate is 10,the video playback speed will be right.



Why has this situation happened ?



I have thought this question for three days. I really hope someone can help me solve this problem.



I really appreciate the help.



#include "stdafx.h"

#ifdef __cplusplus
extern "C"
{
#endif
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libavdevice/avdevice.h"
#include "libavutil/audio_fifo.h"

#include "libavfilter/buffersink.h"
#include "libavfilter/buffersrc.h"
#include "libavutil/imgutils.h"
#include "libavutil/mathematics.h"
#include "libavutil/samplefmt.h"
#include "libavutil/time.h"
#include "libavutil/opt.h"
#include "libavutil/pixdesc.h"
#include "libavutil/file.h"
#include "libavutil/mem.h"
#include "libavutil/frame.h"
#include "libavfilter/avfilter.h"
#include "libswresample/swresample.h"

#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "avdevice.lib")
#pragma comment(lib, "avfilter.lib")

#pragma comment(lib, "avfilter.lib")
#pragma comment(lib, "postproc.lib")
#pragma comment(lib, "swresample.lib")
#pragma comment(lib, "swscale.lib")
#ifdef __cplusplus
};
#endif

AVFormatContext *pFormatCtx_Video = NULL, *pFormatCtx_Audio = NULL, *pFormatCtx_Out = NULL;

AVCodecContext *outVideoCodecCtx = NULL;
AVCodecContext *outAudioCodecCtx = NULL;

AVStream *pVideoStream = NULL, *pAudioStream = NULL;

AVCodec *outAVCodec;
AVCodec *outAudioCodec;

AVCodecContext *pCodecCtx_Video;
AVCodec *pCodec_Video;
AVFifoBuffer *fifo_video = NULL;
AVAudioFifo *fifo_audio = NULL;
int VideoIndex, AudioIndex;
int codec_id;

CRITICAL_SECTION AudioSection, VideoSection;



SwsContext *img_convert_ctx;
int frame_size = 0;

uint8_t *picture_buf = NULL, *frame_buf = NULL;

bool bCap = true;

DWORD WINAPI ScreenCapThreadProc( LPVOID lpParam );
DWORD WINAPI AudioCapThreadProc( LPVOID lpParam );

int OpenVideoCapture()
{
 AVInputFormat *ifmt=av_find_input_format("gdigrab");
 AVDictionary *options = NULL;
 av_dict_set(&options, "framerate", "60", NULL);
 if(avformat_open_input(&pFormatCtx_Video, "desktop", ifmt, &options)!=0)
 {
 printf("Couldn't open input stream.(无法打开视频输入流)\n");
 return -1;
 }
 if(avformat_find_stream_info(pFormatCtx_Video,NULL)<0)
 {
 printf("Couldn't find stream information.(无法获取视频流信息)\n");
 return -1;
 }
 if (pFormatCtx_Video->streams[0]->codec->codec_type != AVMEDIA_TYPE_VIDEO)
 {
 printf("Couldn't find video stream information.(无法获取视频流信息)\n");
 return -1;
 }
 pCodecCtx_Video = pFormatCtx_Video->streams[0]->codec;
 pCodec_Video = avcodec_find_decoder(pCodecCtx_Video->codec_id);
 if(pCodec_Video == NULL)
 {
 printf("Codec not found.(没有找到解码器)\n");
 return -1;
 }
 if(avcodec_open2(pCodecCtx_Video, pCodec_Video, NULL) < 0)
 {
 printf("Could not open codec.(无法打开解码器)\n");
 return -1;
 }

 av_dump_format(pFormatCtx_Video, 0, NULL, 0);

 img_convert_ctx = sws_getContext(pCodecCtx_Video->width, pCodecCtx_Video->height, pCodecCtx_Video->pix_fmt, 
 pCodecCtx_Video->width, pCodecCtx_Video->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL); 

 frame_size = avpicture_get_size(pCodecCtx_Video->pix_fmt, pCodecCtx_Video->width, pCodecCtx_Video->height);
 fifo_video = av_fifo_alloc(30 * avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx_Video->width, pCodecCtx_Video->height));

 return 0;
}

static char *dup_wchar_to_utf8(wchar_t *w)
{
 char *s = NULL;
 int l = WideCharToMultiByte(CP_UTF8, 0, w, -1, 0, 0, 0, 0);
 s = (char *) av_malloc(l);
 if (s)
 WideCharToMultiByte(CP_UTF8, 0, w, -1, s, l, 0, 0);
 return s;
}

int OpenAudioCapture()
{
 AVInputFormat *pAudioInputFmt = av_find_input_format("dshow");
 char * psDevName = dup_wchar_to_utf8(L"audio=virtual-audio-capturer");

 if (avformat_open_input(&pFormatCtx_Audio, psDevName, pAudioInputFmt,NULL) < 0)
 {
 printf("Couldn't open input stream.(无法打开音频输入流)\n");
 return -1;
 }

 if(avformat_find_stream_info(pFormatCtx_Audio,NULL)<0) 
 return -1; 

 if(pFormatCtx_Audio->streams[0]->codec->codec_type != AVMEDIA_TYPE_AUDIO)
 {
 printf("Couldn't find video stream information.(无法获取音频流信息)\n");
 return -1;
 }

 AVCodec *tmpCodec = avcodec_find_decoder(pFormatCtx_Audio->streams[0]->codec->codec_id);
 if(0 > avcodec_open2(pFormatCtx_Audio->streams[0]->codec, tmpCodec, NULL))
 {
 printf("can not find or open audio decoder!\n");
 }

 av_dump_format(pFormatCtx_Audio, 0, NULL, 0);

 return 0;
}

int OpenOutPut()
{
 AVStream *pVideoStream = NULL, *pAudioStream = NULL;
 const char *outFileName = "test.mp4";
 avformat_alloc_output_context2(&pFormatCtx_Out, NULL, NULL, outFileName);

 if (pFormatCtx_Video->streams[0]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
 {
 VideoIndex = 0;
 pVideoStream = avformat_new_stream(pFormatCtx_Out, NULL);
 if (!pVideoStream)
 {
 printf("can not new stream for output!\n");
 return -1;
 }

 outVideoCodecCtx = avcodec_alloc_context3(outAVCodec);
 if ( !outVideoCodecCtx )
 {
 printf("Error : avcodec_alloc_context3()\n");
 return -1;
 }

 //set codec context param
 outVideoCodecCtx = pVideoStream->codec;
 outVideoCodecCtx->codec_id = AV_CODEC_ID_MPEG4;
 outVideoCodecCtx->width = pFormatCtx_Video->streams[0]->codec->width;
 outVideoCodecCtx->height = pFormatCtx_Video->streams[0]->codec->height;
 outVideoCodecCtx->time_base = pFormatCtx_Video->streams[0]->codec->time_base;
 outVideoCodecCtx->pix_fmt = AV_PIX_FMT_YUV420P;
 outVideoCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;

 if (codec_id == AV_CODEC_ID_H264)
 {
 av_opt_set(outVideoCodecCtx->priv_data, "preset", "slow", 0);
 }

 outAVCodec = avcodec_find_encoder(AV_CODEC_ID_MPEG4);
 if( !outAVCodec )
 {
 printf("\n\nError : avcodec_find_encoder()");
 return -1;
 }
 if (pFormatCtx_Out->oformat->flags & AVFMT_GLOBALHEADER)
 outVideoCodecCtx->flags |=CODEC_FLAG_GLOBAL_HEADER;

 if ((avcodec_open2(outVideoCodecCtx,outAVCodec, NULL)) < 0)
 {
 printf("can not open the encoder\n");
 return -1;
 }
 }

 if(pFormatCtx_Audio->streams[0]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
 {
 AVCodecContext *pOutputCodecCtx;
 AudioIndex = 1;
 pAudioStream = avformat_new_stream(pFormatCtx_Out, NULL);

 pAudioStream->codec->codec = avcodec_find_encoder(pFormatCtx_Out->oformat->audio_codec);

 pOutputCodecCtx = pAudioStream->codec;

 pOutputCodecCtx->sample_rate = pFormatCtx_Audio->streams[0]->codec->sample_rate;
 pOutputCodecCtx->channel_layout = pFormatCtx_Out->streams[0]->codec->channel_layout;
 pOutputCodecCtx->channels = av_get_channel_layout_nb_channels(pAudioStream->codec->channel_layout);
 if(pOutputCodecCtx->channel_layout == 0)
 {
 pOutputCodecCtx->channel_layout = AV_CH_LAYOUT_STEREO;
 pOutputCodecCtx->channels = av_get_channel_layout_nb_channels(pOutputCodecCtx->channel_layout);

 }
 pOutputCodecCtx->sample_fmt = pAudioStream->codec->codec->sample_fmts[0];
 AVRational time_base={1, pAudioStream->codec->sample_rate};
 pAudioStream->time_base = time_base;
 //audioCodecCtx->time_base = time_base;

 pOutputCodecCtx->codec_tag = 0; 
 if (pFormatCtx_Out->oformat->flags & AVFMT_GLOBALHEADER) 
 pOutputCodecCtx->flags |= CODEC_FLAG_GLOBAL_HEADER;

 if (avcodec_open2(pOutputCodecCtx, pOutputCodecCtx->codec, 0) < 0)
 {
 printf("编码器打开失败,退出程序\n");
 return -1;
 }
 }

 if (!(pFormatCtx_Out->oformat->flags & AVFMT_NOFILE))
 {
 if(avio_open(&pFormatCtx_Out->pb, outFileName, AVIO_FLAG_WRITE) < 0)
 {
 printf("can not open output file handle!\n");
 return -1;
 }
 }

 if(avformat_write_header(pFormatCtx_Out, NULL) < 0)
 {
 printf("can not write the header of the output file!\n");
 return -1;
 }

 return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
 av_register_all();
 avdevice_register_all();
 if (OpenVideoCapture() < 0)
 {
 return -1;
 }
 if (OpenAudioCapture() < 0)
 {
 return -1;
 }
 if (OpenOutPut() < 0)
 {
 return -1;
 }
// int fps;
 /*printf("输入帧率:");
 scanf_s("%d",&fps);
 if ( NULL == fps)
 {
 fps = 10;
 }*/

 InitializeCriticalSection(&VideoSection);
 InitializeCriticalSection(&AudioSection);

 AVFrame *picture = av_frame_alloc();
 int size = avpicture_get_size(pFormatCtx_Out->streams[VideoIndex]->codec->pix_fmt, 
 pFormatCtx_Out->streams[VideoIndex]->codec->width, pFormatCtx_Out->streams[VideoIndex]->codec->height);
 picture_buf = new uint8_t[size];

 avpicture_fill((AVPicture *)picture, picture_buf, 
 pFormatCtx_Out->streams[VideoIndex]->codec->pix_fmt, 
 pFormatCtx_Out->streams[VideoIndex]->codec->width, 
 pFormatCtx_Out->streams[VideoIndex]->codec->height);



 //star cap screen thread
 CreateThread( NULL, 0, ScreenCapThreadProc, 0, 0, NULL);
 //star cap audio thread
 CreateThread( NULL, 0, AudioCapThreadProc, 0, 0, NULL);
 int64_t cur_pts_v=0,cur_pts_a=0;
 int VideoFrameIndex = 0, AudioFrameIndex = 0;

 while(1)
 {
 if (_kbhit() != 0 && bCap)
 {
 bCap = false;
 Sleep(2000);
 }
 if (fifo_audio && fifo_video)
 {
 int sizeAudio = av_audio_fifo_size(fifo_audio);
 int sizeVideo = av_fifo_size(fifo_video);
 //缓存数据写完就结束循环
 if (av_audio_fifo_size(fifo_audio) <= pFormatCtx_Out->streams[AudioIndex]->codec->frame_size && 
 av_fifo_size(fifo_video) <= frame_size && !bCap)
 {
 break;
 }
 }

 if(av_compare_ts(cur_pts_v, pFormatCtx_Out->streams[VideoIndex]->time_base, 
 cur_pts_a,pFormatCtx_Out->streams[AudioIndex]->time_base) <= 0)
 {
 if (av_fifo_size(fifo_video) < frame_size && !bCap)
 {
 cur_pts_v = 0x7fffffffffffffff;
 }
 if(av_fifo_size(fifo_video) >= size)
 {
 EnterCriticalSection(&VideoSection);
 av_fifo_generic_read(fifo_video, picture_buf, size, NULL); //将数据从avfifobuffer馈送到用户提供的回调。
 LeaveCriticalSection(&VideoSection);

 avpicture_fill((AVPicture *)picture, picture_buf,
 pFormatCtx_Out->streams[VideoIndex]->codec->pix_fmt,
 pFormatCtx_Out->streams[VideoIndex]->codec->width,
 pFormatCtx_Out->streams[VideoIndex]->codec->height); //根据指定的图像参数和提供的图像数据缓冲区设置图片字段。

 //pts = n * ((1 / timbase)/ fps);
 //picture->pts = VideoFrameIndex * ((pFormatCtx_Video->streams[0]->time_base.den / pFormatCtx_Video->streams[0]->time_base.num) / 24);
 picture->pts = VideoFrameIndex * ((outVideoCodecCtx->time_base.den * 100000 / outVideoCodecCtx->time_base.num) / 180);

 int got_picture = 0;
 AVPacket pkt;
 av_init_packet(&pkt);

 pkt.data = NULL;
 pkt.size = 0;
 //从帧中获取输入的原始视频数据
 int ret = avcodec_encode_video2(pFormatCtx_Out->streams[VideoIndex]->codec, &pkt, picture, &got_picture);
 if(ret < 0)
 {
 continue;
 }

 if (got_picture==1)
 {
 pkt.stream_index = VideoIndex;
 /*int count = 1;
 pkt.pts = pkt.dts = count * ((pFormatCtx_Video->streams[0]->time_base.den / pFormatCtx_Video->streams[0]->time_base.num) / 15);
 count++;*/

 //x = pts * (timebase1.num / timebase1.den )* (timebase2.den / timebase2.num);

 pkt.pts = av_rescale_q_rnd(pkt.pts, pFormatCtx_Video->streams[0]->time_base, 
 pFormatCtx_Out->streams[VideoIndex]->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX)); 
 pkt.dts = av_rescale_q_rnd(pkt.dts, pFormatCtx_Video->streams[0]->time_base, 
 pFormatCtx_Out->streams[VideoIndex]->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX)); 


 pkt.duration = ((pFormatCtx_Out->streams[0]->time_base.den / pFormatCtx_Out->streams[0]->time_base.num) / 60);
 //pkt.duration = 1000/60;
 //pkt.pts = pkt.dts = Count * (ofmt_ctx->streams[stream_index]->time_base.den) /ofmt_ctx->streams[stream_index]->time_base.num / 10;

 //Count++;


 cur_pts_v = pkt.pts;

 ret = av_interleaved_write_frame(pFormatCtx_Out, &pkt);
 //delete[] pkt.data;
 av_free_packet(&pkt);
 }
 VideoFrameIndex++;
 }
 }
 else
 {
 if (NULL == fifo_audio)
 {
 continue;//还未初始化fifo
 }
 if (av_audio_fifo_size(fifo_audio) < pFormatCtx_Out->streams[AudioIndex]->codec->frame_size && !bCap)
 {
 cur_pts_a = 0x7fffffffffffffff;
 }
 if(av_audio_fifo_size(fifo_audio) >= 
 (pFormatCtx_Out->streams[AudioIndex]->codec->frame_size > 0 ? pFormatCtx_Out->streams[AudioIndex]->codec->frame_size : 1024))
 {
 AVFrame *frame;
 frame = av_frame_alloc();
 frame->nb_samples = pFormatCtx_Out->streams[AudioIndex]->codec->frame_size>0 ? pFormatCtx_Out->streams[AudioIndex]->codec->frame_size: 1024;
 frame->channel_layout = pFormatCtx_Out->streams[AudioIndex]->codec->channel_layout;
 frame->format = pFormatCtx_Out->streams[AudioIndex]->codec->sample_fmt;
 frame->sample_rate = pFormatCtx_Out->streams[AudioIndex]->codec->sample_rate;
 av_frame_get_buffer(frame, 0);

 EnterCriticalSection(&AudioSection);
 av_audio_fifo_read(fifo_audio, (void **)frame->data, 
 (pFormatCtx_Out->streams[AudioIndex]->codec->frame_size > 0 ? pFormatCtx_Out->streams[AudioIndex]->codec->frame_size : 1024));
 LeaveCriticalSection(&AudioSection);

 AVPacket pkt_out;
 av_init_packet(&pkt_out);
 int got_picture = -1;
 pkt_out.data = NULL;
 pkt_out.size = 0;

 frame->pts = AudioFrameIndex * pFormatCtx_Out->streams[AudioIndex]->codec->frame_size;
 if (avcodec_encode_audio2(pFormatCtx_Out->streams[AudioIndex]->codec, &pkt_out, frame, &got_picture) < 0)
 {
 printf("can not decoder a frame");
 }
 av_frame_free(&frame);
 if (got_picture) 
 {
 pkt_out.stream_index = AudioIndex;
 pkt_out.pts = AudioFrameIndex * pFormatCtx_Out->streams[AudioIndex]->codec->frame_size;
 pkt_out.dts = AudioFrameIndex * pFormatCtx_Out->streams[AudioIndex]->codec->frame_size;
 pkt_out.duration = pFormatCtx_Out->streams[AudioIndex]->codec->frame_size;

 cur_pts_a = pkt_out.pts;

 int ret = av_interleaved_write_frame(pFormatCtx_Out, &pkt_out);
 av_free_packet(&pkt_out);
 }
 AudioFrameIndex++;
 }
 }
 }

 delete[] picture_buf;

 av_fifo_free(fifo_video);
 av_audio_fifo_free(fifo_audio);

 av_write_trailer(pFormatCtx_Out);

 avio_close(pFormatCtx_Out->pb);
 avformat_free_context(pFormatCtx_Out);

 if (pFormatCtx_Video != NULL)
 {
 avformat_close_input(&pFormatCtx_Video);
 pFormatCtx_Video = NULL;
 }
 if (pFormatCtx_Audio != NULL)
 {
 avformat_close_input(&pFormatCtx_Audio);
 pFormatCtx_Audio = NULL;
 }

 return 0;
}

DWORD WINAPI ScreenCapThreadProc( LPVOID lpParam )
{
 AVPacket packet;
 int got_picture;
 AVFrame *pFrame;
 pFrame=av_frame_alloc();

 AVFrame *picture = av_frame_alloc();
 int size = avpicture_get_size(pFormatCtx_Out->streams[VideoIndex]->codec->pix_fmt, 
 pFormatCtx_Out->streams[VideoIndex]->codec->width, 
 pFormatCtx_Out->streams[VideoIndex]->codec->height);

 avpicture_fill((AVPicture *)picture, picture_buf, 
 pFormatCtx_Out->streams[VideoIndex]->codec->pix_fmt, 
 pFormatCtx_Out->streams[VideoIndex]->codec->width, 
 pFormatCtx_Out->streams[VideoIndex]->codec->height);

 FILE *p = NULL;
 p = fopen("proc_test.yuv", "wb+");
 av_init_packet(&packet);
 int height = pFormatCtx_Out->streams[VideoIndex]->codec->height;
 int width = pFormatCtx_Out->streams[VideoIndex]->codec->width;
 int y_size=height*width;
 while(bCap)
 {
 packet.data = NULL;
 packet.size = 0;
 if (av_read_frame(pFormatCtx_Video, &packet) < 0)
 {
 continue;
 }
 if(packet.stream_index == 0)
 {
 if (avcodec_decode_video2(pCodecCtx_Video, pFrame, &got_picture, &packet) < 0)
 {
 printf("Decode Error.(解码错误)\n");
 continue;
 }
 if (got_picture)
 {
 sws_scale(img_convert_ctx, 
 (const uint8_t* const*)pFrame->data,
 pFrame->linesize, 
 0, 
 pFormatCtx_Out->streams[VideoIndex]->codec->height,
 picture->data,
 picture->linesize);

 if (av_fifo_space(fifo_video) >= size)
 {
 EnterCriticalSection(&VideoSection); 
 av_fifo_generic_write(fifo_video, picture->data[0], y_size, NULL);
 av_fifo_generic_write(fifo_video, picture->data[1], y_size/4, NULL);
 av_fifo_generic_write(fifo_video, picture->data[2], y_size/4, NULL);
 LeaveCriticalSection(&VideoSection);
 }
 }
 }
 av_free_packet(&packet);
 }
 av_frame_free(&pFrame);
 av_frame_free(&picture);
 return 0;
}

DWORD WINAPI AudioCapThreadProc( LPVOID lpParam )
{
 AVPacket pkt;
 AVFrame *frame;
 frame = av_frame_alloc();
 int gotframe;
 while(bCap)
 {
 pkt.data = NULL;
 pkt.size = 0;
 if(av_read_frame(pFormatCtx_Audio,&pkt) < 0)
 {
 continue;
 }

 if (avcodec_decode_audio4(pFormatCtx_Audio->streams[0]->codec, frame, &gotframe, &pkt) < 0)
 {
 av_frame_free(&frame);
 printf("can not decoder a frame");
 break;
 }
 av_free_packet(&pkt);

 if (!gotframe)
 {
 printf("没有获取到数据,继续下一次");
 continue;
 }

 if (NULL == fifo_audio)
 {
 fifo_audio = av_audio_fifo_alloc(pFormatCtx_Audio->streams[0]->codec->sample_fmt, 
 pFormatCtx_Audio->streams[0]->codec->channels, 30 * frame->nb_samples);
 }

 int buf_space = av_audio_fifo_space(fifo_audio);
 if (av_audio_fifo_space(fifo_audio) >= frame->nb_samples)
 {
 EnterCriticalSection(&AudioSection);
 av_audio_fifo_write(fifo_audio, (void **)frame->data, frame->nb_samples);
 LeaveCriticalSection(&AudioSection);
 }
 }
 av_frame_free(&frame);
 return 0;
}




Maybe there is another way to calculate PTS and DTS



I hope whatever the frame rate is,video playback speed is right.Not too fast or too slow.


-
Why does my lambda doesn't end with an expected request end event ? [on hold]
16 avril 2019, par Nachum Freedmanconst now = (...a) =>
console.log(...a, Math.floor(new Date().getTime() / 1000) % 3600);
exports.handler = (event, context, callback) => {
console.log("PROCESS START");
const FROM_BUCKET = event.Records[0].s3.bucket.name;
const Key = decodeURIComponent(
event.Records[0].s3.object.key.replace(/\+/g, " ")
);
const uploadKey = Key.replace(/.webm/, ".mp4");
console.log("FROM BUCKET", FROM_BUCKET);
console.log("Recived key", Key);
const slicedFilename = Key.slice(-4) !== "webm" ? Key.slice(-4) : ".webm";
const extension =
slicedFilename === ".mp4"
? ".mp4"
: slicedFilename === ".pcm"
? ".pcm"
: slicedFilename === ".webm"
? ".webm"
: console.log("THE FILE NAME IS INCORRECT PLEASE CHECK @:", Key);
console.log("Extension detected is", extension);
const downloadKey =
Key.slice(-4) !== "webm"
? Key.slice(0, -4) + extension
: Key.slice(0, -5) + extension;
const downloadParams = { Bucket: FROM_BUCKET, Key: downloadKey };
console.log("Downloading file from S3", downloadParams);
const mobileDownloadExtension =
extension === ".mp4" ? ".pcm" : extension === ".pcm" ? ".mp4" : null;
const mobileDownloadParams = {
Bucket: FROM_BUCKET,
Key: Key.slice(0, -4) + mobileDownloadExtension
};
console.log("Downloading file from S3", mobileDownloadParams);
const tmpNamespace = Math.random();
const isMobile = extension === ".mp4" || extension === ".pcm";
let firstDigit = 0;
let restOfTheOffset = 0;
if (isMobile) {
console.log(
"If apply, second key to download is a file type",
mobileDownloadExtension
);
const offsetTime = Key.slice(
Key.lastIndexOf("_") + 1,
Key.indexOf(".")
).replace("-", "");
if (offsetTime > 999) {
firstDigit = 1;
restOfTheOffset = offsetTime.slice(1, offsetTime.length);
} else {
firstDigit = 0;
restOfTheOffset = offsetTime;
}
}
console.log("FIle recieved from Mobile?", isMobile);
Promise.all([
// download file from s3
new Promise((resolve, reject) =>
s3.getObject(downloadParams, (err, response) => {
if (err) {
console.error(
"Error while downloading file from S3",
downloadParams,
err.code,
"-",
err.message
);
return reject(err);
}
console.log("Successfully downloaed file from S3", downloadParams);
fs.writeFile(
tmp + "/input" + tmpNamespace + extension,
response.Body,
err =>
err
? console.log(err.code, "-", err.message) || reject(err)
: console.log(tmp + "/input" + tmpNamespace + extension) ||
resolve()
);
})
),
new Promise((resolve, reject) => {
extension !== ".mp4" && extension !== ".pcm"
? resolve()
: s3.getObject(mobileDownloadParams, (err, response) => {
if (err) {
console.error(
"Error while downloading file from S3",
mobileDownloadParams,
err.code,
"-",
err.message
);
return reject(err);
}
console.log(
"Successfully downloaed file from S3",
mobileDownloadParams
);
fs.writeFile(
tmp + "/input" + tmpNamespace + mobileDownloadExtension,
response.Body,
err =>
err
? console.log(err.code, "-", err.message) || reject(err)
: console.log(
tmp + "/input" + tmpNamespace + mobileDownloadExtension
) || resolve()
);
});
})
])
.then(() =>
Promise.all([
// call the answerVideoReady -> PROCESSING mobileDownloadExtnesion is actually the second file we seek (if mp4 then pcm)
// ,
isMobile
? Promise.resolve()
.then(() => {
new Promise((resolve, reject) => {
console.log("CALLING VIDEO PROCESSING", Key);
videoProcessing(Key);
resolve();
});
})
.then(
() =>
// run ffmpeg
// ffmpeg -i tmp/input.mp4 -movflags faststart -acodec copy -vcodec copy output.mp4
now("FFMPEG CONVERT PCM TO WAV START") ||
new Promise((resolve, reject) =>
spawn("./ffmpeg/ffmpeg", [
"-f",
"s16le",
"-ar",
"16000",
"-ac",
"1",
"-i",
tmp + "/input" + tmpNamespace + ".pcm",
"-ar",
"44100",
"-ac",
"2",
tmp + "/input" + tmpNamespace + ".wav"
]).on("close", code =>
console.log("FFMPEG CONVERT PCM TO WAV SUCCESS", code) ||
!code
? resolve()
: reject()
)
)
)
.then(
() =>
// run ffmpeg
// ffmpeg -i tmp/input.mp4 -movflags faststart -acodec copy -vcodec copy output.mp4
//more stuff
now("FFMPEG COMBINE MP4 AND WAV START") ||
new Promise(
(resolve, reject) =>
console.log(
[
"-i",
tmp + "/input" + tmpNamespace + ".mp4",
"-itsoffset",
"-" + firstDigit + "." + restOfTheOffset,
"-i",
tmp + "/input" + tmpNamespace + ".wav",
"-movflags",
"faststart",
"-filter_complex",
" [1:0] apad ",
"-shortest",
tmp + "/output" + tmpNamespace + ".mp4"
].join(" ")
) ||
spawn("./ffmpeg/ffmpeg", [
"-i",
tmp + "/input" + tmpNamespace + ".mp4",
"-itsoffset",
"-" + firstDigit + "." + restOfTheOffset,
"-i",
tmp + "/input" + tmpNamespace + ".wav",
"-movflags",
"faststart",
"-filter_complex",
" [1:0] apad ",
"-shortest",
tmp + "/output" + tmpNamespace + ".mp4"
]).on("close", code =>
console.log(
"FFMPEG COMBINE MP4 AND WAV SUCCESS",
code
) || !code
? resolve()
: reject()
)
)
)
: Promise.resolve()
.then(() => {
new Promise((resolve, reject) => {
console.log("CALLING VIDEO PROCESSING", Key);
videoProcessing(Key);
resolve();
});
})
.then(
() =>
now("FFMPEG SEND WEBM START") ||
new Promise((resolve, reject) => {
exec(
"./sed -i '1,4d;$d' " +
tmp +
"/input" +
tmpNamespace +
".webm"
).on(
"close",
code =>
console.log("FFMPEG SEND WEBM SUCCESS", code) ||
(!code ? resolve() : reject())
);
})
)
.then(
() =>
// run ffmpeg
// ffmpeg -i tmp/input.mp4 -movflags faststart -acodec copy -vcodec copy output.mp4
now("FFMPEG WEBM WEB READY START") ||
new Promise((resolve, reject) =>
(a => {
a.stdout.on("data", data => {
//console.log(`child stdout:\n${data}`);
});
a.stderr.on("data", data => {
//console.log(`child stdout:\n${data}`);
});
return a;
})(
spawn("./ffmpeg/ffmpeg", [
"-i",
tmp + "/input" + tmpNamespace + extension,
"-movflags",
"faststart",
"-acodec",
"aac",
"-vcodec",
"h264",
"-preset",
"slow",
"-crf",
"26",
"-r",
"25",
tmp + "/output" + tmpNamespace + ".mp4"
])
).on("close", code =>
console.log(
"FFMPEG WEBM WEB READY FINISHED WITH:",
code
) || !code
? resolve()
: reject()
)
)
)
])
)
.then(
() =>
new Promise((resolve, reject) =>
// upload the output.mp4 to s3
fs.readFile(
tmp + "/output" + tmpNamespace + ".mp4",
(err, filedata) => {
if (err) {
console.log("ERROR WHILE TRYING TO READ FILE", err);
throw err;
}
console.log("KEEEEYYY", uploadKey),
s3.putObject(
{
Bucket: TO_BUCKET,
Key: uploadKey,
Body: filedata
},
(err, response) => {
console.log(response);
if (err) {
console.log(
"ERROR WHILE UPLOADING FILE TO S3",
err,
response
);
return reject(err);
}
//uploading file
console.log(
"Successfully uploaded file to " + TO_BUCKET,
Key
);
resolve();
}
);
}
)
)
// call the answerVideoReady -> COMPLETED, context.success or ERROR, context.fail or error on set status to ERROR -> fail
)
.then(p =>
videoCompleted(Key)
.then(c => context.succeed())
.catch(es => context.fail(es))
)
.catch(
e =>
console.log("catch for upload error with:", e) ||
videoError(Key)
.then(p => context.fail(e))
.catch(ee => context.fail(ee))
);
};