Extract a Specific Stream by Index Using FFmpeg
This guide explains how to isolate and extract a specific video,
audio, or subtitle stream from a media file using FFmpeg. By utilizing
the -map option, you can target a stream by its precise
index number and export it into a new file without needing to re-encode
the entire media container.
Step 1: Identify the Stream Index
Before extracting, you must find the index number of the stream you want. Run the following command to inspect your input file:
ffmpeg -i input.mp4In the output, look for the “Stream” lines. They will look similar to this:
Stream #0:0[0x1]: Video: h264 (High) ...
Stream #0:1[0x2](eng): Audio: aac (LC) ...
Stream #0:2[0x3](spa): Audio: aac (LC) ...
Stream #0:3[0x4](eng): Subtitle: mov_text ...
In this example, the video is stream 0:0, the English
audio is 0:1, the Spanish audio is 0:2, and
the subtitle is 0:3. The first number (0)
represents the input file, and the second number represents the stream
index.
Step 2: Extract the Stream Using the Map Option
To extract a specific stream, use the -map flag followed
by the input file index and stream index.
For example, to extract the Spanish audio stream (0:2)
and save it as an MP3 file, run:
ffmpeg -i input.mp4 -map 0:2 output.mp3Stream Extraction with Copying (No Re-encoding)
If you want to extract the stream in its original codec without
losing quality (and do it almost instantly), use the
-c copy (codec copy) flag:
ffmpeg -i input.mp4 -map 0:2 -c copy output.aac(Note: Ensure the output file extension matches the codec of the
stream you are extracting, such as .aac for AAC audio, or
.srt/.vtt for subtitles).
Extracting Multiple Specific Streams
You can also use multiple -map flags to extract more
than one specific stream into a new container. For example, to create a
new file containing only the video stream (0:0) and the
Spanish audio stream (0:2):
ffmpeg -i input.mp4 -map 0:0 -map 0:2 -c copy output_trimmed.mp4