Create a screencast with ffmpeg: how to keep video+audio in sync?

0

I created some screencasts with ffmpeg. The PC I use has capable hardware (Intel Core i7-4930K Six Core 3.40GHz 12MB Cache, 32 GB RAM), but unfortunately it runs Windows 7. Because I want to do my screencast on Linux I installed Kubuntu in VMware. I assigned 4 CPU cores and 4 GB RAM to the VM.

I record my screencast with the following command:

ffmpeg -f alsa -ac 1 -i pulse -f x11grab -r 30 -s 1920x1080 -i :0.0 -acodec mp3 -vcodec libx264 -preset ultrafast -crf 0 -threads 0 output.mp4

However the video runs way faster than the audio. After a couple of minutes the audio (recorded by microphone) lags several seconds behind the video. I tried to record the audio on the Windows Host with audacity in parallel and the audio from audacity and the audio captured by ffmpeg in the VM seem to be in sync. Just the video runs to fast.

What is the reason for this? Could it have to do VMware? Are there any settings that I can tweak? Are there ffmpeg options I could use for syncing? E.g. if I could force ffmpeg to drop or duplicate frames if audio/video is not in sync that would be totally OK for me, but as far as I understand the manual for ffmpeg's -vsync option, this is already the default.

I compiled ffmpeg myself following the Ubuntu Compilation Guide and I got the idea to use ffmpeg from here.

EDIT: ffmpeg output as requested:

$ /home/yankee/bin/ffmpeg -f alsa -ac 1 -i pulse -f x11grab -r 30 -s 1920x1080 -i :0.0 -acodec mp3 -vcodec libx264 -preset ultrafast -crf 0 -threads 0 05-visitor.mp4
ffmpeg version 2.1.git Copyright (c) 2000-2014 the FFmpeg developers
  built on Feb 24 2014 08:38:08 with gcc 4.8 (Ubuntu/Linaro 4.8.1-10ubuntu9)
  configuration: --prefix=/home/yankee/ffmpeg_build --extra-cflags=-I/home/yankee/ffmpeg_build/include --extra-ldflags=-L/home/yankee/ffmpeg_build/lib --bindir=/home/yankee/bin --extra-libs=-ldl --enable-gpl --enable-libass --enable-libfdk-aac --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-nonfree --enable-x11grab
  libavutil      52. 65.100 / 52. 65.100
  libavcodec     55. 52.102 / 55. 52.102
  libavformat    55. 33.100 / 55. 33.100
  libavdevice    55. 10.100 / 55. 10.100
  libavfilter     4.  1.103 /  4.  1.103
  libswscale      2.  5.101 /  2.  5.101
  libswresample   0. 17.104 /  0. 17.104
  libpostproc    52.  3.100 / 52.  3.100
Guessed Channel Layout for  Input Stream #0.0 : mono
Input #0, alsa, from 'pulse':
  Duration: N/A, start: 1394106509.365291, bitrate: 768 kb/s
    Stream #0:0: Audio: pcm_s16le, 48000 Hz, mono, s16, 768 kb/s
[x11grab @ 0x2551e40] device: :0.0 -> display: :0.0 x: 0 y: 0 width: 1920 height: 1080
[x11grab @ 0x2551e40] shared memory extension found
Input #1, x11grab, from ':0.0':
  Duration: N/A, start: 1394106509.415547, bitrate: 1990656 kb/s
    Stream #1:0: Video: rawvideo (BGR[0] / 0x524742), bgr0, 1920x1080, 1990656 kb/s, 30 tbr, 1000k tbn, 30 tbc
No pixel format specified, yuv444p for H.264 encoding chosen.
Use -pix_fmt yuv420p for compatibility with outdated media players.                                                                                                                                                                                                            
[libx264 @ 0x256db60] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
[libx264 @ 0x256db60] profile High 4:4:4 Predictive, level 4.0, 4:4:4 8-bit
[libx264 @ 0x256db60] 64 - core 142 - H.264/MPEG-4 AVC codec - Copyleft 2003-2014 - http://www.videolan.org/x264.html - options: cabac=0 ref=1 deblock=0:0:0 analyse=0:0 me=dia subme=0 psy=0 mixed_ref=0 me_range=16 chroma_me=1 trellis=0 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=0 chroma_qp_offset=0 threads=6 lookahead_threads=1 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=cqp mbtree=0 qp=0
Output #0, mp4, to '05-visitor.mp4':
  Metadata:
    encoder         : Lavf55.33.100
    Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv444p, 1920x1080, q=-1--1, 15360 tbn, 30 tbc
    Stream #0:1: Audio: mp3 (libmp3lame) (i[0][0][0] / 0x0069), 48000 Hz, mono, s16p
Stream mapping:
  Stream #1:0 -> #0:0 (rawvideo -> libx264)
  Stream #0:0 -> #0:1 (pcm_s16le -> libmp3lame)
Press [q] to stop, [?] for help
frame=19011 fps= 30 q=-1.0 Lsize=  186418kB time=00:10:34.84 bitrate=2405.5kbits/s    
video:180861kB audio:4960kB subtitle:0 data:0 global headers:0kB muxing overhead 0.321432%
[libx264 @ 0x256db60] frame I:77    Avg QP: 0.00  size:451985
[libx264 @ 0x256db60] frame P:18934 Avg QP: 0.00  size:  7943
[libx264 @ 0x256db60] mb I  I16..4: 100.0%  0.0%  0.0%
[libx264 @ 0x256db60] mb P  I16..4: 59.7%  0.0%  0.0%  P16..4:  0.0%  0.0%  0.0%  0.0%  0.0%    skip:40.3%
[libx264 @ 0x256db60] coded y,u,v intra: 0.2% 0.2% 0.2% inter: 0.0% 0.0% 0.0%
[libx264 @ 0x256db60] i16 v,h,dc,p: 100%  0%  0%  0%
[libx264 @ 0x256db60] kb/s:2338.03

I played around a little more and I noticed that if the output file already exists ffmpeg asks me if I would like to override the file. In this case the audio stream already contains things I said while the overwrite file question exists. The video however does not start before I confirmed file overwrite and thus the audio naturally lags behind the video quite a lot. But that is easy to deal with. Just don't overwrite existing files.

I don't really care about the format I end up with as long as the video has some lossless format and I can edit it with Adobe Premiere (it does not support mkv).

EDIT2: I got a little closer to the problem. When looking very closely at the resulting video stream, it is sometimes noticeable that a couple of frames are missing. May because I opened a window in just this moment which causes video compression to consume more CPU (because many pixels changed) or the like. Maybe there are some buffers I can assign to ffmpeg to speed up processing in such moments? After all my machine has 32GB RAM, it should be good for something... Or are there any other lossless codecs I could use or...? Next time i'll also try ro set a higher CPU priority for ffmpeg.

yankee

Posted 2014-03-06T16:45:13.427

Reputation: 595

This question looks to be a similar issue and might be of interest: http://superuser.com/questions/728795/new-ffmpeg-swscaler-0xa314080-warning-data-is-not-aligned-this-can-lead/799987. The answer there pointed to a size rounding problem.

– neontapir – 2016-05-25T18:11:56.820

Please include the complete ffmpeg console output. – llogan – 2014-03-06T18:41:37.720

@LordNeckbeard: done – yankee – 2014-03-07T07:20:57.670

Answers

0

This works for me in 2019. I tried with different encoders but when using ones that aren't native to ffmpeg, the audio and video get out of sync. Specifically the video lags behind. mpeg4 and aac are native.

ffmpeg -f alsa -ac 1 -i pulse -f x11grab -r 30 -s 1920x1080  -i :0.0 -acodec aac -vcodec mpeg4 -preset medium -qscale:v 5 rec.mkv

miyalys

Posted 2014-03-06T16:45:13.427

Reputation: 1 757

0

I get a "[swscaler @ 0xa314080] Warning: data is not aligned! This can lead to a speedloss" that seems to be the issue. However, I don't have a resolution yet.

Syborgia Alphas

Posted 2014-03-06T16:45:13.427

Reputation: 81

1Ensure your desired output resolution is exactly divisible by 16. – Cees Timmerman – 2014-08-21T07:34:09.687

Working on FFmpeg is a work in progress. It's now part of a larger project Kubuntu-Remix; https://launchpad.net/kubuntu-remix

– Syborgia Alphas – 2014-08-22T18:16:37.233

1Welcome to Superuser! Your "answer" is more of a "me too!" and although it may be helpful, it should be a comment. – G Koe – 2014-03-13T21:25:25.027

I'm still looking for a resolution, his code did not include the "swscaler" warning. This is not a, "me too" this is trying establish if his issue the same as mine, or is mine something different. – Syborgia Alphas – 2014-03-13T22:43:39.197

my ffmpeg build; ffmpeg version 2.2.git Copyright (c) 2000-2014 the FFmpeg developers built on Mar 13 2014 16:08:45 with gcc 4.8 (Ubuntu/Linaro 4.8.1-10ubuntu9) – Syborgia Alphas – 2014-03-13T22:45:15.453

Great, my apologies. Post yours as a new question, and reference this questions! – G Koe – 2014-03-13T22:45:17.050

ok - thanks. This is what I was waiting for - instead of just posting the question. – Syborgia Alphas – 2014-03-13T22:54:26.730