LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-25-2013, 12:23 PM   #1
dermetfan
LQ Newbie
 
Registered: Aug 2013
Posts: 22

Rep: Reputation: Disabled
Question FFMPEG video is out of sync


Hi everyone!
I'm trying to use ffmpeg to record my desktop. It's working with both audio and video, but no matter which options I use, the video is always out of sync. It's always too fast or too slow, sometimes even changing during one recording.

I used these commands among many others, but always the same result:
Code:
ffmpeg -f alsa -ac 2 -i pulse -f x11grab -r 30 -s 1920x1080 -i :0.0 -acodec pcm_s16le -vcodec libx264 -preset ultrafast -crf 0 -threads 0 -y screencast_out.avi
Code:
ffmpeg -f alsa -ac 2 -ar 44100 -i pulse -f x11grab -r 15 -s 1920x1080 -i :0.0 -vcodec h264 -r 15 -sameq out.avi
I noticed the lines
Code:
[alsa @ 0xb5dba0] Estimating duration from bitrate, this may be inaccurate
Code:
[x11grab @ 0xb41500] Estimating duration from bitrate, this may be inaccurate
maybe they have something to do with it. The audio is working fine so I guess only the x11grab warning is of interest.

Here's the whole output for the second command, in case you need it:
Code:
dermetfan@dermetfan ~ $ ffmpeg -f alsa -ac 2 -ar 44100 -i pulse -f x11grab -r 15 -s 1920x1080 -i :0.0 -vcodec h264 -r 15 -sameq out.avi
ffmpeg version 1.0 Copyright (c) 2000-2012 the FFmpeg developers
  built on Nov 17 2012 11:28:48 with gcc 4.7 (Debian 4.7.2-4)
  configuration: --prefix=/usr --extra-cflags='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security ' --extra-ldflags='-Wl,-z,relro' --cc='ccache cc' --enable-shared --enable-libmp3lame --enable-gpl --enable-nonfree --disable-decoder=libdirac --enable-libvorbis --enable-pthreads --enable-libfaac --enable-libxvid --enable-postproc --enable-x11grab --enable-libgsm --enable-libtheora --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libx264 --enable-libspeex --enable-nonfree --disable-stripping --enable-libschroedinger --disable-encoder=libschroedinger --enable-version3 --enable-libopenjpeg --enable-libvpx --enable-librtmp --enable-avfilter --enable-libfreetype --enable-libvo-aacenc --disable-decoder=amrnb --enable-libvo-amrwbenc --enable-libaacplus --libdir=/usr/lib/x86_64-linux-gnu --disable-vda --enable-libbluray --enable-libcdio --enable-gnutls --enable-frei0r --enable-openssl --enable-libass --enable-libopus --enable-fontconfig --enable-libdc1394 --disable-altive  libavutil      51. 73.101 / 51. 73.101
  libavcodec     54. 59.100 / 54. 59.100
  libavformat    54. 29.104 / 54. 29.104
  libavdevice    54.  2.101 / 54.  2.101
  libavfilter     3. 17.100 /  3. 17.100
  libswscale      2.  1.101 /  2.  1.101
  libswresample   0. 15.100 /  0. 15.100
  libpostproc    52.  0.100 / 52.  0.100
[alsa @ 0x19b7ba0] Estimating duration from bitrate, this may be inaccurate
Guessed Channel Layout for  Input Stream #0.0 : stereo
Input #0, alsa, from 'pulse':
  Duration: N/A, start: 1377451261.923471, bitrate: 1411 kb/s
    Stream #0:0: Audio: pcm_s16le, 44100 Hz, stereo, s16, 1411 kb/s
[x11grab @ 0x199b680] device: :0.0 -> display: :0.0 x: 0 y: 0 width: 1920 height: 1080
[x11grab @ 0x199b680] shared memory extension found
[x11grab @ 0x199b680] Estimating duration from bitrate, this may be inaccurate
Input #1, x11grab, from ':0.0':
  Duration: N/A, start: 1377451262.003424, bitrate: 995328 kb/s
    Stream #1:0: Video: rawvideo (BGR[0] / 0x524742), bgr0, 1920x1080, 995328 kb/s, 15 tbr, 1000k tbn, 15 tbc
File 'out.avi' already exists. Overwrite ? [y/N] y
using cpu capabilities: MMX2 SSE2Fast SSSE3 Cache64
[libx264 @ 0x19b94c0] profile High 4:4:4 Predictive, level 4.0, 4:4:4 8-bit
Output #0, avi, to 'out.avi':
  Metadata:
    ISFT            : Lavf54.29.104
    Stream #0:0: Video: h264 (H264 / 0x34363248), yuv444p, 1920x1080, q=-1--1, 15 tbn, 15 tbc
    Stream #0:1: Audio: mp3 (U[0][0][0] / 0x0055), 44100 Hz, stereo, s16
Stream mapping:
  Stream #1:0 -> #0:0 (rawvideo -> libx264)
  Stream #0:0 -> #0:1 (pcm_s16le -> libmp3lame)
Press [q] to stop, [?] for help
frame=   12 fps=0.0 q=0.0 size=      10kB time=00:00:00.00 bitrate=   0.0kbits/sframe=   23 fps= 22 q=0.0 size=      10kB time=00:00:00.80 bitrate=  97.9kbits/sframe=   31 fps= 20 q=0.0 size=      10kB time=00:00:02.58 bitrate=  30.7kbits/sframe=   32 fps=8.5 q=32766.0 Lsize=     501kB time=00:00:02.71 bitrate=1511.6kbits/s    
video:406kB audio:83kB subtitle:0 global headers:0kB muxing overhead 2.665515%
[libx264 @ 0x19b94c0] frame I:1     Avg QP:20.01  size:278616
[libx264 @ 0x19b94c0] frame P:8     Avg QP:23.55  size: 12353
[libx264 @ 0x19b94c0] frame B:23    Avg QP:20.86  size:  1653
[libx264 @ 0x19b94c0] consecutive B-frames:  3.1%  0.0%  9.4% 87.5%
[libx264 @ 0x19b94c0] mb I  I16..4: 61.5%  5.0% 33.5%
[libx264 @ 0x19b94c0] mb P  I16..4:  1.4%  0.1%  0.9%  P16..4:  3.2%  0.6%  0.4%  0.0%  0.0%    skip:93.3%
[libx264 @ 0x19b94c0] mb B  I16..4:  0.1%  0.1%  0.1%  B16..8:  3.2%  0.1%  0.1%  direct: 0.0%  skip:96.3%  L0:42.9% L1:56.5% BI: 0.6%
[libx264 @ 0x19b94c0] 8x8 transform intra:6.0% inter:40.6%
[libx264 @ 0x19b94c0] coded y,u,v intra: 26.4% 21.4% 19.6% inter: 0.4% 0.1% 0.1%
[libx264 @ 0x19b94c0] i16 v,h,dc,p: 55% 44%  1%  0%
[libx264 @ 0x19b94c0] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 32% 30% 29%  1%  2%  1%  2%  1%  3%
[libx264 @ 0x19b94c0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 31% 34% 15%  2%  3%  3%  5%  3%  4%
[libx264 @ 0x19b94c0] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0x19b94c0] ref P L0: 46.9%  9.3% 31.5% 12.3%
[libx264 @ 0x19b94c0] ref B L0: 67.1% 31.3%  1.6%
[libx264 @ 0x19b94c0] ref B L1: 95.1%  4.9%
[libx264 @ 0x19b94c0] kb/s:1557.98
Does anyone have an idea what might be causing this? Thanks!
 
Old 08-25-2013, 01:24 PM   #2
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,162
Blog Entries: 6

Rep: Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836
Quote:
ffmpeg -f alsa -ac 2 -i pulse -f x11grab -r 30 -s 1920x1080 -i :0.0 -acodec pcm_s16le -vcodec libx264 -preset ultrafast -crf 0 -threads 0 -y screencast_out.avi
Quote:
-r 30 -s 1920x1080
That's huge and 30 fps too. Lower the frames per second, lower the bitrate, capture only part of the desktop that you want seen at a time, don't encode sound and video on the fly,..you also don't say how good a machine you have.

With a 2 core AMD I just did this example with no problem.
Code:
ffmpeg -f alsa -i hw:0,0 -f x11grab -r 24 -s 1000x600 -i :0.0+10,40 -c:a libmp3lame -b:a 32k -c:v libx264 -b:v 500k output.avi
Depends on what you are grabbing. If you are grabbing a terminal while you type you can get away with 15fps, 500K video, if you are talking into a mic while instructing then you can get away with 22050Hz, 32k audio

Last edited by teckk; 08-25-2013 at 01:26 PM.
 
Old 08-25-2013, 03:14 PM   #3
dermetfan
LQ Newbie
 
Registered: Aug 2013
Posts: 22

Original Poster
Rep: Reputation: Disabled
I tried with 12 to 30 frames per second, makes no difference. Changed resolution but it also doesn't make any difference, I need to capture the full screen anyway.
My machine is fast enough, it runs normally while I'm recording.
 
Old 08-26-2013, 02:10 AM   #4
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
How is the video out of sync exactly ? You mean out of sync with the audio or does it stutter ?

Add this to the command:
Code:
-ar 48000 -async 48000

Last edited by H_TeXMeX_H; 08-26-2013 at 02:12 AM.
 
Old 08-26-2013, 05:45 AM   #5
dermetfan
LQ Newbie
 
Registered: Aug 2013
Posts: 22

Original Poster
Rep: Reputation: Disabled
It doesn't stutter, it plays smoothly but faster or slower than the audio. So yes, it's out of sync with the audio.
Unfortunately -ar 48000 -async 48000 didn't change anything :/
 
Old 08-26-2013, 05:54 AM   #6
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Try the various '-vsync' options listed here:
http://ffmpeg.org/ffmpeg.html
 
Old 08-26-2013, 07:30 AM   #7
dermetfan
LQ Newbie
 
Registered: Aug 2013
Posts: 22

Original Poster
Rep: Reputation: Disabled
Thanks for the help so far! The "drop" option makes it a bit better, but it slowly get's out of sync more over time. The first two minutes are ok, then it worsens.
 
Old 08-26-2013, 08:22 AM   #8
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Maybe try a different audio codec, like vorbis or aac.
 
Old 08-26-2013, 09:30 AM   #9
dermetfan
LQ Newbie
 
Registered: Aug 2013
Posts: 22

Original Poster
Rep: Reputation: Disabled
Tried different audio codecs with and without -vsync drop, looks like it's unstoppable on my machine..
 
Old 08-26-2013, 09:30 AM   #10
CrackerPunk
LQ Newbie
 
Registered: Aug 2013
Location: New Jersey
Distribution: FreeBSD 9.1 | Slackware 13
Posts: 17

Rep: Reputation: Disabled
I use this for my full screencast and audio and video are in sync

Code:
nice -n 0 ffmpeg -f x11grab  -r 29.970 -s 1280x1024 -i :0.0 -vcodec rawvideo -pix_fmt yuv420p -qscale 1  -threads 4 -acodec pcm_s16le -f alsa -i pulse -y mydesktop.avi
The video quality is not as great as 1080p but the video quality is good and clear enough to see everything including texts.
 
Old 08-26-2013, 10:09 AM   #11
dermetfan
LQ Newbie
 
Registered: Aug 2013
Posts: 22

Original Poster
Rep: Reputation: Disabled
Thanks It gave me Option pixel_format not found.. When I removed -pix_fmt yuv420p it worked, but got out of sync after three minutes :/
 
Old 08-26-2013, 10:15 AM   #12
CrackerPunk
LQ Newbie
 
Registered: Aug 2013
Location: New Jersey
Distribution: FreeBSD 9.1 | Slackware 13
Posts: 17

Rep: Reputation: Disabled
That is weird. You seem to have an unusual case with ffmpeg.

I don't have any other ideas. But if I do, I'll post.
 
Old 08-26-2013, 10:35 AM   #13
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,162
Blog Entries: 6

Rep: Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836
Yes, that worked fine. Makes a huge file that you can re-encode later. 9 seconds makes a 650MB file.
Code:
ffmpeg -f alsa -i hw:0,0 -c:a pcm_s16le -f x11grab -r 24 -s 1920x1080 -i :0.0 -c:v rawvideo -pix_fmt yuv420p output.avi
You could also scale it down
Code:
ffmpeg -f alsa -i hw:0,0 -c:a pcm_s16le -f x11grab -r 24 -s 1920x1080 -i :0.0 -c:v rawvideo -pix_fmt yuv420p -s 960x540 output.avi
I hadn't done this for a while so I set my machine up to see what I came up with. This is on an AMDx2.
I captured video as I narrated into the mic.
Code:
ffmpeg -f alsa -i hw:0,0 -f x11grab -r 10 -s 1920x1080 -i 0:0 -c:a libmp3lame -b:a 32k -c:v libx264 -b:v 500k -s 960x540 output.avi
I ran that for 3 min, the audio-video did not loose sync, and the file size is 8MB.
 
Old 08-26-2013, 11:41 AM   #14
CrackerPunk
LQ Newbie
 
Registered: Aug 2013
Location: New Jersey
Distribution: FreeBSD 9.1 | Slackware 13
Posts: 17

Rep: Reputation: Disabled
Perhaps it's the version of ffmpeg he has, just my guess.

The switches and parameters can have different outcomes based on the version of ffmpeg and how it was compiled by the distro.

Maybe he can try avconv. Avconv is compatible with ffmpeg switches.

@ dermetfan

You said you have a fast system, can you post the specs of your machine and the type of video card.

Last edited by CrackerPunk; 08-26-2013 at 12:00 PM.
 
Old 08-26-2013, 12:58 PM   #15
dermetfan
LQ Newbie
 
Registered: Aug 2013
Posts: 22

Original Poster
Rep: Reputation: Disabled
Now that's really weird. I modified teckk's last line a bit to work on my sytem (syntax errors) and it just records a part of the screen (even though I additionally set it to 1920x1080 everywhere), but is still in sync after 3:42 minutes (10.4 MB):
Code:
ffmpeg -f alsa -i pulse -f x11grab -i :0.0 -r 10 -s 1920x1080 -c:a libmp3lame -b:a 32k -c:v libx264 -b:v 500k -s 1920x1080 output.avi
I can't record video using avconv though, can I? Also I'd like to stream to an rtmp server later...

Well I said the system was fast enough... At least there were no noticeable lags or slowdowns while recording.
Video card: NVidia 9600 GT
CPU: Intel Core2 CPU 6600 @ 2.40GHz (that might be a bit rusty)
Memory: 4GB

Last edited by dermetfan; 08-26-2013 at 01:00 PM.
 
  


Reply

Tags
audio, ffmpeg, record, sync, video


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ffmpeg mixing audio/video - trying to end vid after video stream completes joejoe99 Linux - Software 2 06-21-2012 11:44 PM
ffmpeg and mencoder slice mp4 video audio out of sync lindylex Linux - Software 5 01-10-2010 03:26 AM
Audio completely out of sync with video playback. (FFmpeg/Xine/VLC) 12.2/2.6.27.7-smp camphor Slackware 9 02-17-2009 01:20 AM
Ubuntu video problems-video in player out of sync wvhillbilly Linux - Software 0 05-02-2007 01:08 PM
i want to sync my video using ffmpeg Jeon, Chang-Min Linux - Software 0 11-17-2005 03:04 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration