DEV Community

Javid Jamae
Javid Jamae

Posted on • Originally published at ffmpeg-micro.com

FFmpeg -map Flag Explained: Stream Selection and Multi-Track Audio

Originally published at ffmpeg-micro.com

You run ffmpeg -i input.mkv output.mp4 and somehow your output is missing the commentary audio track. Or it grabbed the wrong subtitle language. FFmpeg's automatic stream selection does its best, but "best" rarely matches what you actually want when you're working with multi-track files.

The -map flag gives you explicit control over which streams go into your output. This post walks through how -map actually works, with examples you can copy and run.

How FFmpeg Picks Streams Without -map

When you don't use -map, FFmpeg applies its automatic stream selection algorithm. It picks one video stream (the highest resolution), one audio stream (the most channels), and one subtitle stream (the first it finds). Everything else gets dropped.

Identify Your Streams First with ffprobe

ffprobe -v error -show_entries stream=index,codec_type,codec_name,channels -of json input.mkv
Enter fullscreen mode Exit fullscreen mode

Understanding the -map Syntax

The -map argument follows a consistent pattern: input_file:stream_type:stream_index.

Specifier Meaning
0:v All video streams
0:a:0 First audio stream
0:a:1 Second audio stream
0:s All subtitle streams

Selecting Specific Streams

Keep only video and the first audio track:

ffmpeg -i input.mkv -map 0:v -map 0:a:0 -c copy output.mp4
Enter fullscreen mode Exit fullscreen mode

Grab only the second audio track:

ffmpeg -i input.mkv -map 0:a:1 -c:a copy stereo-audio.aac
Enter fullscreen mode Exit fullscreen mode

Discarding Streams

Negative mapping with -map -:

ffmpeg -i input.mkv -map 0 -map -0:s -c copy output.mp4
Enter fullscreen mode Exit fullscreen mode

Common Pitfalls

  • Forgetting that -map disables automatic selection
  • Using absolute indices instead of type specifiers
  • Mapping subtitle streams into MP4 (use MKV instead)

Using -map with an API

curl -X POST https://api.ffmpeg-micro.com/v1/transcodes \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "inputs": [{"url": "https://storage.example.com/multi-track.mkv"}],
    "outputFormat": "mp4",
    "options": [
      {"option": "-map", "argument": "0:v"},
      {"option": "-map", "argument": "0:a:1"}
    ]
  }'
Enter fullscreen mode Exit fullscreen mode

Read the full post with all examples

Top comments (0)