日本語

Combining Two Mono Audio Files into Left and Right Channels with FFmpeg

​​FFmpeg​audio,command line,video

To combine two mono audio files into one stereo file, with one on the left and the other on the right, use FFmpeg's amerge filter. This method is useful when you want to put the audio from a conversation or interview recorded with two separate microphones into one file, but still be able to split it back into the original two tracks later by left and right.

In my test environment, I created a 440 Hz tone (a.wav) and an 880 Hz tone (b.wav) as separate files. After combining them, I measured which tone was in the left and right channels and at what level.

Note

The test environment was Ubuntu 24.04.5 with FFmpeg 6.1.1 (Ubuntu package 6.1.1-3ubuntu5). The audio was sine waves generated with lavfi sine. a.wav and b.wav are 48 kHz, 16-bit, 10-second mono files. a.wav has amplitude 0.5 (RMS -9.03 dBFS), and b.wav has amplitude 0.25 (RMS -15.05 dBFS). I did not test recordings of human voices or music, compressed inputs such as MP3, or combining three or more audio files. I converted the files to 16-bit PCM, computed RMS with a Python script, and got the frequencies from the zero-crossing count.

Putting the first input on the left and the second on the right with amerge

The amerge filter arranges the channels of several audio inputs side by side into one audio stream. Since there are two inputs, write it with -filter_complex.

bash
ffmpeg -i a.wav -i b.wav -filter_complex "[0:a][1:a]amerge=inputs=2" lr.wav

The resulting lr.wav had 2 channels, 48 kHz, and 10 seconds. Here are the measurements for each channel:

Channel RMS Main frequency
Left (1st) -9.03 dBFS about 440 Hz
Right (2nd) -15.05 dBFS about 880 Hz

The left channel had a.wav and the right channel had b.wav, both at their original levels. When I extracted each channel and compared the MD5 of the 16-bit PCM, the left matched a.wav and the right matched b.wav. The samples were placed side by side as they were, without changing the volume or mixing.

The left/right assignment follows the order written in [0:a][1:a]. When I wrote it in reverse, [1:a][0:a]amerge=inputs=2, the left channel became 880 Hz and the right channel became 440 Hz.

Running this command prints the following warning. I omitted the preceding No channel layout for input 1 line and include only the line about the overlap.

text
[Parsed_amerge_0 @ 0x…] Input channel layouts overlap: output layout will be determined by the number of distinct input channels

This means both inputs are mono, so their channel layouts overlap. In this case, the output layout is determined by the total number of channels, and FFmpeg reported it as stereo. The contents of the left and right channels are as shown in the table above, so you can ignore this warning.

join gives the same result

You can do the same with the join filter. Specify the output channel layout with channel_layout (the default is stereo).

bash
ffmpeg -i a.wav -i b.wav -filter_complex "[0:a][1:a]join=inputs=2:channel_layout=stereo" lr_join.wav

The MD5 of the resulting file matched lr.wav made with amerge. With join, the warning above was not printed.

With join, you can use map to name which input goes to which channel. For example, map=0.0-FR|1.0-FL places the first channel of the first input on the right (FR) and the first channel of the second input on the left (FL). With this mapping, the left channel became 880 Hz and the right channel became 440 Hz. This is useful when you want to swap left and right without changing the input order.

When lengths differ, the output stops at the shorter input

When I used b6.wav, a 6-second file, in place of b.wav, the output ended at 6 seconds. The official amerge documentation also says that when input lengths differ, the output stops at the shorter one. join also produced 6 seconds.

If you want to match the longer input, add silence to the end of the shorter one with the apad filter:

bash
ffmpeg -i a.wav -i b6.wav -filter_complex "[1:a]apad[r];[0:a][r]amerge=inputs=2" lr_pad.wav

Without options, apad keeps adding silence indefinitely, but amerge stops at the shorter input. The output was 10 seconds, the same as a.wav. After 6 seconds, the left channel still had the -9.03 dBFS tone, and the right channel was silent, with every sample at 0.

With amerge alone, both channels end at 6 seconds. When apad is added to the right channel, the left channel still has sound until 10 seconds, and the right channel is silent from 6 to 10 seconds
Presence of sound per second when the 6-second audio is put on the right channel

When sample rates differ, the output is converted to match the first input

The official amerge documentation says all inputs must have the same sample rate and format. So I combined a.wav (48 kHz) with b44.wav, which is b.wav created at 44.1 kHz.

bash
ffmpeg -i a.wav -i b44.wav -filter_complex "[0:a][1:a]amerge=inputs=2" sr_amerge.wav

With FFmpeg 6.1.1, this produced 48 kHz stereo without an error. The right channel was about 880 Hz at -15.05 dBFS, with the original pitch and level. Running the command with -loglevel verbose showed that FFmpeg automatically inserted a conversion filter (auto_aresample_0) that converts 44.1 kHz to 48 kHz:

text
[auto_aresample_0 @ 0x…] ch:1 chl:mono fmt:s16 r:44100Hz -> ch:1 chl:mono fmt:s16 r:48000Hz

When I reversed the inputs so that the 44.1 kHz b44.wav came first, the output was 44.1 kHz. The output sample rate was determined by the first input.

If you insert aresample yourself, also add aformat

When I inserted the aresample filter to control the conversion myself, nothing was written and I got the following error:

bash
ffmpeg -i a.wav -i b44.wav -filter_complex "[1:a]aresample=48000[r];[0:a][r]amerge=inputs=2" sr_fix.wav
text
[AVFilterGraph @ 0x…] The following filters could not choose their formats: Parsed_amerge_1
Consider inserting the (a)format filter near their input or output.

I included only the first two lines of the output, which explain the cause. @ 0x… stands in for a memory address that I omitted because it changes on every run. Even when I inserted aresample on b.wav, which has the same sample rate as a.wav, I got the same error.

As the message suggests, adding the aformat filter with channel_layouts=mono after aresample let the file be written:

bash
ffmpeg -i a.wav -i b44.wav -filter_complex "[1:a]aresample=48000,aformat=channel_layouts=mono[r];[0:a][r]amerge=inputs=2" sr_fix.wav

The MD5 of the file made this way matched the one made by letting FFmpeg handle the conversion without an explicit aresample.

Combining two stereo files gives 4 channels

amerge puts all the channels of its inputs side by side, so combining two stereo files gives 4 channels. I tested this with s1.wav (440 Hz on the left, 660 Hz on the right) and s2.wav (880 Hz on the left, 1320 Hz on the right).

bash
ffmpeg -i s1.wav -i s2.wav -filter_complex "[0:a][1:a]amerge=inputs=2" st.wav

The resulting st.wav had channels=4 and channel_layout=4.0, containing s1 left, s1 right, s2 left, and s2 right, in that order. The amerge documentation also says that when combining stereo inputs, the layout ends up as 4.0 by default, which may not be what you intended.

Adding -ac 2 afterwards to get 2 channels does not put s1 on the left and s2 on the right. The s1 left went into the output left, and the s1 right went into the output right, each at 0.45 times. Both channels of s2 were mixed into both output channels: the left at 0.32 times and the right at 0.23 times.

With -ac 2, the output left is the s1 left at 0.45 times, plus the s2 left at 0.32 times and the s2 right at 0.23 times. The output right is mixed with both s2 channels in the same way. If pan is used first, the output left is the s1 left and right at 0.50 times each, and the output right is the s2 left and right at 0.50 times each
The gain at which each source entered the left and right output channels when two stereo files were combined

To make s1 the left channel and s2 the right channel, convert each input to mono with the pan filter before amerge. c0<c0+c1 is a setting that adds the left and right channels together and normalizes the coefficients so they sum to 1.

bash
ffmpeg -i s1.wav -i s2.wav -filter_complex "[0:a]pan=mono|c0<c0+c1[l];[1:a]pan=mono|c0<c0+c1[r];[l][r]amerge=inputs=2" st_lr.wav

The resulting st_lr.wav has 2 channels. The left channel contains s1's left and right channels at 0.5 times each, and the right channel contains s2's left and right channels at 0.5 times each. The MD5 of the left channel matched s1.wav converted to mono with -ac 1.

If you want to use only the left channel of each input without mixing the left and right channels, use pan=mono|c0=c0. With this setting, the left channel contained only s1's left channel at 440 Hz (-9.03 dBFS), and the right channel contained only s2's left channel at 880 Hz (-15.05 dBFS).

Adding separately recorded audio as the right channel of a video's audio

To put the mono audio in a video on the left and separately recorded audio on the right, write the combined audio out together with the video using -map. cam.mp4 is a 10-second video with 320×180 H.264 video and the sound of a.wav encoded as mono AAC at 128 kbps.

bash
ffmpeg -i cam.mp4 -i b.wav -filter_complex "[0:a][1:a]amerge=inputs=2[a]" -map 0:v -map "[a]" -c:v copy -c:a aac -b:a 192k out.mp4

The audio in the resulting out.mp4 was AAC with channels=2 and channel_layout=stereo. The left channel was about 440 Hz at -9.04 dBFS, and the right channel was about 880 Hz at -15.06 dBFS. Because I copied the video with -c:v copy, the MD5 of the video stream matched the original cam.mp4.

Using join=inputs=2:channel_layout=stereo instead of amerge also produced a stereo AAC file.

To split the combined stereo file back into the two original tracks later, you can use channelsplit or pan=mono|c0=c0 to extract the left and right channels separately. Both are covered in the separate article "Converting Stereo Audio to Mono with FFmpeg and How to Split Left and Right Channels."

​Posted by 86