{"id":4519,"date":"2014-03-30T13:11:45","date_gmt":"2014-03-30T13:11:45","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/desktop-audio-falls-behind-when-recording-microphone-desktop-audio-screen-using-ffmpeg-collection-of-common-programming-errors\/"},"modified":"2014-03-30T13:11:45","modified_gmt":"2014-03-30T13:11:45","slug":"desktop-audio-falls-behind-when-recording-microphone-desktop-audio-screen-using-ffmpeg-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/desktop-audio-falls-behind-when-recording-microphone-desktop-audio-screen-using-ffmpeg-collection-of-common-programming-errors\/","title":{"rendered":"Desktop audio falls behind when recording microphone + desktop audio + screen using ffmpeg-Collection of common programming errors"},"content":{"rendered":"<p>I have put together this script for recording the microphone, the desktop audio and the screen using ffmpeg:<\/p>\n<pre><code>DATE=`which date`\nRESO=2560x1440\nFPS=30\nPRESET=ultrafast\nDIRECTORY=$HOME\/Video\/\nFILENAME=videocast`$DATE +%d%m%Y_%H.%M.%S`.mkv\n\nffmpeg -y -vsync 1 \\\n-f pulse -ac 2 -i alsa_output.pci-0000_00_1b.0.analog-stereo.monitor \\\n-f pulse -ac 1 -ar 25000 -i alsa_input.usb-0d8c_C-Media_USB_Headphone_Set-00-Set.analog-mono \\\n-filter_complex aresample=async=1,amix=duration=shortest,apad \\\n-f x11grab -r $FPS -s $RESO -i :0.0 \\\n-acodec libvorbis \\\n-vcodec libx264 -pix_fmt yuv420p -preset $PRESET -threads 0 \\\n$DIRECTORY$FILENAME\n<\/code><\/pre>\n<p>Everything is recorded and between the screen and the microphone sound there are no issues what so ever, however the desktop audio falls behind badly.<\/p>\n<p>It begins in sync but gets worse over time during playback, also in ffplay. It does not matter what application playing sound: both Youtube-videos in the browser, desktop sounds and Rhythmbox (playing a couple of seconds of song then stops, wait and repeat) gets out of sync.<\/p>\n<p>The terminal output complain about<\/p>\n<pre><code>\"ALSA lib pcm.c:7843:(snd_pcm_recover) overrun occurred22.73 bitrate=10384.5kbits\/s    \nALSA lib pcm.c:7843:(snd_pcm_recover) underrun occurred\" \n<\/code><\/pre>\n<p>and similar but I do not know what that means.<\/p>\n<p>Full terminal output here:<\/p>\n<pre><code>ffmpeg version 2.0.1 Copyright (c) 2000-2013 the FFmpeg developers\n  built on Aug 11 2013 14:52:28 with gcc 4.8.1 (GCC) 20130725 (prerelease)\n  configuration: --prefix=\/usr --disable-debug --disable-static --enable-avresample --enable-dxva2 --enable-fontconfig --enable-gpl --enable-libass --enable-libbluray --enable-libfreetype --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-librtmp --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-libv4l2 --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxvid --enable-pic --enable-postproc --enable-runtime-cpudetect --enable-shared --enable-swresample --enable-vdpau --enable-version3 --enable-x11grab\n  libavutil      52. 38.100 \/ 52. 38.100\n  libavcodec     55. 18.102 \/ 55. 18.102\n  libavformat    55. 12.100 \/ 55. 12.100\n  libavdevice    55.  3.100 \/ 55.  3.100\n  libavfilter     3. 79.101 \/  3. 79.101\n  libavresample   1.  1.  0 \/  1.  1.  0\n  libswscale      2.  3.100 \/  2.  3.100\n  libswresample   0. 17.102 \/  0. 17.102\n  libpostproc    52.  3.100 \/ 52.  3.100\nGuessed Channel Layout for  Input Stream #0.0 : stereo\nInput #0, pulse, from 'alsa_output.pci-0000_00_1b.0.analog-stereo.monitor':\n  Duration: N\/A, start: 0.014093, bitrate: 1536 kb\/s\n    Stream #0:0: Audio: pcm_s16le, 48000 Hz, stereo, s16, 1536 kb\/s\nGuessed Channel Layout for  Input Stream #1.0 : mono\nInput #1, pulse, from 'alsa_input.usb-0d8c_C-Media_USB_Headphone_Set-00-Set.analog-mono':\n  Duration: N\/A, start: 0.006172, bitrate: 400 kb\/s\n    Stream #1:0: Audio: pcm_s16le, 25000 Hz, mono, s16, 400 kb\/s\n[x11grab @ 0x218a6e0] device: :0.0 -&gt; display: :0.0 x: 0 y: 0 width: 2560 height: 1440\n[x11grab @ 0x218a6e0] shared memory extension found\nInput #2, x11grab, from ':0.0':\n  Duration: N\/A, start: 1379021580.184321, bitrate: N\/A\n    Stream #2:0: Video: rawvideo (BGR[0] \/ 0x524742), bgr0, 2560x1440, -2147483 kb\/s, 30 tbr, 1000k tbn, 30 tbc\n[libx264 @ 0x21ae560] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX\n[libx264 @ 0x21ae560] profile Constrained Baseline, level 5.0\n[libx264 @ 0x21ae560] 264 - core 133 r2339 585324f - H.264\/MPEG-4 AVC codec - Copyleft 2003-2013 - http:\/\/www.videolan.org\/x264.html - options: cabac=0 ref=1 deblock=0:0:0 analyse=0:0 me=dia subme=0 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=0 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=0 threads=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=0 keyint=250 keyint_min=25 scenecut=0 intra_refresh=0 rc=crf mbtree=0 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=0\nOutput #0, matroska, to '\/home\/anders\/Video\/videocast12092013_23.33.00.mkv':\n  Metadata:\n    encoder         : Lavf55.12.100\n    Stream #0:0: Audio: vorbis (libvorbis) (oV[0][0] \/ 0x566F), 25000 Hz, mono, fltp\n    Stream #0:1: Video: h264 (libx264) (H264 \/ 0x34363248), yuv420p, 2560x1440, q=-1--1, 1k tbn, 30 tbc\nStream mapping:\n  Stream #0:0 (pcm_s16le) -&gt; aresample (graph 0)\n  Stream #1:0 (pcm_s16le) -&gt; amix:input1 (graph 0)\n  amix (graph 0) -&gt; Stream #0:0 (libvorbis)\n  Stream #2:0 -&gt; #0:1 (rawvideo -&gt; libx264)\nPress [q] to stop, [?] for help\nALSA lib pcm.c:7843:(snd_pcm_recover) overrun occurred22.73 bitrate=10384.5kbits\/s    \nALSA lib pcm.c:7843:(snd_pcm_recover) underrun occurred\nALSA lib pcm.c:7843:(snd_pcm_recover) underrun occurred3.22 bitrate=10423.3kbits\/s    \nALSA lib pcm.c:7843:(snd_pcm_recover) overrun occurred25.25 bitrate=11011.0kbits\/s    \nALSA lib pcm.c:7843:(snd_pcm_recover) underrun occurred\nALSA lib pcm.c:7843:(snd_pcm_recover) underrun occurred5.76 bitrate=11013.7kbits\/s    \nALSA lib pcm.c:7843:(snd_pcm_recover) overrun occurred27.25 bitrate=11175.4kbits\/s    \nALSA lib pcm.c:7843:(snd_pcm_recover) underrun occurred7.76 bitrate=11168.7kbits\/s    \nALSA lib pcm.c:7843:(snd_pcm_recover) underrun occurred8.24 bitrate=11176.4kbits\/s    \nALSA lib pcm.c:7843:(snd_pcm_recover) overrun occurred55.48 bitrate=11243.8kbits\/s    \nALSA lib pcm.c:7843:(snd_pcm_recover) underrun occurred\nALSA lib pcm.c:7843:(snd_pcm_recover) underrun occurred\nframe=12871 fps= 30 q=-1.0 Lsize=  542369kB time=00:07:09.31 bitrate=10349.3kbits\/s    \nvideo:539762kB audio:2363kB subtitle:0 global headers:3kB muxing overhead 0.044476%\n[libx264 @ 0x21ae560] frame I:52    Avg QP:15.46  size:725888\n[libx264 @ 0x21ae560] frame P:12819 Avg QP:18.26  size: 40172\n[libx264 @ 0x21ae560] mb I  I16..4: 100.0%  0.0%  0.0%\n[libx264 @ 0x21ae560] mb P  I16..4:  2.6%  0.0%  0.0%  P16..4: 18.1%  0.0%  0.0%  0.0%  0.0%    skip:79.3%\n[libx264 @ 0x21ae560] coded y,uvDC,uvAC intra: 57.8% 49.8% 25.3% inter: 8.9% 8.7% 2.2%\n[libx264 @ 0x21ae560] i16 v,h,dc,p: 23% 29% 32% 16%\n[libx264 @ 0x21ae560] i8c dc,h,v,p: 45% 28% 18%  9%\n[libx264 @ 0x21ae560] kb\/s:10306.26\n<\/code><\/pre>\n<p>Please help me, I am really close to get this working!<\/p>\n<p><strong>UPDATE<\/strong>: The desktop audio is out of sync when skipping filter_complex and microphone also, bit in a smaller amount. Using <code>copy<\/code> instead of <code>libvorbis<\/code> does not change anything either.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have put together this script for recording the microphone, the desktop audio and the screen using ffmpeg: DATE=`which date` RESO=2560&#215;1440 FPS=30 PRESET=ultrafast DIRECTORY=$HOME\/Video\/ FILENAME=videocast`$DATE +%d%m%Y_%H.%M.%S`.mkv ffmpeg -y -vsync 1 \\ -f pulse -ac 2 -i alsa_output.pci-0000_00_1b.0.analog-stereo.monitor \\ -f pulse -ac 1 -ar 25000 -i alsa_input.usb-0d8c_C-Media_USB_Headphone_Set-00-Set.analog-mono \\ -filter_complex aresample=async=1,amix=duration=shortest,apad \\ -f x11grab -r $FPS -s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4519","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4519","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4519"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4519\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}