Multiplex Multiple Audio Streams into AVI Using FFmpeg

This article provides a quick and practical guide on how to combine a single video file with multiple separate audio tracks into a single AVI container using FFmpeg. You will learn the exact command-line syntax, the importance of stream mapping, and how to configure codecs for optimal compatibility with the AVI format.

The Basic Command Structure

To multiplex (mux) multiple audio streams into an AVI file, you must use FFmpeg's -map option. By default, FFmpeg only includes one video stream and one audio stream in the output. The -map flag manually overrides this behavior, allowing you to select and assign multiple input streams to the output container.

Here is the standard command to combine one video file and two separate audio files into an AVI container:

ffmpeg -i video.mp4 -i audio1.wav -i audio2.wav -map 0:v -map 1:a -map 2:a -c:v copy -c:a ac3 output.avi

Understanding the Parameters

  • -i video.mp4: Defines the first input file (index 0), which contains the video.
  • -i audio1.wav: Defines the second input file (index 1), representing the first audio track.
  • -i audio2.wav: Defines the third input file (index 2), representing the second audio track.
  • -map 0:v: Maps the video stream (v) from the first input file (0) to the output.
  • -map 1:a: Maps the audio stream (a) from the second input file (1) as the first audio track in the output.
  • -map 2:a: Maps the audio stream (a) from the third input file (2) as the second audio track in the output.
  • -c:v copy: Copies the video stream stream directly without re-encoding, preserving original quality and saving processing time.
  • -c:a ac3: Encodes the audio streams into AC-3 format. The AVI container has limited support for modern audio formats like AAC; formats like AC-3 or MP3 (-c:a libmp3lame) ensure maximum compatibility.

Labeling the Audio Streams (Metadata)

To help media players identify the different audio tracks (for example, different languages), you can add language metadata to each stream using the -metadata option:

ffmpeg -i video.mp4 -i english.wav -i spanish.wav -map 0:v -map 1:a -map 2:a -c:v copy -c:a ac3 -metadata:s:a:0 language=eng -metadata:s:a:1 language=spa output.avi

In this command:

  • -metadata:s:a:0 language=eng sets the language of the first audio stream (index 0) to English.
  • -metadata:s:a:1 language=spa sets the language of the second audio stream (index 1) to Spanish.