C.W.K.
Stream
Lesson 07 of 10 · published

Streams and Stream Specifiers

~10 min · streams, selectors, mapping

Level 0Viewer
0 XP0/73 lessons0/15 achievements
0/100 XP to next level100 XP to go0% complete

One file, many streams

A media container holds streams: typically one video, one audio, sometimes a subtitle track, occasionally a 'data' stream (timecode, metadata). A Blu-ray rip might have 1 video + 4 audio (different languages) + 8 subtitle streams.

FFmpeg uses stream specifiers to point at specific streams. The grammar is small but you'll hit it constantly.

The grammar

  • v = video, a = audio, s = subtitle, d = data, t = attachment.
  • v:0 = the first video stream. a:1 = the second audio stream.
  • 0:v:0 = the first video stream of input #0 (when you have multiple inputs).
  • -map 0:a:1 = take audio stream #1 from input #0 and put it in the output.
  • -c:v libx264 = use libx264 for all video streams in the output.
  • -c:a:0 copy -c:a:1 aac = copy the first audio stream, re-encode the second to AAC.

By default if you don't -map anything, FFmpeg picks the 'best' video and 'best' audio (highest bitrate / preferred language). When you have multi-track files, be explicit — auto-pick is the source of half the 'wrong audio language' bugs in the world.

Code

List streams, then explicitly map·bash
# What streams does this file have?
ffprobe -v error -show_entries stream=index,codec_type,codec_name,channels,channel_layout \
  -of csv=p=0 input.mkv

# Output (example):
# 0,video,h264,,
# 1,audio,ac3,6,5.1
# 2,audio,aac,2,stereo
# 3,subtitle,subrip,,
# 4,subtitle,subrip,,

# Take video, the SECOND audio (stereo AAC), and the FIRST subtitle
ffmpeg -i input.mkv \
  -map 0:v:0 -map 0:a:1 -map 0:s:0 \
  -c copy output.mkv
Per-stream encoding choices·bash
# Video re-encoded H.265, audio kept untouched, subs dropped
ffmpeg -i input.mkv \
  -map 0:v -map 0:a \
  -c:v libx265 -crf 22 \
  -c:a copy \
  output.mkv

External links

Exercise

Find or download an MKV with multiple audio tracks (a Blu-ray rip, an anime release, a multi-language YouTube download). List its streams with ffprobe. Then write an ffmpeg command that creates a new MP4 with: video copied, only the English audio, no subtitles. Verify with ffprobe on the output.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.