Main menu:


Please consider a small donation:



Or you can donate bitcoin:


Thanks to TekLinks in Birmingham, AL, for providing colocation and bandwidth.

Page Rank


FOSS Force Best Blog--2013 Award

Recent posts

Recent comments

About this blog

I am Eric Hameleers, and this is where I think out loud.
More about me.


Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 388 other subscribers

My Favourites



August 2018
« Jul    

RSS Alien's Slackware packages

RSS Alien's unofficial KDE Slackware packages

RSS Alien's multilib packages

RSS Slackware64-current



Fixing audio sync with ffmpeg

The ffmpeg developers and their libav antipodes are engaged in a healthy battle. Ever since there was a fall-out and the ffmpeg developer community split in two (forking ffmpeg into “libav”), ffmpeg itself has seen many releases which tend to incorporate the good stuff from the other team as well as their own advancements.

Last in series is ffmpeg-0.9 for which I built Slackware packages (if you want to be able to create mp3 or aac sound, get the packages with MP3 and AAC encoding enabled instead.

The package will come in handy if you want to try what I am going to describe next.

Re-sync your movie’s audio.

You probably have seen the issue yourself too: for instance, I have a file “original.avi” which has an audio track (or “stream“) which is slightly out of sync with the video… just enough to annoy the hell out of me. I need to delay the audio by 0.2 seconds to make the movie playback in sync. Luckily, ffmpeg can fix this for you very easily.

Let’s analyze the available streams in the original video (remember, UNIX starts counting at zero):

$ ffmpeg -i original.avi

Input #0, avi, from ‘original.avi’:

Stream #0.0: Video: mpeg4, yuv420p, 672×272 [PAR 1:1 DAR 42:17], 23.98 fps, 23.98 tbr, 23.98 tbn, 23.98 tbc
Stream #0.1: Audio: mp3, 48000 Hz, stereo, s16, 128 kb/s

You see that ffmpeg reports a “stream #0.0” which is the first stream in the first input file (right now we have only one input file but that will change later on) – the video. The second stream, called “stream #0.1“, is the audio track.

What I need is to give ffmpeg the video and audio as separate inputs, instruct it to delay our audio and re-assemble the two streams into one resultant movie file. The parameters which define two inputs where the second input will be delayed for N seconds, goes like this:

$ ffmpeg -i inputfile1 -itsoffset N -i inputfile2

However, we do not have a separate audio and video tracks, we just have the single original AVI file. Luckily, the “inputfile1” and “inputfile2” can be the same file! We just need to find a way to tell ffmpeg what stream to use from which input. Look at how ffmpeg reports the content of input files if you list the same file twice:

$ ffmpeg -i original.avi -i original.avi

Input #0, avi, from ‘original.avi’:

Stream #0.0: Video: mpeg4, yuv420p, 672×272 [PAR 1:1 DAR 42:17], 23.98 fps, 23.98 tbr, 23.98 tbn, 23.98 tbc
Stream #0.1: Audio: mp3, 48000 Hz, stereo, s16, 128 kb/s

Input #1, avi, from ‘original.avi’:

Stream #1.0: Video: mpeg4, yuv420p, 672×272 [PAR 1:1 DAR 42:17], 23.98 fps, 23.98 tbr, 23.98 tbn, 23.98 tbc
Stream #1.1: Audio: mp3, 48000 Hz, stereo, s16, 128 kb/s

You see that the different streams in multiple input files are all numbered uniquely. We will need this defining quality. I colored the numbers with red & purple – these colors will show up in my example commands below.

Our remaining issue is that ffmpeg must be told that it has to use only the video stream of the first inputfile, and only the audio stream of the second inputfile. Ffmpeg will then have to do its magic and finally re-assemble the two streams into a resulting movie file. That resulting AVI file also expects video as the first stream, and audio as the second stream, just like our original AVI is laid out. Movie players will get confused otherwise.

Ffmpeg has the “map” parameter to specify this. I have looked long and hard at this parameter and its use… it is not easy for me to follow the logic. A bit like the git version control system, which does not fit into my brain conceptually, either. But perhaps I can finally explain it properly, to myself as well as to you, the reader.

Actually, we need two “map” parameters, one to map the input to the output video and another to map the input to the output audio. Map parameters are specified in the order the streams are going to be added to the output file. Remember, we want to delay the audio, so inherently the audio track must be taken from the second inputfile.

In the example below, the first “-map 0:0” parameter specifies how to create the first stream in the output. We need the first stream in the output to be video. The “0:0” value means “first_inputfile:first_stream“.

The second “-map 1:1” parameter specifies where ffmpeg should find the audio (which is going to be the second stream in the output). The value “1:1” specifies “second_inputfile:seccond_stream.

$ ffmpeg -i original.avi -itsoffset 0.2 -i original.avi -map 0:0 -map 1:1

There is one more thing (even though it looks like ffmpeg is smart enough to do this without explicitly telling so). I do not want any re-encoding of the audio or video to happen, so I instruct ffmpeg to “copy” the audio and video stream without intermediate decoding and re-encoding. The “‘-acodec copy” and “-vcodec copy” parameters take care of this.

We now have the information to write a ffmpeg commandline which takes audio and video streams from the same file and re-assembles the movie with the audio stream delayed by 0.2 seconds. The resulting synchronized movie is called “synced.avi” and the conversion takes seconds, rather than minutes:

$ ffmpeg -i original.avi -itsoffset 0.2 -i original.avi -map 0:0 -map 1:1  -acodec copy -vcodec copy synced.avi

Cheers, Eric


Comment from gegechris99
Posted: December 14, 2011 at 14:24

Thanks Eric for this tutorial.

Your explanation of the mapping logic is quite clear to me (the reader) 🙂

Pingback from Links 16/12/2011: Red Hat Upgraded, Android Everywhere | Techrights
Posted: December 16, 2011 at 11:27

[…] Fixing audio sync with ffmpeg […]

Comment from Lawrence McDonald
Posted: December 17, 2011 at 13:42

Thank you Eric
Merry Christmas and happy new year !

Comment from pcelka
Posted: December 19, 2011 at 23:06

Thanks for this nice tutorial!

Joyeux Noël et bonne année! (From France)

Pingback from Herk's Lab » Video Capture for Linux – Take One
Posted: October 29, 2012 at 02:30

[…] here for the explanation of syncing your audio with […]

Comment from Clm
Posted: November 28, 2012 at 20:44

Exactly what I was looking for…
Thanks !

Comment from Lord Stranger
Posted: January 30, 2013 at 17:10

Thanks – worked a treat!

Comment from kaaposc
Posted: April 19, 2013 at 10:54

Thanks for this post! I have one de-synced video that I first tried to sync with VLC’s sync tool and pin pointed delay of audio at 1.750 seconds. When put this value in ffmpeg, audio was now lagging behind video. Finally ffmpeg gave me good results with -itsoffset 1.0.. I wonder why such difference?

Pingback from avi file, audio out of sync
Posted: July 4, 2013 at 00:59

[…] a permanent fix. Last night I was trawling around for solutions. I came across this using ffmpeg:…c-with-ffmpeg/ The only problem with this is accurately defining the time at which audio lags. Now I need to dig […]

Comment from gmv
Posted: April 28, 2014 at 06:16

I will record the video as a series of images and sound from the line-in then get in front of the camera and bang two sticks together like the movie people do then figure the delay by seeing the impact frame number and how far it is from the start. Then I know how to align the two in a final video. Just remember the audio travels about 1130FPS to the microphone. Seems to work OK. BUT you-tube will still report audio might not be in sync ????

Comment from cr
Posted: September 13, 2014 at 12:50

I have a file DL’d from Youtube, where the sync is OK for the first half then drifts to 250m out by the end. I tried making several files with different audio offsets with ffmpeg as above, then cutting out chunks with different offsets with MP4Box -split-chunk, then putting them together again into one corrected file. The chunks played OK (correct sync) in VLC and mplayer, but not in Totem (gstreamer based).
MP4Box -cat reassembled the chunks but the sound was lost from all chunks after the first. Tried using Openshot instead to reassemble the file but the offsets were lost i.e. the out-of-sync returned.

Just have to keep trying I guess.

Comment from Fabio
Posted: June 4, 2015 at 13:10

Hi, I’m in current and use your ffmpeg package (2.4.3) and your kde 4.14.3, but I’ve a big problem with kdenlive: I’ve built v0.98 from sources but can’t open .avi or .mpeg files because he request all libraries in and i’ve all in
Then i’ve done symlink with 55 instead of 56 but when run the program it give me this error :

mlt_repository_init: failed to dlopen /usr/lib/mlt/
(/usr/lib/ version `LIBAVDEVICE_55′ not found (required by /usr/lib/mlt/

I’ve tried to build mlt but it’s the same.
Have you any suggestion to me?

Thanks in advance and excuse me if this is’nt the correct place where to put this question.

Comment from alienbob
Posted: June 4, 2015 at 22:15

Hi Fabio

If you are running Slackware-current then you should switch to the KDE 4.14.3 that is now part of Slackware-current. I removed all those packages from my ‘ktown’ repository except for phonon-vlc.

Your error about “” means that you have compiled your sources in the presence of an older ffmpeg and then upgraded ffmpeg. Recompiling again, is the only solution probably.

Comment from Fabio
Posted: June 4, 2015 at 22:41

Thankyou very much for the reply. Ok I’m going to trying this way. But I’ve anoter little question: if i switch my system to the new KDE 5 (with your packages of course), it’s possible to compile kdenlive for this platform?

Comment from alienbob
Posted: June 5, 2015 at 10:21

Hi Fabio. I do not see why that would not work. Just try.

Pingback from A deep dive into Periscope – and how to save the stream from the website | Marc Durdin's Blog
Posted: June 25, 2015 at 13:08

[…] audio stream and pauses it for two seconds, before recombining it with the video stream. This blog post explains the details of this […]

Pingback from Capturar la pantalla en Linux | monstruosoft
Posted: February 23, 2016 at 22:53

[…] hay que usar el FFmpeg o Libav para aplicar el retraso; la forma de hacerlo la obtuve de esta página que explica de una forma muy clara la función del argumento -map. Básicamente, para aplicar un […]

Comment from john morrissey
Posted: March 14, 2016 at 23:02

need help with ffmpeg 3.0.

I have a sync issue with .mxf
RAN: ffmpeg -i elrs0322.mxf
stream 0:0
stream 0:2
RAN: ffmpeg -i elrs0311.mxf -itsoffset 1 -i input2file2
–permission error on input2file2
RA: ffmpeg -i elrs0311.mxf -i elrs0311.mxf
no input file.
could you email me?

RAN: ffmpeg -i elrs0311.mxf -itsoffset 1 -i elrs0311.mxf
permission error folder


Comment from alienbob
Posted: March 15, 2016 at 00:19

Are you for real? Am I your slave? Private free assistance? What do you think happens now? Silence.

Comment from Daniel Matthews
Posted: June 3, 2016 at 02:49

Much thanks! I used “-cv copy” instead of “-acodec copy -vcodec copy”, as the latter was spitting out an error. Must be my version of FFMpeg.

Comment from alienbob
Posted: June 3, 2016 at 12:32

You know, perhaps that “-vcodec” option has been deprecated in your ffmpeg binary.
If you look at the manual page at ; then you see that “-vcodec copy” is an alias to “-codec:v copy”. And that last one can be shortened to “-c:v copy”.
The “-codec:” syntax would seem to be the preferable option anyway since it uses the same stream syntax that other options like “stream mapping” also use.

Comment from magg
Posted: November 22, 2016 at 05:27


When ever you have time, can you please add to ffmpeg the flag to support pulse? right now looks like it is build without it:

$ ffmpeg –version 2>&1 | grep -i pulse
$ ffmpeg -video_size 1024×768 -framerate 25 -f x11grab -i :0.0+100,200 -f pulse -ac 2 -i default output.mkv
Unknown input format: ‘pulse’

not that i like pulse, but trying to record something that use pulse is hard 🙂

thanks for all your packages

Comment from alienbob
Posted: November 22, 2016 at 23:41

magg, so apparently “-f pulse” generates that error because by default, when compiling ffmpeg it will not use libpulse and adding “–enable-libpulse” is required.

I will take care of that with the next series of ffmpeg updates. Thanks for showing this.

Comment from Brian
Posted: January 25, 2017 at 02:52

I appreciate still seeing help articles like this long since they were authored. Kudos for keeping this online. Most of the search results I found referenced offsetting a full second or more. This is the first result I found with an example of fractional delay. Thanks again.

Comment from SK
Posted: February 5, 2017 at 09:59

Real Magic this code.

On one desynced movie the value of 0.2 worked swell, on another the value of 1.0 .

Thank you Alien Pastures.

Pingback from HTML5: Video convertidors « A new IS hope
Posted: March 31, 2017 at 07:17

[…] FFmpeg – Correcting audio […]

Comment from Anthony The Koala
Posted: October 26, 2017 at 20:12

Dear Eric,
Thank you for this post. I have an mp4 with video, and a wav audio file. I know that I have a 433millisecond delay between the video and audio. I tried this with VLC, but it did not save the synched audio and video.
Then I tried with ffmpeg with the general structure as described in your ‘tutorial’.
ffmpeg -i inputfile1 -itsoffset N -i inputfile2 -map 0:0 -map 1:1 -acodec copy -vcodec copy synced.mp4

That is with the video in the mp4 and the audio in the mp3 I got the following

ffmpeg -i mymovie.mp4 -itsoffset 0.433 -i mysound.wav -map 0:0 -map 1:1 -acodec copy -vcodec copy synced.mp4

Note: 0.433 is 433 milliseconds.

Unrecognized option ‘itoffset’.
Error splitting the argument list: Option not found

Any idea?
Thank you,
Anthony of Sydney Australia

Comment from Anthony The Koala
Posted: October 26, 2017 at 20:56

Dear Eric,
I tried to do as much as I can to figure out the problem:

First I tried using an mp3 version of the file mysound.mp3 (144 kbs, 44.1kHz):

ffmpeg -i mymovie.mp4 -itsoffset 0.433 -i mysound.mp3 -map 0:0 -map 1:1 -acodec copy -vcodec copy synced.mp4

I get the error:
Stream map ‘1:1’ matches no streams.
To ignore this, add a trailing ‘?’ to the map.

Second, I added a trailing ‘?’ to the map.

ffmpeg -i mymovie.mp4 -itsoffset 0.433 -i mysound.mp3 -map 0:0 -map 1:1? -acodec copy -vcodec copy synced.mp4

NOW – when I playback synced.mp4, there’s video BUT no sound.

Thank you in advance,
Anthony of Sydney Australia

Comment from alienbob
Posted: October 26, 2017 at 21:14

No idea Anthony unless you misspelled ‘itoffset’ also in the actual command. It is ‘itsoffset’.

Comment from alienbob
Posted: October 26, 2017 at 21:17

Anthony check what I wrote about the map parameter and also run “ffmpeg -i mymovie.mp4 -i mysound.mp3” to see why the “-map 1:1” is wrong for a file which contains only one stream.

Comment from Anthony The Koala
Posted: October 26, 2017 at 22:06

Dear Eric,
Thank you very much for averting me to the meaning of map fileno:streamno.
It works now with the following structure
I replace map 1:1 with map 1:0, that is the audio file = second file with the one stream.

ffmpeg -i mymovie.mp4 -itsoffset 0.433 -i mysound.mp3 -map 0:0 -map 1:0 -acodec copy -vcodec copy synced.mp4

The syncing is perfect.
Many thanks

Anthony from Sydney Australia

Comment from Anthony The Koala
Posted: October 27, 2017 at 11:12

Dear Eric,
I don’t know if this question is out-of-the-scope of the tutorial. The abovementioned file synced.mp4 is perfectly synced when I play on my PC.

However, when I play the same file synced.mp4 from the TV’s USB or playing the file from a burnt DVD, the audio is out-of-sync with the video.

Do I have to convert the file synced.mp4 to something else?

Thank you,
Anthony of Sydney Australia

Comment from alienbob
Posted: October 27, 2017 at 15:15

Anthony, playback of audio/video means that the playback device needs to decode both audio and video from their compressed formats to the uncompressed bitstream. That requires processing power. Some compression formats require more CPU power than others. In you case it looks like the television (which will certainly have a lower-spec CPU) has issues keeping up with the decoding.
Many of the older PC’s can’t cope all that well with modern MP4 video but can playback old-fashioned AVI files with more ease. Try re-encoding that MP4 to a AVI file.
Try something like:

ffmpeg -i myvideo.mp4 -vcodec mpeg4 -acodec libmp3lame -qscale:v 2 -qscale:a 5 myvideo.avi

The ‘qscale’ parameters control the quality (and thus the bitrate) of the resulting AVI file. Try setting the “-qscale:v” value to 3 or 4 if your television is still having difficulty with the setting of “2” – lower value means higher quality video.

Write a comment