FFmpeg `-map` Explained: Precisely Select Video, Audio, and Subtitle Streams

A practical guide to FFmpeg `-map`: stream naming, multi-input composition, negative mapping, and optional mapping.

In multi-audio and multi-subtitle workflows, -map is one of FFmpeg’s most important and most frequently misused options.

If you don’t explicitly specify -map, FFmpeg auto-selects streams using default rules, and the output is often not what you expect. For example:

  • Subtitles disappear after export
  • The wrong language track is selected
  • Unwanted data streams are included

This article uses common real-world scenarios to explain how -map works.

First, Understand What a “Stream” Is

A container file (such as mp4 or mkv) usually contains multiple streams, including:

  • Video streams (v)
  • Audio streams (a)
  • Subtitle streams (s)
  • Attachment/data streams (fonts, cover art, chapters, etc.)

You can inspect streams with ffprobe:

1
ffprobe -hide_banner input.mkv

Basic -map Syntax

Most common pattern:

1
-map input_index[:stream_type][:stream_index]

Examples:

  • 0:v: all video streams from the 1st input
  • 0:a:0: the 1st audio stream from the 1st input
  • 1:s:1: the 2nd subtitle stream from the 2nd input

Notes:

  • input_index starts from 0, based on -i order
  • stream_index also starts from 0

Practical Examples

1) Video from A, audio from B

1
2
3
4
ffmpeg -i english.mp4 -i french.mp3 \
  -map 0:v:0 -map 1:a:0 \
  -c:v copy -c:a aac \
  french.mp4

Meaning:

  • Use the first video stream from english.mp4
  • Use the first audio stream from french.mp3
  • Merge into french.mp4

2) Keep all streams from input 1, then add one more audio track

1
2
3
4
ffmpeg -i english.mp4 -i french.mp3 \
  -map 0 -map 1:a:0 \
  -c copy \
  english-french.mp4

Meaning:

  • -map 0 keeps all streams from the first input
  • Then append the first audio stream from the second input

Two Useful Advanced Tricks

1) Negative mapping: exclude unwanted streams

For example, keep everything from input 1 but remove its second audio stream:

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

2) Optional mapping: don’t fail when a stream is missing

If some files may not have subtitles, use ?:

1
ffmpeg -i input.mp4 -map 0:v -map 0:a -map 0:s? -c copy output.mp4

0:s? means: map subtitles if present; otherwise skip without error.

Common Pitfalls

  1. Once you use -map, FFmpeg stops automatic stream selection, so you must map everything you need.
  2. -c copy only remuxes without transcoding. If the target container doesn’t support a codec, it still fails.
  3. With multiple inputs, index mistakes are common. Input indices are determined only by -i order.
  4. For robust scripts, inspect with ffprobe first, then generate -map dynamically.

Summary

The core idea of -map is simple: explicitly tell FFmpeg which input to use, what stream type to pick, and which stream index to select.

Once you master this, you can reliably handle complex cases like multi-audio, multi-subtitle, and cross-file stream composition.

记录并分享
Built with Hugo
Theme Stack designed by Jimmy