How to Extract FFmpeg Stream by Metadata Tag
This article explains how to isolate and extract a specific audio, video, or subtitle stream from a media file using its metadata tags in FFmpeg. Instead of relying on unpredictable stream index numbers, you will learn how to use FFmpeg’s stream specifiers to target metadata keys like language or title, allowing for precise and automated stream extraction.
The Basic Syntax
To select a stream by its metadata, you use the -map
option combined with the metadata specifier
m:key:value.
The basic command structure is:
ffmpeg -i input.mkv -map 0:m:key:value -c copy output.mp40: Specifies the first input file.m: Indicates that you are matching by metadata.key: The metadata tag name (e.g.,language,title).value: The specific value of the tag you want to extract.-c copy: Copies the stream without re-encoding, ensuring a fast and lossless extraction.
Practical Examples
1. Extract Audio by Language Tag
If you have a video with multiple language tracks and want to extract
only the English audio stream, you can target the language
tag set to eng:
ffmpeg -i input.mkv -map 0:a:m:language:eng -c copy english_audio.m4aNote: Adding :a before :m restricts the
search specifically to audio streams, preventing the extraction of
subtitles that might also share the eng language
tag.
2. Extract Subtitles by Language Tag
To extract only the Spanish subtitles from a multi-language MKV file:
ffmpeg -i input.mkv -map 0:s:m:language:spa -c copy spanish_subtitles.srtNote: :s restricts the selection to subtitle streams
only.
3. Extract a Stream by Title Tag
If a stream has a specific title, such as “Director’s Commentary”,
you can extract it by targeting the title metadata key:
ffmpeg -i input.mkv -map 0:m:title:"Director's Commentary" -c copy commentary.aacHow to Find Metadata Keys and Values
If you are unsure what metadata tags exist in your file, use the
ffprobe command to inspect the streams:
ffprobe -show_streams input.mkvLook for the TAG: lines in the output to identify the
exact keys (like language or title) and values
(like eng or Stereo) associated with each
stream.