videoffmpeg

Overlay video onto another video at multiple points in time


I have a case where I want to have a background video and use a second video overlaying the first but at multiple points in time. The second video is shorter.

So for example the main video is a minute long and the second video is 10 seconds. I want to overlay the second video once at 15seconds then again at 35 seconds and then again at 55 seconds (terminating when the main video does).

I am able to get the second video to overlay successfully using overlay filter and between option but it only ever works for the first overlay. The second overlay seems to show the last or first frame of the video(??) but for the period I set in between. I am guessing it needs to be rewound and played somehow.

Eg:

ffmpeg  -i background.mp4 -i overlay.mov -filter_complex "[0][1]overlay=0:0:enable='between(t,0,2)'[out];[out][1]overlay=-200:200:enable='between(t,4,8)'[out]" -map "[out]" -t 00:00:10 -y output.mp4

I have also tried specifying the overlay as an input twice eg:

ffmpeg  -i background.mp4 -i overlay.mov -i overlay.mov -filter_complex "[0][1]overlay=0:0:enable='between(t,0,2)'[out];[out][2]overlay=-200:200:enable='between(t,4,8)'[out]" -map "[out]" -t 00:00:10 -y output.mp4

The same thing happens.

I'm sure the answer is simple and probably to do with how I'm using inputs but I'm not able to wrap my head around how to get the overlay video to play twice at different times on top of the original.

I have also tried using the setpts filter to rewind the overlay but again the results are exactly the same.

Eg:

ffmpeg  -i background.mp4 -i overlay.mov -filter_complex "[0][1]overlay=0:0:enable='between(t,0,2)'[out];[1]setpts=PTS-STARTPTS,[out]overlay=-200:200:enable='between(t,4,8)'[out]" -map "[out]" -t 00:00:10 -y output.mp4

There are a lot of similar questions involving overlaying multiple videos but I can't find anything about reusing the same video at multiple points in time.


Solution

  • I'm going to answer my own question, as often ends up happening.

    Posting the question helped me think about it objectively. I hope this answer helps people in the future.

    enable is a red herring, don't use it. It's really used for blur filters etc. What you need to do is just start your overlay from the point you want it to using setpts and end it when the video file ends by using eof_action.

    Example:

    ffmpeg  -i background.mp4 -i overlay.mov -filter_complex "[1]setpts=PTS+2/TB[ts],[0][ts]overlay=0:0:eof_action=pass[out];[1]setpts=PTS+4/TB[b],[out][b]overlay=-200:200:eof_action=pass[out]" -map "[out]" -t 00:00:10 -y output.mp4
    

    In this example PTS is the beginning of the output clip and +2 means start 2 seconds in, +4 is four seconds in. eof_action=pass allows the output to go to out rather than displaying the clips end frame for the rest of the output video.