This repository has been archived by the owner on Nov 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
mkv-extract-subtitles
executable file
·93 lines (79 loc) · 2.37 KB
/
mkv-extract-subtitles
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/bin/bash
#
# Version 1
#
# (C) 2016 Daniel Faust <[email protected]>
#
# A BASH script that extracts subtitles from a matroska file.
#
# Requires mkvmerge, mkvextract
#
# Usage:
# chmod +x mkv-extract-subtitles
# ./mkv-extract-subtitles "My File.mkv"
#
# This will create a new file for each subtitle in the current directory.
#
# For batch processing all .mkv files in the current directoy
# copy mkv-extract-subtitles to a directory in your PATH and execute:
#
# find -iname "*.mkv" -exec mkv-extract-subtitles '{}' \;
if [[ "$1" == "" ]] || [[ "$1" == "--help" ]] || [[ "$1" == "-h" ]]; then
echo
echo "Process single file:"
echo "mkv-extract-subtitles file.mkv"
echo
echo "Process multiple files:"
echo "find -iname \"*.mkv\" -exec mkv-extract-subtitles '{}'\;"
echo
exit
fi
hash mkvmerge 2>/dev/null || { echo >&2 "Error: Command mkvmerge not found"; exit 1; }
hash mkvextract 2>/dev/null || { echo >&2 "Error: Command mkvextract not found"; exit 1; }
file_name=$1
IFS='
'
tracks=$(LANG=en_US.utf8 LANGUAGE=en_US.utf8 mkvmerge --identify-verbose "$file_name" | tail --lines=+2)
for track in $tracks; do
track_id=""
[[ "$track" =~ Track\ ID\ ([0-9]+) ]] &&
track_id=${BASH_REMATCH[1]}
[[ "$track" =~ language:([a-z]+) ]] &&
language=${BASH_REMATCH[1]}
[[ "$track" =~ track_name:([^ ]+) ]] &&
track_name=${BASH_REMATCH[1]}
[[ "$track" =~ Track\ ID\ [0-9]+:\ ([a-z]*) ]] &&
track_type=${BASH_REMATCH[1]}
ext=""
if [[ "$track" =~ S_TEXT/UTF8 ]]; then
ext="srt"
elif [[ "$track" =~ S_TEXT/ASS ]]; then
ext="ssa"
elif [[ "$track" =~ S_TEXT/USF ]]; then
ext="usf"
elif [[ "$track" =~ S_VOBSUB ]]; then
ext="sub"
elif [[ "$track" =~ S_HDMV/PGS ]]; then
ext="sup"
fi
if [[ "$track_id" == "" ]]; then
continue
fi
if [[ "$track_type" != "subtitles" ]]; then
continue
fi
name="$track_id"
if [[ "$language" != "" ]]; then
name="$name, $language"
fi
if [[ "$track_name" != "" ]]; then
track_name=${track_name/\//_} # replace '/' with '_'
track_name=${track_name/\\s/\ } # replace '\s' with ' '
track_name=${track_name/\\2/\"} # replace '\2' with '"'
track_name=${track_name/\\c/:} # replace '\c' with ':'
track_name=${track_name/\\h/#} # replace '\h' with '#'
# track_name=${track_name/\\\\/\\} # replace '\\' with '\' doesn't work
name="$name, $track_name"
fi
mkvextract tracks "$file_name" "$track_id:${file_name%.*} [$name].$ext"
done