133

In my .mp4 file the audio delay is -3840 ms. I synced it in KMplayer, and I don't want to use MKVGUI to make a .mkv file. I just need to delay the audio by 3840 ms, everything else should be left intact.
What would be the right command to accomplish this using ffmpeg?
I would appreciate your help.

Weaver
  • 2,254
  • 1
  • 12
  • 8
Alireza
  • 1,331

7 Answers7

204

If you need to delay video by 3.84 seconds, use a command like this:

ffmpeg -i "movie.mp4" -itsoffset 3.84 -i "movie.mp4" -map 1:v -map 0:a -c copy "movie-video-delayed.mp4"

If you need to delay audio by 3.84 seconds, use a command like this:

ffmpeg -i "movie.mp4" -itsoffset 3.84 -i "movie.mp4" -map 0:v -map 1:a -c copy "movie-audio-delayed.mp4"

Make sure, that your ffmpeg build is not too old, newer than 2012 will suffice.


Explanation

-itsoffset 3.84 -i "movie.mp4"

Offsets timestamps of all streams by 3.84 seconds in the input file that follows the option (movie.mp4). itsoffset is documented in the Main options section

-map 1:v -map 0:a

Takes video stream from the second (delayed) input and audio stream from the first input - both inputs may of course be the same file. map is documented in the Advanced options section

A more verbose explanation can be found here:
http://alien.slackbook.org/blog/fixing-audio-sync-with-ffmpeg/

Weaver
  • 2,254
  • 1
  • 12
  • 8
5

As stated in the currently top voted answer, you can use ffmpeg's -itsoffset. According to the ffmpeg wiki, if you do not want to offset all streams, all you have to do is to specify the input file twice. Once for the streams you want to keep as they are and once again for the streams you want to offset. Then you simply map the streams you want to the final output file. This way, there is no need to extract streams and then remux them together later.

For instance (copied from the wiki), if you want all audio streams to be offset by 5 seconds:

ffmpeg -i video.mp4 -itsoffset 5 -i video.mp4 -map 0:v -map 1:a out.mp4
  • -map 0:v will copy the video from the first input file (video.mp4)
  • -map 1:a will copy the audio from the second input file. Which happens to be video.mp4 too, but delayed 5 seconds by the use of -itsoffset right before the corresponding -i option.
edison23
  • 133
3

Make first silence audio:

ffmpeg -f lavfi -i anullsrc=channel_layout=5.1:sample_rate=48000 -t 3 silence_3_sec.mp3

Then concat files:

ffmpeg -i "concat:silence_3_sec.mp3|input.mp3" -acodec copy out.mp3
Giacomo1968
  • 58,727
1

I extracted audio with Audacity, then cut some silence (equal to delay) from end of video, and added to beginning of audio.

After doing any other adjustments to audio eg. normalization, I exported audio, and replaced audio in original via ffmpeg:

ffmpeg-i "in.mp4" -i "synced.m4a" -vcodec copy -acodec copy -map 0:0 -map 1:0 out.mp4
Giacomo1968
  • 58,727
Zimba
  • 1,291
1

Newer versions of ffmpeg require the -map options to be located just before the output options. For example:

ffmpeg.exe %InputOpts% -i %1 -itsoffset 1.00 -i %1  %CodecsOpts% %MapOpts% %OutputOpts%
Giacomo1968
  • 58,727
1

A method that works without ffmpeg having to read and process the file twice is to use a so-called complex filter that manipulates the presentation timestamps of either the video or audio tracks.

The benefit of this method is that it works for streams (which can be read from only once), and not just for files.

For example -filter_complex "asetpts=PTS-0.3/TB" subtracts 300ms from the presentation time stamps of audio packets.

Peregrino69
  • 5,004
0

Having had no luck with the earlier suggestions, I finally got it to work. My input was mp4 made of mpeg-4 avc with mp3 audio. The critical information points that were missing for me, is that -itsoffset with codec copy doesn't update the streams. It only applies timestamp offset information to the output stream. And this: "For MP4s, ffmpeg's muxer writes an edit list for delaying a stream. Players which honor it, will play as expected e.g. Potplayer does, WMP does not.". That came from https://trac.ffmpeg.org/ticket/1349

So I output to mkv instead.

I'd seen comments that -itsofset works only on video not audio, and that it doesn't work on avi, but also saw both those positions contradicted. YMMV.

To simplify I split the input into separate video and audio files to avoid the -map complexity. I expect this method will also work with map. My audio was late and needed to be brought forward.

ffmpeg -itsoffset 0.4 -i inputVid.mp4 -itsoffset 0 -i inputAudio.mp3 -vcodec copy -acodec copy fixed.mkv