-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_subtitles.txt
139 lines (124 loc) · 5.16 KB
/
extract_subtitles.txt
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/bin/bash
# This script is a modified version of neik's script at https://emby.media/community/index.php?/topic/83855-script-to-extract-subs/&do=findComment&comment=878783, cleaned up for my usage.
DEBUG=false
DOCKER=true
lang_list=(eng)
DIR="/mnt/user/Anime"
mkvinfocmd=(docker exec MKVToolNix /usr/bin/mkvinfo)
mkvmergecmd=(docker exec MKVToolNix /usr/bin/mkvmerge)
mkvextractcmd=(docker exec MKVToolNix /usr/bin/mkvextract)
jsonoutput() {
"${mkvmergecmd[@]}" -J "$1" | jq -r '.tracks |map((.id | tostring) + "," + .type + "," + .properties.language + "," + (.properties.forced_track|tostring) + "," + .properties.track_name + "," + .properties.codec_id + "," + .properties.encoding) |join("\n")'
}
info_log() {
printf "Log: %s \n" "$1"
}
debug_log() {
if [ $DEBUG = "true" ]; then
printf "Debug: %S \n" "$1"
fi
}
movefile() {
if [ ! -f "$2" ]; then
mv "$1" "$2" #>/dev/null 2>&1
fi
}
echo "Working from directory $DIR"
while IFS= read -r -d '' MKVFILE; do
MKV=${MKVFILE/\/mnt\/user//\/storage/}
BASENAME=$(basename "$MKV" ".mkv")
DIRNAME=$(dirname "${MKV}")
OUTPUTDIR=$(dirname "${MKVFILE}")
NEWBASEFILE=${BASENAME// /_}
NEWNAME="${NEWBASEFILE}.***"
if [ "$DEBUG" = "true" ]; then
debug_log "MKVFILE: $MKV"
debug_log "BASENAME: ${BASENAME}"
debug_log "DIRNAME: $DIRNAME"
debug_log "NEWBASEFILE: $NEWBASEFILE"
debug_log "NEWNAME: $NEWNAME"
"${mkvinfocmd[@]}" -r "$DIRNAME/$BASENAME.log" "$MKVFILE"
fi
subtitlename=${BASENAME%.*}
# Test if an english subtitle exist
info_log "No subtitles found for $BASENAME"
# Number of matching embedded subtitle tracks
subnum=$(jsonoutput "$MKV" | grep -c subtitles)
info_log "Number of matching subtitles is $subnum"
for lang in "${lang_list[@]}"; do
# Find out which tracks contain the subtitles
info_log "Checking $BASENAME for $lang subtitle tracks"
jsonoutput "$MKV" | grep subtitles | grep "$lang" | while read -r subline; do
# Grep the number of the subtitle track
tracknumber=$(echo "$subline" | cut -d "," -f1)
# Grep the subtitle format
format=$(echo "$subline" | cut -d "," -f6)
forced=$(echo "$subline" | cut -d "," -f4)
language=$(echo "$subline" | cut -d "," -f3)
track_name=$(echo "$subline" | cut -d "," -f5)
debug_log "Subline: $subline"
debug_log "format: ${format}"
debug_log "forced: $forced"
debug_log "language: $language"
debug_log "track_name: $track_name"
case $format in
S_TEXT/UTF8)
format=".srt"
;;
S_TEXT/SSA)
format=".ssa"
;;
S_TEXT/ASS)
format=".ass"
;;
S_TEXT/USF)
format=".usf"
;;
S_VOBSUB)
format=""
;;
S_HDMV/PGS)
format=".sup"
;;
*) ;;
esac
# Get base name for subtitle
subtitlename=${BASENAME%.*}
# Extract the track to a .tmp file
info_log "Extracting subtitle track to temporary file for $BASENAME"
"${mkvextractcmd[@]}" tracks "$MKV" "${tracknumber}:${subtitlename}${format}.tmp" >/dev/null 2>&1
if [ $DOCKER = "true" ]; then
docker cp MKVToolNix:/tmp/"${subtitlename}${format}.tmp" "${subtitlename}${format}.tmp"
docker exec MKVToolNix rm -rf "/tmp/${subtitlename}${format}.tmp"
fi
if [ -f "${subtitlename}${format}.tmp" ]; then
chmod g+rw "$subtitlename$format.tmp"
fi
if [ -f "${subtitlename}${format}.tmp" ]; then
debug_log "File exist: $subtitlename$format.tmp"
if [ -n "$track_name" ]; then
subname="${track_name//[^A-Za-z0-9._-]/_}"
else
subname="$language"
fi
if [ "$forced" = "true" ]; then
# rename forced subtitle for plex
info_log "Subtitle track appears to be forced, renaming as ${subname}.forced$format"
if [ ! -f "$OUTPUTDIR/$subtitlename.${subname}.forced$format" ]; then
movefile "${subtitlename}${format}.tmp" "$OUTPUTDIR/${subtitlename}.${subname}.forced$format"
else
rm -rf "${subtitlename}${format}.tmp"
fi
else
# Rename in .$lang_en.$format for plex
info_log "Subtitle track appears to be full, renaming as ${subname}${format}"
if [ ! -f "$OUTPUTDIR/$subtitlename.${subname}${format}" ]; then
movefile "${subtitlename}${format}.tmp" "$OUTPUTDIR/${subtitlename}.${subname}${format}"
else
rm -rf "${subtitlename}${format}.tmp"
fi
fi
fi
done
done
done < <(find "$DIR" -size +180M -name '*.mkv' -print0 | sort -n)