ffmpegstream-selectionmulti-track-audioffprobebucket-doc

FFmpeg -map Flag Explained: Stream Selection, Multi-Track Audio, and Output Mapping

·Javid Jamae·9 min read
FFmpeg -map Flag Explained: Stream Selection, Multi-Track Audio, and Output Mapping

You run ffmpeg -i input.mkv output.mp4 and somehow your output is missing the commentary audio track. Or it grabbed the wrong subtitle language. Or it silently dropped one of your three audio streams into a file that only supports two. FFmpeg's automatic stream selection does its best, but "best" rarely matches what you actually want when you're working with multi-track files.

The -map flag gives you explicit control over which streams go into your output. But the official docs read like a specification, not a tutorial. This post walks through how -map actually works, with examples you can copy and run.

Quick answer: The -map flag tells FFmpeg exactly which streams to include in your output. -map 0:a:1 means "from the first input file (0), take the second audio stream (a:1)." Without -map, FFmpeg picks one "best" stream per type automatically, which often isn't what you want.

How FFmpeg Picks Streams Without -map

When you don't use -map, FFmpeg applies its automatic stream selection algorithm. It picks one video stream (the highest resolution), one audio stream (the most channels), and one subtitle stream (the first it finds). Everything else gets dropped.

For a simple file with one video and one audio track, automatic selection works fine. It falls apart when your file has multiple audio tracks (think different languages or a commentary track), multiple subtitle streams, or data streams you need to preserve.

Identify Your Streams First with ffprobe

Before you can select streams, you need to know what's in the file. That's what ffprobe is for.

ffprobe -v error -show_entries stream=index,codec_type,codec_name,channels -of json input.mkv

A typical multi-track MKV might return something like this:

{
  "streams": [
    { "index": 0, "codec_type": "video", "codec_name": "h264" },
    { "index": 1, "codec_type": "audio", "codec_name": "aac", "channels": 6 },
    { "index": 2, "codec_type": "audio", "codec_name": "aac", "channels": 2 },
    { "index": 3, "codec_type": "subtitle", "codec_name": "srt" },
    { "index": 4, "codec_type": "subtitle", "codec_name": "srt" }
  ]
}

Five streams, five indices. The -map flag uses these indices to select exactly what you want.

Understanding the -map Syntax

The -map argument follows a consistent pattern: input_file:stream_type:stream_index.

SpecifierMeaning
`0:0`First input file, first stream (absolute index)
`0:v`First input file, all video streams
`0:a`First input file, all audio streams
`0:a:0`First input file, first audio stream
`0:a:1`First input file, second audio stream
`0:s`First input file, all subtitle streams
`1:v`Second input file, all video streams

The first number is always the input file index, starting at 0. The letter (v, a, s) is a stream type specifier. The final number is the stream index within that type.

One thing that trips people up: 0:1 and 0:a:0 might refer to the same stream if the audio stream happens to be at absolute index 1. But 0:a:0 is safer because it says "first audio stream" regardless of where it sits in the overall stream order. If someone re-muxes the file and the stream order changes, the type-based specifier still works.

Selecting Specific Streams

Keep only video and the first audio track (drop everything else):

ffmpeg -i input.mkv -map 0:v -map 0:a:0 -c copy output.mp4

Each -map flag adds one stream (or group of streams) to the output. No -map for subtitles means no subtitles in the output.

Grab only the second audio track (the stereo one):

ffmpeg -i input.mkv -map 0:a:1 -c:a copy stereo-audio.aac

This extracts just the stereo commentary track from the example above. If you've read the guide on extracting audio from video, you've seen -vn used to drop video. Using -map is more precise, especially when the file has multiple audio tracks and -vn alone would still grab the wrong one.

Keep all audio tracks but drop subtitles:

ffmpeg -i input.mkv -map 0:v -map 0:a -c copy output.mkv

-map 0:a without a stream index selects every audio stream from the first input.

Discarding Streams You Don't Want

There are two approaches to excluding streams. You can either only map what you want (as shown above), or you can use negative mapping to exclude specific streams from a broader selection.

Negative mapping with -map -:

ffmpeg -i input.mkv -map 0 -map -0:s -c copy output.mp4

-map 0 selects all streams from the first input. -map -0:s then removes all subtitle streams. The result: video and all audio, no subtitles. This is useful when a file has many streams and you want to exclude just one or two types rather than listing everything you want to keep.

Drop a specific audio track while keeping the rest:

ffmpeg -i input.mkv -map 0 -map -0:a:1 -c copy output.mkv

This keeps everything except the second audio stream.

Mapping Streams Across Multiple Inputs

When you have multiple input files, -map is how you tell FFmpeg which input contributes which streams. This is essential for merging audio and video from separate files.

ffmpeg -i video.mp4 -i narration.wav \
  -map 0:v -map 1:a \
  -c:v copy -c:a aac output.mp4

-map 0:v takes video from the first input. -map 1:a takes audio from the second input. Without -map, FFmpeg would only use the "best" audio it finds across all inputs, which might be the original video's audio instead of your narration file.

Mapping to Multiple Outputs

A single FFmpeg command can write to multiple output files. Each output gets its own set of -map flags.

ffmpeg -i input.mkv \
  -map 0:v -map 0:a:0 -c copy english.mp4 \
  -map 0:v -map 0:a:1 -c copy spanish.mp4

This reads the source once and writes two MP4 files, each with the same video but a different audio track. It's much faster than running FFmpeg twice, because the input only gets demuxed once. You'll see this pattern a lot in filter_complex workflows where a single input fans out into multiple processed outputs.

Common Pitfalls

Forgetting that -map disables automatic selection. The moment you add one -map flag, FFmpeg stops selecting streams automatically. If you map only audio, you won't get video unless you explicitly map that too. This catches everyone at least once.

Using absolute indices instead of type specifiers. -map 0:1 works, but it breaks if the stream order changes. -map 0:a:0 is resilient to reordering. Always prefer type specifiers when you can.

Mapping subtitle streams into MP4. MP4 containers only support a handful of subtitle codecs (like mov_text). If you try to stream-copy SRT subtitles into an MP4, FFmpeg will either error out or silently drop them. Use MKV if you need to carry subtitles.

Not checking stream contents with ffprobe first. Stream index 2 isn't always what you think it is. Run ffprobe before building your command. One quick check saves you from debugging a silent wrong-stream problem.

Combining -map with -vn/-an/-sn. These flags interact in ways that aren't obvious. If you use -map 0 and then -an, the audio streams get removed. But if you use -map 0:a:0 and -an together, you've selected an audio stream and then told FFmpeg to drop all audio. The result is no audio. Pick one approach and stick with it.

Using -map with the FFmpeg Micro API

If you're building an application that needs to select specific streams programmatically, you can pass -map flags directly through the FFmpeg Micro API's options array.

Extract the second audio track from a multi-track video:

curl -X POST https://api.ffmpeg-micro.com/v1/transcodes \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "inputs": [{"url": "https://storage.example.com/multi-track.mkv"}],
    "outputFormat": "mp4",
    "options": [
      {"option": "-map", "argument": "0:v"},
      {"option": "-map", "argument": "0:a:1"},
      {"option": "-c:v", "argument": "copy"},
      {"option": "-c:a", "argument": "aac"}
    ]
  }'

Each -map flag is its own entry in the options array. You can stack as many as you need, just like you would on the command line. The API runs FFmpeg on cloud infrastructure, so you don't need FFmpeg installed locally or worry about managing processes. Grab a free API key to try it.

FAQ

What happens if I use -map with a stream index that doesn't exist?

FFmpeg will exit with an error: Stream map '0:a:5' matches no streams. You can add a trailing ? to make it optional: -map 0:a:5? will silently skip the mapping if the stream doesn't exist instead of failing.

Can I reorder streams with -map?

Yes. The order of your -map flags determines the stream order in the output. -map 0:a -map 0:v puts audio before video in the container. Most players don't care about order, but some tools and workflows expect video as the first stream.

How is -map different from -vn, -an, and -sn?

-vn, -an, and -sn are exclusion flags. They remove all video, audio, or subtitle streams from the output. -map is an inclusion flag. It explicitly adds specific streams. -map gives you finer control because you can select individual tracks within a type, not just include or exclude the entire type.

Does -map work with -c copy?

Yes. -map controls which streams are selected. -c copy controls whether those streams are re-encoded or copied as-is. They're independent. You can map specific streams and stream-copy them in a single command, like -map 0:v -map 0:a:0 -c copy, which is the fastest way to remux with specific tracks.

How do I map all streams from all inputs?

Use -map 0 for all streams from the first input, -map 1 for all streams from the second, and so on. There's no single flag for "all streams from all inputs," but chaining -map flags for each input achieves the same thing.

About Javid Jamae

Founder & CEO at FFmpeg Micro

Javid is a software engineer, author, and entrepreneur with over 25 years of professional software development experience across enterprise, startup, and consulting environments. He founded FFmpeg Micro to make video processing accessible to developers through a simple, automation-first REST API.

Software EngineeringVideo ProcessingFFmpegCloud ArchitectureAPI DesignAutomation

Ready to process videos at scale?

Start using FFmpeg Micro's simple API today. No infrastructure required.

Get Started Free