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.mp3

In 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 (:).

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.mp3

Mixing 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.mp3

How the Filter Chain Works:

  1. [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].
  2. [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].
  3. [a1][a2]amix=inputs=2 takes both labeled streams and mixes them together into the final output.