FFmpeg Map Streams to Multiple Output Files
This guide explains how to use the FFmpeg -map option to
extract and direct specific audio, video, or subtitle streams from a
source file into separate output files. You will learn the fundamental
syntax of stream selection and see practical, step-by-step examples of
how to split multi-stream media files in a single command.
Understanding FFmpeg Stream Mapping Syntax
FFmpeg uses the -map option to select which streams from
the input files should be sent to the output files. The basic syntax for
identifying a stream is:
input_file_index:stream_type:stream_index
- input_file_index: The index of the input file,
starting at
0for the first input. - stream_type: The type of stream, such as
vfor video,afor audio, orsfor subtitles. - stream_index: The index of the stream within that
type, starting at
0.
To write different streams to different files in a single command,
you must place the -map option immediately before the
output file it belongs to. FFmpeg applies mapping options to the output
file that directly follows them.
Example 1: Separating Video and Audio into Different Files
If you have an MP4 file and want to save the video stream to one file and the audio stream to another, use the following command:
ffmpeg -i input.mp4 -map 0:v:0 output_video.mp4 -map 0:a:0 output_audio.mp3How it works:
-i input.mp4: Specifies the input file (index0).-map 0:v:0 output_video.mp4: Maps the first video stream (0:v:0) tooutput_video.mp4.-map 0:a:0 output_audio.mp3: Maps the first audio stream (0:a:0) tooutput_audio.mp3.
Example 2: Splitting Multiple Audio Tracks
For media files containing multiple audio tracks (such as different languages), you can export each language track to its own audio file.
ffmpeg -i input.mkv -map 0:a:0 english.wav -map 0:a:1 spanish.wavHow it works:
-map 0:a:0 english.wav: Directs the first audio stream (usually the default language) toenglish.wav.-map 0:a:1 spanish.wav: Directs the second audio stream tospanish.wav.
Example 3: Extracting Subtitles, Audio, and Video Simultaneously
You can split a single file into three separate files containing the video, a specific audio track, and the subtitle track:
ffmpeg -i input.mkv \
-map 0:v:0 video_only.mp4 \
-map 0:a:0 audio_only.m4a \
-map 0:s:0 subtitles.srtHow it works:
-map 0:v:0 video_only.mp4: Extracts the first video stream without any audio.-map 0:a:0 audio_only.m4a: Extracts the first audio stream.-map 0:s:0 subtitles.srt: Extracts the first subtitle stream and converts it to SRT format.