FFmpeg amix Filter: How to Mix Audio Tracks
This article provides a practical guide on how to use the
amix filter in FFmpeg to combine multiple audio inputs into
a single output track. You will learn the basic syntax, key parameters
such as inputs and duration, and how to apply
practical configurations like adjusting individual volumes before mixing
them together.
Basic Syntax of the amix Filter
The amix filter takes multiple audio inputs and mixes
them into a single audio output. The simplest command to mix two audio
files looks like this:
ffmpeg -i input1.mp3 -i input2.mp3 -filter_complex amix=inputs=2 output.mp3In this command, -filter_complex is used because we are
dealing with multiple inputs. amix=inputs=2 tells FFmpeg to
expect two audio streams and combine them.
Key Parameters for the amix Filter
You can customize how the audio tracks are merged by using specific
parameters separated by colons (:).
- inputs: Specifies the number of inputs to mix. The default is 2.
- duration: Decides when the mixing should stop. It
accepts three values:
longest: The output ends when the longest input ends (default).shortest: The output ends when the shortest input ends.first: The output ends when the first input ends.
- dropout_transition: Specifies the transition time (in seconds) for volume renormalization when an input stream ends. The default is 2 seconds.
Example with Custom Duration
If you want to mix a voice track and a background music track, but
you want the output to end as soon as the voice track (the first input)
finishes, use the duration=first parameter:
ffmpeg -i voice.mp3 -i music.mp3 -filter_complex "amix=inputs=2:duration=first" output.mp3Mixing Audio with Volume Adjustments
By default, the amix filter automatically lowers the
volume of each input to prevent clipping (distortion caused by
overloading the audio levels). If you want to manually control the
volume of each track before mixing them, you can chain the
volume filter with the amix filter.
In the following example, the volume of the first input (voice) is kept at 100%, while the second input (background music) is lowered to 20% of its original volume:
ffmpeg -i voice.mp3 -i music.mp3 -filter_complex "[0:a]volume=1.0[a1]; [1:a]volume=0.2[a2]; [a1][a2]amix=inputs=2:duration=first" output.mp3How the Filter Chain Works:
[0:a]volume=1.0[a1]takes the audio of the first input (0:a), applies the volume filter, and labels the temporary stream[a1].[1:a]volume=0.2[a2]takes the audio of the second input (1:a), reduces its volume to 20%, and labels the temporary stream[a2].[a1][a2]amix=inputs=2takes both labeled streams and mixes them together into the final output.