How to Use FFmpeg xfade Filter for Video Transitions
This guide explains how to apply transition effects between two video
files using the FFmpeg xfade filter. You will learn the
fundamental command structure, how to calculate the correct transition
timing, how to handle accompanying audio, and how to choose from various
transition styles to create seamless video cuts.
The xfade Filter Syntax
The xfade (cross-fade) filter merges two video streams
using a specified transition effect. To use it successfully, you must
define three primary parameters:
transition: The name of the transition effect (e.g.,fade,wipeleft,slideup).duration: The length of the transition effect in seconds.offset: The exact timestamp (in seconds) in the first video where the transition should begin.
How to Calculate the Offset
The most critical step in using xfade is calculating the
offset. If your first video is 10 seconds long and you want
a 1-second transition, the transition must start at the 9-second
mark.
The formula is:
offset = (Duration of Video 1) - (Transition Duration)
Basic Command Example
To apply a 1-second fade transition between video1.mp4
(which is 10 seconds long) and video2.mp4, use the
following command:
ffmpeg -i video1.mp4 -i video2.mp4 -filter_complex "[0:v][1:v]xfade=transition=fade:duration=1:offset=9[v]" -map "[v]" output.mp4Here is a breakdown of the command: *
-i video1.mp4 -i video2.mp4: Imports the two input videos.
* -filter_complex: Invokes FFmpeg’s complex filtergraph. *
[0:v][1:v]: Selects the video streams of the first and
second inputs. * xfade=transition=fade:duration=1:offset=9:
Applies the fade transition for 1 second starting at second 9. *
[v]: Labels the output video stream. *
-map "[v]": Ensures the processed video stream is mapped to
the output file.
Adding Audio Transitions
The xfade filter only processes video. To prevent the
audio from cutting off abruptly, you must cross-fade the audio streams
using the acrossfade filter.
Here is the complete command combining both video and audio transitions:
ffmpeg -i video1.mp4 -i video2.mp4 -filter_complex "[0:v][1:v]xfade=transition=fade:duration=1:offset=9[v];[0:a][1:a]acrossfade=d=1[a]" -map "[v]" -map "[a]" output.mp4In this command, acrossfade=d=1 cross-fades the audio
streams for a duration (d) of 1 second at the end of the
first clip.
Available Transition Effects
FFmpeg supports dozens of transition effects. You can substitute
transition=fade in the command with any of the following
popular options:
- Wipes:
wipeleft,wiperight,wipeup,wipedown - Slides:
slideleft,slideright,slideup,slidedown - Shapes:
circlecrop,rectcrop - Dissolves:
dissolve,pixelize