FFmpeg - Selecting streams
The -map option in FFmpeg gives you precise control over which input streams are included in the output. When -map is used, FFmpeg will include *only* the streams you explicitly specify. This allows you to choose, rearrange, or exclude streams during transcoding or remuxing.
Why Stream Mapping Matters
By default, FFmpeg attempts to “do the right thing” by automatically selecting one stream per type (e.g., one video, one audio, one subtitle). However, many situations require manual control:
- Multiple audio or subtitle tracks
- Mixing streams from multiple input files
- Removing unwanted metadata, data streams, or alternate tracks
- Re‑ordering streams
- Ensuring exact, deterministic output for automation or workflows
The -map option gives you explicit, predictable control for all such scenarios.
Basic Usage
The -map option follows the format:
-map <input_index>:<stream_type>:<stream_index>
Where:
input_index— zero‑based index of the input filestream_type—v(video),a(audio),s(subtitle),d(data), etc.stream_index— zero‑based index of that stream type within the input
Example stream references:
0:v:0→ first video stream from the first input1:a:2→ third audio stream from the second input0:s→ all subtitle streams from the first input
Selecting Streams from Multiple Inputs
The following command selects:
- the **first video stream** from the **first input file**
- the **first audio stream** from the **second input file**
ffmpeg -i input.mp4 -i input.mkv \
-map 0:v:0 \
-map 1:a:0 \
output.file
Only the chosen streams are included in the output. All others are ignored.
Negative Mapping (Excluding Streams)
FFmpeg also supports “negative mapping” to remove unwanted streams:
-map -0:s
This example removes **all subtitle streams** from input 0, while keeping other mapped streams intact.
Negative mapping is especially useful when:
- Automatically selecting streams but excluding one type
- Removing embedded cover art
- Removing unwanted data streams generated by screen recorders or mobile devices
Common Use Cases
1. Remux Only Certain Streams
ffmpeg -i movie.mkv \
-map 0:v \
-map 0:a:1 \
-c copy output.mp4
This copies:
- the video
- the *second* audio track
…without transcoding.
2. Combine Audio and Video from Different Sources
Useful when replacing or enhancing audio tracks:
ffmpeg -i video.mp4 -i high_quality_audio.wav \
-map 0:v \
-map 1:a \
output.mp4
3. Extract a Single Stream
Extract only subtitles:
ffmpeg -i input.mkv -map 0:s:0 subs.srt
Extract only audio:
ffmpeg -i input.mp4 -map 0:a:0 audio.aac
4. Reorder Output Streams
ffmpeg -i input.mkv \
-map 0:a \
-map 0:v \
output.mkv
This puts audio *before* video — useful for players or pipelines that rely on stream order.
Tips and Pitfalls
- If you use *any*
-mapoption, FFmpeg disables all automatic selection. -map 0means “include all streams from input 0.”- Use
-map -0:dto remove data streams (some phones embed sensor data). - Use
-c copywhen remuxing to avoid unnecessary transcoding. - Probe input streams using:
ffprobe -hide_banner -i filename
References
- FFmpeg Official Wiki – Selecting streams with the -map option:
[[1]]