Can I overlay/downmix two audio mp3 files into one mp3 output file using ffmpeg?
-
You can decode both of them, mix ([pretty simple operation](http://stackoverflow.com/a/4567156/453271)) and encode again – Andriy Tylychko Jan 24 '13 at 15:55
-
1Can you please mention commands to do that. I checked the other post you mentioned in your link but it did not help. – Faisal Jan 24 '13 at 17:54
-
@Faisalcan u please guide me..how to build ffmpeg on ubuntu? if yes then i will post new question on Stackoverflow with all the necessary details.. – TheFlash Jun 19 '13 at 05:37
7 Answers
stereo + stereo → stereo
Normal downmix
Use the amix filter:
ffmpeg -i input0.mp3 -i input1.mp3 -filter_complex amix=inputs=2:duration=longest output.mp3
Or the amerge filter:
ffmpeg -i input0.mp3 -i input1.mp3 -filter_complex amerge=inputs=2 -ac 2 output.mp3
Downmix each input into specific output channel
Use the amerge and pan filters:
ffmpeg -i input0.mp3 -i input1.mp3 -filter_complex "amerge=inputs=2,pan=stereo|c0<c0+c1|c1<c2+c3" output.mp3
mono + mono → stereo
Use the join filter:
ffmpeg -i input0.mp3 -i input1.mp3 -filter_complex join=inputs=2:channel_layout=stereo output.mp3
Or amerge:
ffmpeg -i input0.mp3 -i input1.mp3 -filter_complex amerge=inputs=2 output.mp3
mono + mono → mono
Use the amix filter:
ffmpeg -i input0.mp3 -i input1.mp3 -filter_complex amix=inputs=2:duration=longest output.mp3
More info and examples

- 121,796
- 28
- 232
- 243
-
1Thanks, it really helped and done without errors. But my audio files are different in length. Do you have any idea that how to generate output file to the maximum length of input files? – Faisal Jan 28 '13 at 13:51
-
please can you give the example by putting fake path and value . It will be appreciated – Ahmad Arslan Jul 09 '14 at 06:52
-
Will it be using like this ?? {"ffmpeg","-i", Audio,"-i",Audio2, "-filter_complex" ,"amerge", "-c:a", "libmp3lame","-q:a","4" ,filenameVideoReplace}; – Ahmad Arslan Jul 09 '14 at 07:02
-
-
It worked for me. It's perfect and faster "ffmpeg -y -i input1.mp3 -i input2.mp3 -filter_complex amerge -c:a libmp3lame -q:a 4 output.mp3" – sonida May 06 '15 at 08:42
-
-
1@ArslanAhmad Without your actual command and the complete console output I can only guess that your `ffmpeg` was not configured with `--enable-libmp3lame` and therefore cannot use this encoder. – llogan Mar 24 '16 at 16:38
-
`amerge` creates *one multi-channel* which is then "squashed" into a stereo stream by `-ac 2`. @Ehsan's answer utilising `amix` is the better way to go. – Hermann Aug 12 '18 at 19:59
-
@Hermann amerge is more flexible in this regard because you can choose the desired downmix channel layout, but either should be fine for the general user. – llogan Aug 12 '18 at 23:08
-
@llogan, I tried using this cmd but it goes to the failure callback method. – Asif Sb Mar 27 '19 at 10:04
-
-
@llogan, I tried using this command for merging two audios using FFMPEG, It is not working.If it is working for you can you please share some code? – Asif Sb Mar 28 '19 at 06:06
-
@AsifSb Use a pastebin link to show your ffmpeg command and the complete log from the command. – llogan Mar 28 '19 at 17:21
-
@AsifSb Is the problem with the script or the ffmpeg command? If the problem is ffmpeg then get rid of the extra layer of complexity from the scripting. Just run the ffmpeg command manually, unscripted and provide the complete log from the ffmpeg command. Once you verify that ffmpeg runs only then should you attempt to script it. – llogan Mar 29 '19 at 17:15
-
I don't think the problem is with the FFmpeg but command. Because when I try to execute another command it gives me success. – Asif Sb Apr 01 '19 at 06:59
-
@AsifSb The command works for me. I can't help you because you never provided the log from ffmpeg. – llogan Apr 01 '19 at 16:57
Check this out:
ffmpeg -y -i ad_sound/whistle.mp3 -i ad_sound/4s.wav -filter_complex "[0:0][1:0] amix=inputs=2:duration=longest" -c:a libmp3lame ad_sound/outputnow.mp3
I think it will help.

- 9,671
- 41
- 36
- 56

- 486
- 4
- 9
-
1this worked better for me, as take care of the differences in lenght.. this is how I mixed three sounds: ffmpeg -i flute_long.ogg -i gallop.ogg -i wind.ogg -filter_complex "[0:0][1:0] amix=inputs=3:duration=longest" main.ogg – cesarpachon Oct 26 '16 at 21:31
-
Is there anyway to adjust the volume on one of the input files down say 20%? – chovy Mar 21 '17 at 00:20
-
3@chovy ffmpeg -y -i A.mp3 -i B.mp3 -filter_complex "[0:0]volume=0.2[a];[1:0]volume=0.5[b];[a][b]amix=inputs=2:duration=longest" -c:a libmp3lame output1.mp3 – dastan Mar 15 '18 at 10:26
-
Thanks a lot, worked perfectly for me. I wanted to combine 2 different sound's into 1 file that has different durations. – Shino Lex Feb 25 '19 at 11:55
-
Not a good solution. The volume of one of the audios must be decreased significantly if it is used as backgroumd (most common case). So one has to *actually* modify the volume of that file in order thast if can be mixed. See solution by @Sheetansh Kumar ... – Apostolos Aug 17 '23 at 09:08
For merging two audio files with different volumes and different duration following command will work:
ffmpeg -y -i audio1.mp3 -i audio2.mp3 -filter_complex "[0:0]volume=0.09[a];[1:0]volume=1.8[b];[a][b]amix=inputs=2:duration=longest" -c:a libmp3lame output.mp3
Here duration can be change to longest or to shortest, you can also change the volume levels according to your need.
If you're looking to add background music to some voice use the following command as in the gaps the music will become loud automatically:
ffmpeg -i bgmusic.mp3 -i audio.mp3 -filter_complex "[1:a]asplit=2[sc][mix];[0:a][sc]sidechaincompress=threshold=0.003:ratio=20[bg]; [bg][mix]amerge[final]" -map [final] final.mp3
In this threshold is something whose value will decide how much loud the audio should be, the less the threshold more the audio will be. Ratio gives how much the other audio should be compressed, the more the ratio the more the compression is.

- 191
- 2
- 8
-
Hi, can you please help me with my issue: I have 1 audio (length about 1 hour) and 1 background music only 1-3 minutes length. I want background music loop with audio length. How can I do that? – VnDevil Nov 24 '22 at 19:04
-
You can concatenate same audio clip multiple times till the required length and while merging with 1hour clip you selected longest/shortest according to your requirement. – Sheetansh Kumar Dec 07 '22 at 08:14
-
1getting error with amerge The following filters could not choose their formats: Parsed_amerge_2 Consider inserting the (a)format filter near their input or output. – Naveen Dew Dec 07 '22 at 16:07
-
Great solution! Modify the volume on the run and ww/o having to actually lower the volume of one of the audios. This answer should get the most upvotes! – Apostolos Aug 17 '23 at 09:07
The amix
filter helps to mix multiple audio inputs into a single output.
If you run the following command:
ffmpeg -i INPUT1 -i INPUT2 -i INPUT3 -filter_complex amix=inputs=3:duration=first:dropout_transition=3 OUTPUT
This command will mix 3 input audio streams (I used two mp3 files, in the example below) into a single output with the same duration as the first input and a dropout transition time of 3 seconds.
The amix
filter accepts the following parameters:
inputs: The number of inputs. If unspecified, it defaults to 2.
duration: How to determine the end-of-stream.
longest: The duration of the longest input. (default)
shortest: The duration of the shortest input.
first: The duration of the first input.
dropout_transition: The transition time, in seconds, for volume renormalization when an input stream ends. The default value is 2 seconds.
For example, I ran the following command in Ubuntu: FFMPEG version: 3.2.1-1 UBUNTU 16.04.1
ffmpeg -i background.mp3 -i bSound.mp3 -filter_complex amix=inputs=2:duration=first:dropout_transition=0 -codec:a libmp3lame -q:a 0 OUTPUT.mp3
-codec:a libmp3lame -q:a 0
was used to set a variable bit rate. Remember that, you need to install the libmp3lame library, if is necessary. But, it will work even without the -codec:a libmp3lame -q:a 0
part.
Reference: https://ffmpeg.org/ffmpeg-filters.html#amix
-
-
Works perfectly well AND is also available for avconv, contrary to the amerge plugin which didn't work for my version of avconv `(avconv version 9.20-6:9.20-0ubuntu0.14.04.1)` – ychaouche Jan 24 '19 at 09:10
-
Hi, can you please help me with my issue: I have 1 audio (length about 1 hour) and 1 background music only 1-3 minutes length. I want background music loop with audio. How can I do that? – VnDevil Nov 24 '22 at 19:03
With Bash
set 'amovie=a.mp3 [gg]; amovie=b.mp3 [hh]; [gg][hh] amerge'
ffmpeg -f lavfi -i "$1" -q 0 c.mp3

- 1
- 62
- 391
- 407
-
-
example offline - what exactly does set do with this argument? how does this influence the following `ffmpeg` command? – Daniel Alder Jun 14 '22 at 13:30
You can use the following command arguments:
// Command is here
let commandValue = "-y -i \(recordedAudioPath) -i \(backgroundAudio) -filter_complex [\(0):a][\(1):a]amerge=inputs=\(2)[a] -map [a] -ac \(2) -shortest -preset ultrafast \(outputPath)"
MobileFFmpeg.execute(commandValue)

- 4,213
- 2
- 17
- 28

- 503
- 2
- 12