Skip to content

Instantly share code, notes, and snippets.

@AndreiDuma
Created January 13, 2014 20:30
Show Gist options
  • Save AndreiDuma/8407525 to your computer and use it in GitHub Desktop.
Save AndreiDuma/8407525 to your computer and use it in GitHub Desktop.
A little script to help with extracting subtitles from .mkv files.
#!/bin/sh
if ! command -v mkvmerge mkvextract >/dev/null 2>&1; then
echo >&2 "Tools mkvmerge & mkvextract are required. Install package mkvtoolnix."
exit 1
fi
filename=$1
if [ ! -f $filename ]; then
echo "File not found."
exit 1
fi
filename_no_ext=$(echo $1 | sed -r 's/.mkv//')
sed_exp='s/.*([0-9]+):.*language:([a-z]+).*/\1:'$filename_no_ext'.\2.\1.srt/'
mkvmerge -I $filename | grep subtitles | sed -r $sed_exp | xargs mkvextract tracks $filename
@Seegras
Copy link

Seegras commented Jun 7, 2016

Fails when there are more than 10 tracks, fails if the subtitles are vobsub (because mkvextract will then ditch anything beyond the first dot, and make two files named .idx and .sub respectively -- probably a bug in mkvextract, it should split off at the last dot, not the first).

Here's a sed-line that fixes the first and works around the second.
sed_exp='s/Track\sID\s([0-9]{1,3}):.language:([a-z]+)./\1:'$filename_no_ext'-\1-\2/'

@Seegras
Copy link

Seegras commented Jun 8, 2016

And in order to make this work with filenames containing the path, you need to do this:

filename_no_ext=$(basename $1 .mkv)

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