FFmpeg Map Audio with Specific Channel Count

To map only the audio tracks that have a specific channel count in FFmpeg, you cannot use a native static stream specifier alone, as FFmpeg does not support filtering by channel count directly inside the standard -map argument. Instead, the most efficient and robust method is to use ffprobe to identify the stream indexes that match your target channel count, and then pass those indexes to the -map option in your ffmpeg command. This article provides a ready-to-use command-line pipeline to automate this process.

The Two-Step Manual Process

If you want to perform the steps manually, you first query the file to find the correct audio stream index, and then map it.

Step 1: Identify the Stream Index with FFprobe

Run the following command to list all audio streams along with their index and channel count:

ffprobe -v error -select_streams a -show_entries stream=index,channels -of csv=p=0 input.mkv

This will return an output where the first number is the stream index and the second is the channel count:

1,2
2,6

(In this example, stream index 1 has 2 channels (stereo), and stream index 2 has 6 channels (5.1 surround).)

Step 2: Map the Targeted Stream in FFmpeg

Once you know the index of the stream with your desired channel count, map it using the -map 0:[index] syntax. For example, to map only the stereo track (index 1) and keep the video:

ffmpeg -i input.mkv -map 0:v -map 0:1 -c copy output.mkv

Automated One-Liner (Bash)

To automate this in a single command without manual inspection, you can pipe the output of ffprobe directly into ffmpeg using command substitution and awk.

The following command automatically maps the video and only the audio streams that have exactly 2 channels (stereo):

ffmpeg -i input.mkv -map 0:v? $(ffprobe -v error -select_streams a -show_entries stream=index,channels -of csv=p=0 input.mkv | awk -F, '$2==2 {print "-map 0:"$1}') -c copy output.mkv

How it works: