How to Fade In and Out a Video Overlay in FFmpeg
This guide explains how to apply both a fade-in and a fade-out effect
to a video overlay using a single FFmpeg filtergraph. By chaining the
format, fade, and overlay filters
together in one command, you can smoothly transition an overlay video on
and off your main video background without needing complex multi-step
rendering.
The FFmpeg Command
To apply a fade-in and fade-out transition to an overlay, run the following command in your terminal:
ffmpeg -i main_video.mp4 -i overlay_video.mp4 -filter_complex \
"[1:v]format=yuva420p,fade=t=in:st=1:d=1:alpha=1,fade=t=out:st=8:d=1:alpha=1[over]; \
[0:v][over]overlay=x=10:y=10:shortest=1" \
-c:a copy output.mp4How the Filtergraph Works
The core of this process lies within the -filter_complex
argument, which processes the inputs using a single, continuous chain of
filters:
[1:v]: This selects the video stream of the second input (overlay_video.mp4).format=yuva420p: This converts the overlay video format to support an alpha (transparency) channel. This step is crucial; without an alpha channel, the fade filters will fade to a solid color (like black) instead of becoming transparent.fade=t=in:st=1:d=1:alpha=1: This applies the fade-in effect.t=in: Specifies a fade-in.st=1: The start time of the fade (at the 1-second mark).d=1: The duration of the fade (1 second long, meaning it becomes fully visible at 2 seconds).alpha=1: Tells FFmpeg to fade the transparency channel rather than fading to black.
fade=t=out:st=8:d=1:alpha=1: This applies the fade-out effect.t=out: Specifies a fade-out.st=8: The start time of the fade-out (at the 8-second mark).d=1: The duration of the fade-out (1 second long, meaning it becomes fully transparent at 9 seconds).alpha=1: Ensures the video fades to transparent.
[over]: This labels the processed overlay stream so it can be used in the next step.[0:v][over]overlay=x=10:y=10:shortest=1: This takes the main video ([0:v]) and places the processed overlay ([over]) on top of it.x=10:y=10: Positions the overlay 10 pixels from the left and 10 pixels from the top.shortest=1: Tells FFmpeg to stop encoding when the shortest input video ends.
Customizing the Timing
You can easily adjust the timing of the transitions by changing the
st (start time) and d (duration) values in the
fade filters. For example, if you want a 2-second fade-in that starts
immediately at 0 seconds, and a 1.5-second fade-out that starts at 15
seconds, you would use:
fade=t=in:st=0:d=2:alpha=1,fade=t=out:st=15:d=1.5:alpha=1