brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: ffmpeg -itsoffset shifts audio against the video without re-encoding

Ffmpeg
~/tips/tip-ffmpeg-itsoffset-fix-audio-delay

ffmpeg -i movie.mp4 -itsoffset 0.5 -i movie.mp4 -map 0:v -map 1:a -c copy fixed.mp4

Delays one stream against another, which fixes audio that is a fixed moment out of sync.

Audio that is a fraction of a second late (or early) is irritating, and it often happens with a file you cannot record again. ffmpeg can shift one stream against the other, and with stream copy it does not touch the picture.

-itsoffset is an input option: it adds an offset to the timestamps of the file that follows it. The docs say a positive offset means the corresponding streams are delayed by that duration. So to delay the audio, open the same file twice, put the offset on the second copy, and take the video from the first and the audio from the second with -map:

ffmpeg -i movie.mp4 -itsoffset 0.5 -i movie.mp4 -map 0:v -map 1:a -c copy fixed.mp4

Here the audio plays half a second later than before. If the sound is already late, the shift has to go to the video instead:

ffmpeg -itsoffset 0.5 -i movie.mp4 -i movie.mp4 -map 0:v -map 1:a -c copy fixed.mp4

Now the first input, which gives the video, is delayed, which has the same effect as moving the audio earlier. The manual points at the same -map 1:a idea: it selects all audio streams from the second input.

The offset uses the usual time duration syntax, so 0.5 and 00:00:00.5 both read as half a second. I’d start with small steps, 0.2 or 0.3, and play the result.

The caveats are practical. Because -itsoffset sits before one -i only, putting it in the wrong place shifts the wrong stream and makes things worse, so check the order. The sound can also drift instead of being a fixed gap. In that case a single offset fixes the start and leaves the end wrong, and this tip is not the answer. And when you pick -c copy, the container still has to accept the shifted timestamps, so a quick playback test at the start and the end is worth the minute.

Further reading: ffmpeg documentation: -itsoffset and -map

More Ffmpeg tips ffmpeg -stream_loop 2 -i in.mp4 -c copy out.mp4 ffmpeg -i in.mp4 \ ffmpeg -i video.mp4 -i video.en.srt -map 0 -map 1 -c copy -c:s mov_text -metadata:s:s:0 language=eng video-subs.mp4 ffmpeg -i film.mkv -map 0:v -map 0:a:0 -c:v copy -c:a aac -b:a 192k -ac 2 film-stereo.mkv ffmpeg -i talk.mp4 -vn -c:a copy talk.m4a All Ffmpeg tips (38) All topics

Link