FFmpeg Concatenate Two Videos with Fade Transition
This article provides a straightforward guide on how to merge two
video files with a seamless crossfade transition using FFmpeg. You will
learn the exact command-line syntax, how the xfade filter
works, and how to calculate the transition timing based on your video
length.
To concatenate two videos with a fade transition, you must use
FFmpeg’s xfade filter for the video stream and the
acrossfade filter for the audio stream.
The FFmpeg Command
Here is the standard command template:
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.mp4How to Calculate the Offset
The offset parameter determines exactly when the
transition begins. To calculate the correct offset, use this
formula:
\[\text{Offset} = \text{Duration of Video 1} - \text{Duration of Transition}\]
For example, if your first video is 10 seconds long and you want a 1-second fade transition: * Duration (\(D\)) = 10 * Transition Duration (\(T\)) = 1 * Offset (\(O\)) = \(10 - 1 = 9\) seconds.
Parameter Breakdown
-i video1.mp4 -i video2.mp4: Defines the two input video files.[0:v][1:v]: Takes the video streams from the first (0) and second (1) inputs.xfade=transition=fade: Applies a standard fade-out/fade-in transition. You can also use other transition types likewipeleft,slideup, orcirclecrop.duration=1: The transition effect will last for 1 second.offset=9: The transition starts at the 9-second mark of the first video.[0:a][1:a]acrossfade=d=1: Smoothly crossfades the audio streams from both videos with a duration (\(d\)) of 1 second.-map "[v]" -map "[a]": Combines the newly processed video and audio streams into the final output file (output.mp4).