How to Use FFmpeg amerge Filter to Merge Audio Tracks
This article provides a straightforward guide on how to use the
amerge filter in FFmpeg to combine multiple audio streams
into a single multi-channel audio track. You will learn the basic
command syntax, how to merge two or more audio inputs, how to handle
mismatched sample rates, and how amerge differs from the
amix filter.
Understanding the amerge Filter
The amerge filter merges two or more audio streams into
a single multi-channel stream. For example, if you have two separate
mono audio files, you can use amerge to combine them into a
single stereo (2-channel) audio file, where one input becomes the left
channel and the other becomes the right channel.
Basic Syntax for Merging Two Audio Tracks
To merge two separate audio files into a single stereo output, use the following command:
ffmpeg -i input1.wav -i input2.wav -filter_complex amerge=inputs=2 -ac 2 output.wav-i input1.wav -i input2.wav: Specifies the two input audio files.-filter_complex amerge=inputs=2: Calls theamergefilter and specifies that there are2input streams.-ac 2: Forces the output to have 2 channels (stereo).
Merging Specific Audio Streams from Multi-stream Files
If you are working with video files that contain multiple audio tracks and want to merge them, you must explicitly map the audio streams:
ffmpeg -i input.mp4 -filter_complex "[0:a:0][0:a:1]amerge=inputs=2[aout]" -map 0:v -map "[aout]" -c:v copy output.mp4[0:a:0][0:a:1]: Selects the first and second audio streams of the first input file.[aout]: Labels the merged audio output.-map 0:v: Copies the video stream from the input file without re-encoding.-map "[aout]": Maps the merged audio to the output file.
Merging More Than Two Streams
You can merge three or more streams by increasing the
inputs parameter and specifying the input labels
accordingly:
ffmpeg -i in1.wav -i in2.wav -i in3.wav -i in4.wav -filter_complex "[0:a][1:a][2:a][3:a]amerge=inputs=4[aout]" -map "[aout]" output.wavHandling Mismatched Sample Rates
The amerge filter requires all input streams to have the
exact same sample rate (e.g., 44100 Hz or 48000 Hz). If the sample rates
differ, the command will fail. You can resolve this by adding the
aresample filter to synchronize the sample rates before
merging:
ffmpeg -i input1.wav -i input2.wav -filter_complex "[0:a]aresample=async=1[a1];[1:a]aresample=async=1[a2];[a1][a2]amerge=inputs=2" output.wavThe Difference Between amerge and amix
It is important not to confuse amerge with
amix: * amerge: Joins streams
side-by-side into separate channels (e.g., merging two Mono tracks to
create one Stereo track). * amix:
Overlays/mixes the audio together into the same channels (e.g., mixing
background music with a voiceover so both are heard simultaneously on
both the left and right speakers).