Skip to content

Instantly share code, notes, and snippets.

@wernersbacher
Forked from cinatic/extractMkvSubtitles.sh
Last active December 13, 2022 19:00
Show Gist options
  • Save wernersbacher/582a2e6395ccf9507e787bdfa5a1f33e to your computer and use it in GitHub Desktop.
Save wernersbacher/582a2e6395ccf9507e787bdfa5a1f33e to your computer and use it in GitHub Desktop.
Extract Subtitles from mkv
#!/bin/bash
# Extract SRT subtitles from each MKV file in the given directory
# call script.sh <path> <language-searchtag>
# If no directory is given, work in local dir
if [ "$1" = "" ]; then
DIR="."
else
DIR="$1"
fi
searchname="$2"
# Get all the MKV files in this dir and its subdirs
find "$DIR" -type f -name '*.mkv' | while read filename
do
echo Scanning $filename
# Find out which tracks contain the subtitles
mkvmerge -J "$filename" | jq -r '
.tracks |
map((.id | tostring) + " " + .properties.language + .properties.track_name + " " + .codec) |
join("\n")' | grep 'SubRip' | while read subline
do
#echo subline $subline
# Grep the number of the subtitle track
tracknumber=`echo $subline | cut -d' ' -f1`
language=`echo $subline | cut -d' ' -f2`
# Get base name for subtitle
subtitlename="${filename%.*}_$language"
#echo found $language
# add language check if you like e.g.:
echo Found $language
if [[ "$language" == "$searchname"* ]]
then
echo extracting srt..
mkvextract tracks "$filename" $tracknumber:"$subtitlename.srt" > /dev/null 2>&1
chmod g+rw "$subtitlename.srt"
else
echo skipping.
fi
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment