-
Notifications
You must be signed in to change notification settings - Fork 0
/
pair-subs.sh
executable file
·67 lines (53 loc) · 1.44 KB
/
pair-subs.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
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
#!/usr/bin/env bash
THRES=20000
MOVIE_DIRS=(
/mnt/media/torrents
)
SUBS_DIRS=(
/mnt/media/tmp.*
)
# takes a movie file name and returns best possible subtittle with score
function pair_file {
local movie="$1"
local subs="$2"
local mdir="$(basename "$(dirname "$movie")")"
local mfile="$(basename "$movie")"
local dist1="$(tre-agrep -isB "$mdir/$mfile" "$subs")"
local dist2="$(tre-agrep -isB "$mfile" "$subs")"
if [ ${dist1%%:*} -lt ${dist2%%:*} ]; then
[ ${dist1%%:*} -lt $THRES ] && echo $dist1
else
[ ${dist2%%:*} -lt $THRES ] && echo $dist2
fi
}
function pair_all {
local subs="$(mktemp)"
local subfiles="$(mktemp)"
[ -z "${SUBS_DIRS[@]}" ] && return 1
find ${SUBS_DIRS[@]} -iname '*.srt' \
> "$subfiles"
cat "$subfiles" |
xargs -r -L 1 -d '\n' basename \
> "$subs"
if [ "$(cat "$subfiles" | wc -l)" -gt 0 ]; then
find ${MOVIE_DIRS[@]} -iname '*.avi' -o -iname '*.mkv' |
while read mfile; do
subfile="$(pair_file "$mfile" "$subs")"
if [ "$subfile" ]; then
subfile="$(grep "${subfile#*:}" "$subfiles")"
echo "
mv '$subfile' \\
'$(echo $mfile | sed 's/\.avi$\|\.mkv$//').srt'"
fi
done
fi
rm -f "$subs" "$subfiles"
}
scriptfile="$(mktemp)"
pair_all > "$scriptfile" || exit 1
if [ "$(cat "$scriptfile" | wc -l)" -gt 0 ]; then
vim "$scriptfile"
echo -n "Apply pairing ? "; read applyreq
[ $applyreq = yes ] && bash "$scriptfile"
fi
rm -f "$scriptfile"