How can I specify how long I want an overlay on a video to last with FFMPEG?

4

4

I'm making a video for YouTube, and I've already edited and touched it up in a full video editing program. The last thing I want to do is add an overlay banner with subscribe and playlist buttons which I'll turn into annotations. I want this to appear for the first 5 seconds of the video. Unfortunately my full video editing program can't due that, so I'd like to use FFMPEG to achieve this effect.

I've seen a few tutorials about this (this one in particular seemed pretty good), but none of them mention how to specify the duration of the overlay.

Does anyone have any ideas on how I could achieve this? Thanks in advance! :)

Miguel

Posted 2012-11-21T18:49:07.527

Reputation: 131

2One idea is to chain a fade filter to an overlay, but I haven't tried it so I don't know if it will work as expected. – llogan – 2012-11-21T20:38:45.850

@LordNeckbeard I'm willing to try it. How exactly would I do this? :) – Miguel – 2012-11-21T21:41:01.633

I'm not sure, and I am unable to try it myself now. Asking at the ffmpeg-user mailing list may provide an answer. Make sure you're using a recent ffmpeg build. – llogan – 2012-11-22T04:20:41.117

Answers

5

Fade and Overlay

You can chain a fade and overlay filter together like so:

ffmpeg -i input.mp4 -f image2 -loop 1 -r 24 -i banner.png -filter_complex \
'[1:v]fade=out:96:24:alpha=1[wm];[0:v][wm]overlay=10:10[outv]' \
-map [outv] -map 0:a -c:a copy -c:v libx264 -preset veryfast output.mp4

-f image2 -loop 1 -r 24 -i banner.png turns banner.png into a 24fps video, looping to infinity.

[1:v]fade=out:96:24:alpha=1[wm] makes the watermark begin fading out at the 96th frame (4 seconds @ 24fps), and fade out over 24 frames (1 second @ 24fps); and it will fade out the alpha channel, meaning the watermark will turn transparent rather than fading to a black block. This creates an output labelled [wm]. Note that the lowest you can go for the length of the fade is 1 frame.

[0:v][wm]overlay=0:0[outv] overlays the banner on input.mp4, creating an output labelled [outv].

-map [outv] -map 0:a tells ffmpeg to use [outv] and the audio from input.mp4 in the output. The rest of the options are encoding options.

You can make the banner fade in and out by chaining two fade filters. The following will make the banner fade in starting at frame 96, with a duration of 24 frames; and then fade out starting at frame 216 (9 seconds @ 24fps), for a duration of 24 frames:

ffmpeg -i input.mp4 -f image2 -loop 1 -r 24 -i banner.png -filter_complex \
'[1:v]fade=in:96:24:alpha=1,fade=out:216:24:alpha=1[wm];[0:v][wm]overlay=10:10[outv]' \
-map [outv] -map 0:a -c:a copy -c:v libx264 -preset veryfast output.mp4

Stream Seeking with -ss and -t

This achieves a similar end-result, but requires the creation of larger intermediate files. However, it only transcodes the first five-second section, and so will save on processing power and possibly be faster.

This involves cutting the video into two bits using -t and -ss, putting the watermark over the first, five-second video, and then concatenating the two videos together.

ffmpeg -i input.mp4 -i watermark.png \
-filter_complex '[0:v][1:v]overlay[outv]' -map [outv] -map 0:a \
-t 5 -c:a copy -c:v libx264 -crf 22 -preset veryfast start.mp4

ffmpeg -i input.mp4 -ss 5 -c copy end.mp4

Then concatenate the two videos using the concat demuxer: first create a file called inputs.txt, containing the following lines:

file 'start.mp4'
file 'end.mp4'

Then,

ffmpeg -f concat -i inputs.txt -c copy output.mp4

evilsoup

Posted 2012-11-21T18:49:07.527

Reputation: 10 085