Skip to content

Instantly share code, notes, and snippets.

@ivuorinen
Created January 30, 2015 07:31
Show Gist options
  • Save ivuorinen/5e0a4bf151171918e34a to your computer and use it in GitHub Desktop.
Save ivuorinen/5e0a4bf151171918e34a to your computer and use it in GitHub Desktop.
MKV Subtitle extraction
#!/bin/bash
# Ismo Vuorinen <ismo@ivuorinen.net>, 2014
SCRIPTNAME=`basename $0`
# If no directory is given, work in local dir
if [ "$1" = "" ]; then
DIR="."
else
DIR="$1"
fi
if [[ -n `which mkvextract` ]] && [[ -n `which mkvmerge` ]] && [[ -n `which vobsub2srt` ]]; then
# Extract subtitles
find "$DIR" -type f -name '*.mkv' | while read f; do
mkvmerge --identify-verbose "$f" | grep subtitles | while read sub; do
[[ "$sub" =~ S_TEXT/ASS ]] && ext=ass || ext=srt
track=$(awk -F '[ :]' '{print $3}' <<< $sub)
lang=$(awk -F '[ :]' '{print $18}' <<< $sub)
# We go through the first time
if [ ! -f "${f%.mkv}_$lang.$ext" ]; then
mkvextract tracks "$f" "$track:${f%.mkv}_$lang.$ext"
fi
done
done
# Convert vobsub to srt
find "$DIR" -type f -name '*.idx' | while read f; do
fname=${f%.idx}
vobsub2srt "$fname"
# Remove the idx/sub files if we have srt
if [ -f "$fname.srt" ]; then
rm "$fname.idx"
rm "$fname.sub"
fi
done
exit 0
else
echo "! Could not find either mkvtoolnix tools or vobsub2srt"
exit 127
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment