FFmpeg xfade: How to Configure Duration and Offset
This article provides a straightforward guide on how to configure the
transition duration and start offset using the xfade filter
in FFmpeg. You will learn the exact syntax, how the timing parameters
interact, and see practical command-line examples to seamlessly
transition between two video inputs.
The xfade (crossfade) filter in FFmpeg merges two video
streams using a specified transition effect. To control when the
transition starts and how long it lasts, you must configure two key
parameters: offset and duration.
Understanding the Parameters
offset: The time (in seconds) from the beginning of the first video when the transition effect starts.duration: The length of the transition effect (in seconds) during which both videos overlap.
The Timing Formula
To calculate the correct offset, you must know the
duration of your first video. Because the transition overlaps both
videos, the transition should begin before the first video ends.
Use this formula to calculate the offset:
Offset = (Duration of Video 1) - (Transition Duration)
For example, if your first video is 10 seconds long and you want a
2-second fade transition, the transition must start at 8 seconds:
10 - 2 = 8 seconds
Command Syntax and Example
Here is the basic FFmpeg command to apply a fade transition between two videos using the calculated timing:
ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "[0:v][1:v]xfade=transition=fade:duration=2:offset=8[outv]" -map "[outv]" output.mp4Command Breakdown
-i input1.mp4 -i input2.mp4: Imports the two source video files.-filter_complex: Invokes FFmpeg’s complex filtergraph, which is required for processing multiple inputs.[0:v][1:v]: Selects the video streams of the first and second inputs.xfade=transition=fade: Specifies the transition type (in this case, a standardfade). Other options includewipe,slide,circlecrop, and more.duration=2: Sets the transition length to 2 seconds.offset=8: Starts the transition 8 seconds into the first video.[outv]: Names the output video stream.-map "[outv]": Maps the filtered output stream to the final output fileoutput.mp4.