-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_subtitles.sh
33 lines (28 loc) · 1019 Bytes
/
extract_subtitles.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/bin/bash
# Extract subtitles from each MKV file in the given directory
# If no directory is given, work in local dir
if [ "$1" = "" ]; then
DIR="."
else
DIR="$1"
fi
# Get all the MKV files in this dir and its subdirs
find "$DIR" -type f -name '*.mkv' | while read filename
do
# 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
# 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"
# add language check if you like e.g.:
# if [ "$language" == "ger" ] || [ "$language" == "chiSimplified" ]; then
`mkvextract tracks "$filename" $tracknumber:"$subtitlename.srt" > /dev/null 2>&1`
`chmod g+rw "$subtitlename.srt"`
done
done