How to Use FFmpeg acrossfade Filter
This article provides a straightforward guide on how to use the
acrossfade filter in FFmpeg to seamlessly transition
between two audio tracks. You will learn the basic command structure,
the key parameters that control the duration and curve of the fade, and
practical examples to help you implement smooth audio crossfades in your
projects.
The acrossfade filter in FFmpeg merges two audio streams
by fading out the first stream while fading in the second stream over a
specified overlapping period. This is highly useful for creating
playlists, music mixes, or smooth transitions in video soundtracks.
Basic Command Structure
To apply a crossfade between two audio inputs, you must use the
-filter_complex option to route both audio streams through
the acrossfade filter.
Here is the fundamental command template:
ffmpeg -i input1.mp3 -i input2.mp3 -filter_complex "[0:a][1:a]acrossfade=d=5[a]" -map "[a]" output.mp3Key Parameters Explained
You can customize the transition by adjusting the filter’s parameters
inside the acrossfade argument, separated by colons:
d(duration): Sets the duration of the crossfade in seconds. The default is 4 seconds. For example,d=5creates a 5-second overlap and fade transition.o(overlap): A boolean value (0 or 1) that determines whether the files overlap. By default, this is set to1(enabled). If set to0, the tracks will play sequentially with a fade-out and fade-in but without overlapping.c1(curve 1): Defines the fade-out curve for the first audio stream.c2(curve 2): Defines the fade-in curve for the second audio stream.
Available Curve Types
You can choose from several mathematical curves to alter the feel of
the transition. Common options include: * linear (linear
transition, default) * tri (triangular, helpful for
maintaining constant volume) * qsin (quarter sine wave) *
esinh (exponential sine) * exp
(exponential)
Practical Examples
1. A Simple 3-Second Linear Crossfade
This command merges two files with a standard 3-second linear transition:
ffmpeg -i song1.wav -i song2.wav -filter_complex "[0:a][1:a]acrossfade=d=3:c1=linear:c2=linear[out]" -map "[out]" mixed.wav2. Smooth Exponential Crossfade
For a more natural-sounding transition where the volume drops slowly at first and then quickly (and vice versa for the fade-in), use the exponential curve:
ffmpeg -i track1.mp3 -i track2.mp3 -filter_complex "[0:a][1:a]acrossfade=d=6:c1=exp:c2=exp[out]" -map "[out]" output.mp3Important Considerations
- File Duration: The first input track must be longer
than the specified crossfade duration (
d), otherwise the filter will fail. - Audio Formats: Ensure both input files share the
same sample rate and channel layout for the best results. If they
differ, you may need to resample them first using the
aresamplefilter.