Skip to content

Instantly share code, notes, and snippets.

@cdeath
Last active January 15, 2024 15:28
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save cdeath/1f8fdd96ead3829465897e413188190a to your computer and use it in GitHub Desktop.
Save cdeath/1f8fdd96ead3829465897e413188190a to your computer and use it in GitHub Desktop.
extract stuff from .mkv with ffmpeg

Extract stuff from .mkv with ffmpeg

Full documentation: https://ffmpeg.org/ffmpeg.html

List all tracks

ffmpeg -i input.mkv

track indexes are zero-based.

Track types

v - video
a - audio
s - subtitles
t - attachments
d - data
m - metadata

Pick tracks

syntax

ffmpeg -i input.mkv -map 0:type -map 0:type -c copy output.mkv
ffmpeg -i input.mkv -map 0:type:index -map 0:type:index -c copy output.mkv

examples

pick all video tracks, second audio track and third subtitle track:

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

only pick first video, audio and subtitle tracks but keep all attachments:

ffmpeg -i "$i" -map 0:v:0 -map 0:a:0 -map 0:s:0 -map 0:t -c copy

Omit tracks (negative mapping)

syntax

ffmpeg -i input.mkv -map 0 -map -0:type:index -c output.mkv

examples

keep all tracks except first audio and first subtitle tracks:

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

Extract subtitle track

syntax

ffmpeg -i input.mkv -map 0:m:key:value -c:s copy|format output.srt

examples

ffmpeg -i input.mkv -map 0:m:language:eng -c:s copy output.srt
ffmpeg -i input.mkv -map 0:m:title:SDH -c:s copy output.srt
ffmpeg -i input.mkv -map 0:m:key:value -c:s srt output.srt

Batch operations

extract subtitles

for i in *.mkv; do ffmpeg -i "$i" -map 0:m:key:value -c:s copy "${i%.*}.srt"; done

pick tracks

for i in *.mkv; do ffmpeg -i "$i" -map 0:v -map 0:a:0 -map 0:s:0 -c copy "_${i}"; done

omit tracks

for i in *.mkv; do ffmpeg -i "$i" -map 0 -map -0:a:0 -c copy "_${i}"; done

Show only stats, warnings and errors

ffmpeg -v 24 -stats ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment