I'm trying to replace the first few seconds of a video with a different one, while retaining the audio from the first.

This didn't work for me (the last frame of the overlay is retained after it finishes) https://stackoverflow.com/questions/35269387/ffmpeg-overlay-one-video-onto-another-video

I also tried doing it in multiple steps:

INTRO_TIME=$(/usr/bin/ffmpeg -i intro.mkv 2>&1 | grep Duration | cut -d ' ' -f 4 | sed s/,//  )
rm temp1.mkv temp2.mkv
/usr/bin/ffmpeg -i $1 -i intro.mkv -filter_complex "overlay" -shortest -c:a flac -c:v libx264 -crf 18 -t ${INTRO_TIME} temp1.mkv
/usr/bin/ffmpeg -accurate_seek -ss ${INTRO_TIME} -i $1 -c:a flac -c:v libx264 temp2.mkv 
/usr/bin/ffmpeg -f concat -safe 0 -i <(echo file ${DIR}/temp1.mkv; echo file ${DIR}/temp2.mkv) -c copy $2

But this appears to be corrupted after the intro ends (the audio plays but the video doesn't. N.B. both temp files look ok)

Ideally I'd like the last frame of the intro to be frozen and then faded out to reveal the original video underneath.

Also, the original video(s) can be quite long, so if there's a way to do it without re-encoding the bulk of the original video, even better. (I realise re-encoding may be necessary for the duration of the intro, and maybe a few seconds beyond that)

  • You need to use overlay=eof_action=pass to keep the overlay input from freezing on top after its runtime, and drop the shortest=1. Re-encoding just the affected portion of the main video is possible but it is tedious to get it right.
    – Gyan
    Commented Oct 14, 2017 at 17:36
  • sorry, can you put that into a full command? I tried a few things but can't figure out how to applyit
    – DJL
    Commented Oct 14, 2017 at 18:15

1 Answer 1


A command of the form below will do it:

ffmpeg -i main.mkv -i intro.mkv
   -filter_complex "overlay=eof_action=pass[v]"
   -map "[v]" -map 0:a -c:v libx264 -crf 18 -c:a copy out.mkv

If your ffmpeg build is recent, you'll need to add another option:


  • That works great, thanks. Now I need to figure out the fadeout...
    – DJL
    Commented Oct 14, 2017 at 19:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.