Fade In and Out an Image Overlay in FFmpeg
This article explains how to apply fade-in and fade-out transition effects to an image overlaying a video using FFmpeg. By utilizing FFmpeg’s complex filtergraph, you will learn how to manipulate the alpha channel of an image to smoothly transition it on and off-screen at specific timestamps.
To apply a fade-in and fade-out effect to an image overlay, you must
apply the fade filter to the image input before
combining it with the background video using the overlay
filter. Crucially, the fade filter must target the alpha (transparency)
channel of the image.
Here is the standard FFmpeg command to achieve this:
ffmpeg -i background.mp4 -loop 1 -i overlay.png -filter_complex "[1:v]format=rgba,fade=in:st=1:d=1:alpha=1,fade=out:st=5:d=1:alpha=1[over];[0:v][over]overlay=x=10:y=10:shortest=1" -c:a copy output.mp4Command Breakdown
-i background.mp4: Defines the main background video as the first input (0:v).-loop 1 -i overlay.png: Loops the static image so it can exist for the duration of the video. This represents the second input (1:v).format=rgba: Converts the image pixel format to RGBA, ensuring an alpha channel exists for transparency manipulation.fade=in:st=1:d=1:alpha=1: Applies the fade-in effect.st=1: The start time of the fade (1 second into the video).d=1: The duration of the fade (1 second long).alpha=1: Tells FFmpeg to fade the alpha channel (transparency) instead of fading to a solid color.
fade=out:st=5:d=1:alpha=1: Applies the fade-out effect.st=5: The start time of the fade-out (5 seconds into the video).d=1: The duration of the fade-out (1 second long).alpha=1: Targets the alpha channel for the fade-out.
[over]: Assigns a temporary label to the processed image stream.[0:v][over]overlay=x=10:y=10:shortest=1: Overlays the processed image stream[over]onto the background video[0:v].x=10:y=10: Positions the overlay 10 pixels from the top-left corner.shortest=1: Crucial when looping an image, this tells FFmpeg to stop encoding as soon as the shortest input (the background video) finishes.
-c:a copy: Copies the audio stream without re-encoding to save processing time.