sábado, 4 de junho de 2011

How to fix out of sync between audio and video using ffmpeg

Quick fix for A/V out-of-sync when using ffmpeg

ffmpeg now has the option called: '-async <number>' which syncronizes audio and video at the beginnig of the stream when using '-async 1'  in the command line of ffmpeg.

    ffmpeg -i sample.mp4 -async 1 ......

No need to use the -itsoffset with separate A/V inputs, no need to calculate the offset. Specially handsome while converting video streams like dvb-recordings.

The longer way:

If this first fix doesn't work for your stream, them use the -itsoffset command the option.

Let say that the video we want to convert is: 'sample.ogg' and that the converted (final output) is called: 'video.flv'

Considere that we want ffmpeg to take the first input file as the audio, and the second as the video, since we need to delay the video and we've applied the delay to the second input file.

The '-map' switches should be listed in the order you want the streams to appear in the output file.

Please note this syntax:  -map <file(0=first;1=second)>:<stream(0=first;1=second)>

Since I know that a flash movie has two streams: video first and then audio second, (which you can see using "ffmpeg -i movie.flv"), the first 'map' switch specifies the video, and the second specifies the audio.  The command line will be:

      ffmpeg -i sample.ogg -itsoffset <delay in seconds> -i sample.ogg -map 1:0 -map 0:1 -ar 22050 video.flv

Here "-map 1:0" tells ffmpeg to use the second input file (1) and take its first stream (0) as the video source.

Since that's the input that follows the "-itsoffset" switch, that's the stream that will be delayed.

Then "-map 0:1" tells it to use the second stream from the first input as the audio. Since there's no offset applied to the first input file, it will be processed normally.

Finally we change the audio sample rate (-ar) to something suitable for a flash movie and output will be a file called "video.flv". ffmpeg will see the ".flv" and apply the proper codecs for the audio and video to make a ".flv" file.)

Have fun!

Nenhum comentário:

Postar um comentário