Search code examples
bashaudioffmpeg

Mix multiple audios into one with FFmpeg


I have a lot of audio files I want to mix down into one audio file, that then should sound like as if all audios would play at the same time.

I tried to use the amix filter in the last command, but it fails with "Cannot find a matching stream for unlabeled input pad 1 on filter Parsed_amix_0".

> inputs.txt
n=0
for file in *.mp3; do
  echo "file '$file'" >> inputs.txt
  ((n++))
done

ffmpeg -f concat -i inputs.txt -filter_complex amix=inputs=$n output.mp3

If I manually parse the inputs files (e.g. 2 files) as follows

ffmpeg -i file1.mp3 -i file2.mp3 -filter_complex amix=inputs=2 output.mp3

the command works as intended. But I need it to work with an arbitrary amount of inputs.


Solution

  • It seems some command line tools are easier for dealing with a large amount of input files.

    Using sox, the command is rather simple:

    sox -m *.wav outputsox.wav
    

    And here is a Bash script using ffmpeg, that takes audio-/video-files as input arguments, converts them to .wav-files and mixes them with amix.

    inputs=()
    for f in "$@"; do
      ffmpeg -i "$f" -vn -acodec pcm_s16le "${f%.*}.wav"
      inputs+=("-i" "${f%.*}.wav")
    done
    ffmpeg "${inputs[@]}" -filter_complex amix=inputs=$# output.mp3