FFmpeg - Selecting streams: Difference between revisions

From PiRho Knowledgebase
Jump to navigationJump to search
mNo edit summary
Dex (talk | contribs)
No edit summary
 
Line 1: Line 1:
[[Category:FFmpeg]]
[[Category:FFmpeg]]
The <code>-map</code> option is used to choose which streams from the input(s) should be included in the output(s). The <code>-map</code> option can also be used to exclude specific streams with negative mapping. When the <code>-map</code> option is used, only the selected streams will be included in the output.<br/>
The <code>-map</code> option in FFmpeg gives you precise control over which input streams are included in the output. When <code>-map</code> is used, FFmpeg will include *only* the streams you explicitly specify. This allows you to choose, rearrange, or exclude streams during transcoding or remuxing.
The code below selects the 1st video stream from the 1st input file and the 1st audio stream in the 2nd input file:<br/>
 
<code>ffmpeg -i <input.mp4> -i <input.mkv> -map 0:v:0 -map 1:a:0 <output.file></code><br/>
== Why Stream Mapping Matters ==
Reference: [[https://trac.ffmpeg.org/wiki/Map FFmpeg: Selecting streams with the -map option]]
 
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 <code>-map</code> option gives you explicit, predictable control for all such scenarios.
 
== Basic Usage ==
 
The <code>-map</code> option follows the format:
 
<code> -map &lt;input_index&gt;:&lt;stream_type&gt;:&lt;stream_index&gt; </code>
 
Where:
 
* <code>input_index</code> — zero‑based index of the input file 
* <code>stream_type</code> — <code>v</code> (video), <code>a</code> (audio), <code>s</code> (subtitle), <code>d</code> (data), etc.
* <code>stream_index</code> — zero‑based index of that stream type within the input 
 
Example stream references:
 
* <code>0:v:0</code> → first video stream from the first input 
* <code>1:a:2</code> → third audio stream from the second input 
* <code>0:s</code> → 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**
 
<code>
ffmpeg -i input.mp4 -i input.mkv \
  -map 0:v:0 \
  -map 1:a:0 \
  output.file
</code>
 
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:
 
<code>
-map -0:s
</code>
 
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 ===
 
<code>
ffmpeg -i movie.mkv \
  -map 0:v \
  -map 0:a:1 \
  -c copy output.mp4
</code>
 
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:
 
<code>
ffmpeg -i video.mp4 -i high_quality_audio.wav \
  -map 0:v \
  -map 1:a \
  output.mp4
</code>
 
=== 3. Extract a Single Stream ===
 
Extract only subtitles:
 
<code>
ffmpeg -i input.mkv -map 0:s:0 subs.srt
</code>
 
Extract only audio:
 
<code>
ffmpeg -i input.mp4 -map 0:a:0 audio.aac
</code>
 
=== 4. Reorder Output Streams ===
 
<code>
ffmpeg -i input.mkv \
  -map 0:a \
  -map 0:v \
  output.mkv
</code>
 
This puts audio *before* video — useful for players or pipelines that rely on stream order.
 
== Tips and Pitfalls ==
 
* If you use *any* <code>-map</code> option, FFmpeg disables all automatic selection. 
* <code>-map 0</code> means “include all streams from input 0.” 
* Use <code>-map -0:d</code> to remove data streams (some phones embed sensor data).
* Use <code>-c copy</code> when remuxing to avoid unnecessary transcoding. 
* Probe input streams using: 
  <code>ffprobe -hide_banner -i filename</code>
 
== References ==
 
* FFmpeg Official Wiki – ''Selecting streams with the -map option'':
  [[https://trac.ffmpeg.org/wiki/Map]]

Latest revision as of 13:49, 14 March 2026

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 file
  • stream_typev (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 input
  • 1:a:2 → third audio stream from the second input
  • 0: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* -map option, FFmpeg disables all automatic selection.
  • -map 0 means “include all streams from input 0.”
  • Use -map -0:d to remove data streams (some phones embed sensor data).
  • Use -c copy when 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]]