Video Crossfade in FFmpeg Using the xfade Filter
This guide explains how to blend two separate video files using the
xfade video filter in FFmpeg to create a smooth crossfade
transition. You will learn the correct command-line syntax, how to
calculate the transition offset time, how to synchronize audio
transitions using acrossfade, and the prerequisite video
properties required for the filter to work properly.
Basic Syntax and the Offset Calculation
The xfade filter requires three primary parameters:
transition: The style of the transition (e.g.,fade).duration: The length of the transition in seconds.offset: The timestamp (in seconds) in the first video where the transition begins.
To ensure a seamless transition, calculate the offset
using this formula:
\[\text{offset} = \text{Duration of Video 1} - \text{Transition Duration}\]
For example, if your first video is 10 seconds long and you want a
1-second crossfade, the transition must begin at the 9-second mark
(offset=9).
Video-Only Crossfade Command
To apply a standard crossfade transition between two video streams without processing audio, run:
ffmpeg -i video1.mp4 -i video2.mp4 -filter_complex "[0:v][1:v]xfade=transition=fade:duration=1:offset=9[v]" -map "[v]" output.mp4In this command:
[0:v]and[1:v]take the video streams from the first and second input files.xfade=transition=fade:duration=1:offset=9starts fading between the two clips at second 9 for a duration of 1 second.[v]names the resulting output video stream.
Crossfading Both Video and Audio
Because xfade only handles video streams, the audio will
cut abruptly unless you crossfade it simultaneously. Use the
acrossfade audio filter alongside xfade:
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]" -c:v libx264 -c:a aac output.mp4Here, acrossfade=d=1 creates a 1-second audio overlap,
aligning the audio transition with the video fade.
Technical Requirements for Inputs
For the xfade filter to function without errors, both
video clips must share the same:
- Dimensions (Width and Height)
- Frame Rate (FPS)
- Time Base
If your input videos have different formats or resolutions, normalize them within the filtergraph before applying the transition:
ffmpeg -i video1.mp4 -i video2.mp4 -filter_complex \
"[0:v]scale=1920:1080,fps=30,settb=AVTB[v0]; \
[1:v]scale=1920:1080,fps=30,settb=AVTB[v1]; \
[v0][v1]xfade=transition=fade:duration=1:offset=9[v]; \
[0:a][1:a]acrossfade=d=1[a]" \
-map "[v]" -map "[a]" -c:v libx264 -c:a aac output.mp4Alternative Transition Types
FFmpeg supports numerous transition effects using the same syntax.
Replace transition=fade with other supported values, such
as:
transition=wipelefttransition=sliderighttransition=circlecroptransition=dissolve