Ffmpeg – mapping multiple audios to one channel

ffmpegvideo streamingvideo-encoding

I want to output a video file with multiple sounds, but put them in one channel.
I've tried couple ways to do it, but I'm always given a video with multiple sound streams.
Like this

My command is

ffmpeg -r 30 -i test_%03d.png 
-itsoffset 10 -ss 5 -t 20 -i s01.wav
-itsoffset 15 -ss 10 -t 30 -i s02.wav 
-map 0:v -map 1:a:0 -map 2:a:0 -c:v libopenh264 -c:a mp3 test.mp4

New command test

-framerate 30 -i test_%03d.png -ss 0.2 -t 1 -i sound01.wav -ss 1 -t 2 -i sound02.wav
-filter_complex
"[1] aformat=sample_fmts=s16p:sample_rates=44100:channel_layouts=mono [a1];
 [2] aformat=sample_fmts=s16p:sample_rates=44100:channel_layouts=mono [a2]; 
 [a1]adelay=400|400,apad[b1]; 
 [a2]adelay=900|900[b2]; 
 [b1][b2]amerge=2[a]" 
 -map 0:v -map "[a]" -c:v libopenh264 -c:a mp3 -ac 2 output.mp4

Because filters could not choose their formats error occurred, I added aformat to the command.
But now another error is happening, No channel layout for input 1 and output video lacks sound01, please help me out!

Best Answer

To combine multiple audio streams into one, you have to use filters to merge the streams:

ffmpeg -framerate 30 -i test_%03d.png -i s01.wav -i s02.wav \
       -filter_complex "[1][2]amerge=2[a]" \
       -map 0:v -map "[a]"   
       -c:v libopenh264 -c:a mp3 -ac 2 test.mp4

The framerate is the right option for image and raw streams.

The -ac 2 downmixes the merged audio to 2 channels since that's the upper limit for the MP3 encoder.


For the updated command,

ffmpeg -framerate 30 -i test_%03d.png 
       -ss 5 -t 20 -i s01.wav
       -ss 10 -t 30 -i s02.wav
       -filter_complex "[1]adelay=10000|10000,apad[a1];
                        [2]adelay=15000|15000[a2];
                        [a1][a2]amerge=2[a]" \
       -map 0:v -map "[a]" -c:v libopenh264 -c:a mp3 test.mp4

The adelay pads the audio in front, and takes value in milliseconds per channel. The apad pads audio at the end, and is needed because amerge ends with the shortest stream.