-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
animegrep.sh
executable file
·162 lines (127 loc) · 4.01 KB
/
animegrep.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/bin/bash
# Copyright Daniel Jones and Ayane Satomi 2019
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Make sure the script fails if the other commands fail as well
set -eo pipefail
print_help() {
echo "$0 [-d <directory> | -f <file> ] -t <track> -w <WORD> -m -h "
echo ""
echo "animegrep is a bash script that extracts the subtitles from an mkv video file, greps them for a specified word, parses them and extracts only that time frame from the source video file"
echo ""
echo "-d <directory> sets a directory as your source media"
echo "-f <file> sets a file as your source media. You cannot set directory and files at the same time."
echo "-t <track> Not Documented. "
echo "-w <word> track number for your source media."
echo "-m to merge multiple output files as one. Useful if you have -d set."
}
POSITIONAL=();
while getopts d:f:t:w:mh opt; do
case "${opt}" in
d) DIRECTORY="${OPTARG}" ;;
f) FILE="${OPTARG}" ;;
t) TRACK="${OPTARG}" ;;
w) WORD="${OPTARG}" ;;
m) MERGE=1 ;;
h) print_help; exit 0 ;;
*) print_help; exit 3 ;;
esac
done
shift $(($OPTIND -1))
set -- "{$POSITIONAL[@]}";
# check if out directory exists
# TODO handle case out exists but other subdirs don't
if [ -z "$MERGE" ]; then
if [ ! -z "$FILE" ] && [ ! -z "$DIRECTORY" ]; then
echo "You have both -d and -f set.. choose one, not both..";
exit 127;
fi
if [ -z "$FILE" ] && [ -z "$DIRECTORY" ]; then
echo "no file or directory provided (use -f [file] or -d [dir])..";
exit;
fi
if [ -z "$TRACK" ]; then
#assume subtitle track = 2
echo "track not set (use -t [track number]). You kind find the subtitle track number using 'mkvinfo [file]'..";
exit 3;
fi
if [ -z "$WORD" ]; then
echo "no word set, please set a word to grep to (use -w [word] to set the word)..";
exit 3;
fi
if [ ! -z "$FILE" ]; then
#echo "using single file..";
SINGLEFILE=true;
fi
if [ ! -z "$DIRECTORY" ]; then
#echo "using directory";
SINGLEFILE=false;
fi
fi
# we're done the boring stuff
# check for ffmpeg
command -v ffmpeg >/dev/null 2>&1 || { echo >&2 "Requires ffmpeg. Aborting."; exit 3; }
if [ ! -d "out" ]; then
echo "making out directory..";
mkdir out;
mkdir out/clips;
fi
x=0;
grepsubs() {
# $1 = file $2 = CFILE
while read -r line; do
((x++)) || true;
parseline "$1" "$line" "$2";
done < <(grep -i "$WORD" out/subs.srt);
}
getsubs() {
# $1 = file
#echo "getting subs for "$1"";
rm out/subs.srt || true;
CFILE=$(basename "$1" .mkv);
if [ -z "$TRACK" ]; then
ffmpeg -i "$1" out/subs.srt;
else
ffmpeg -i "$1" -map "$TRACK:n" out/subs.srt;
fi
grepsubs "$1" "$CFILE";
}
parseline() {
# $1 = file $2 = line $3 = CFILE
IFS=',' read -r -a array <<< "$2";
echo "start time: ${array[1]}";
echo "end time: ${array[2]}";
cutvideo "$1" "${array[1]}" "${array[2]}" "$3";
}
cutvideo() {
echo "$1" start time\: "$2" end time: "$3" >> out/times.txt;
ffmpeg -i "$1" -ss 0"$2" -to 0"$3" -async 1 -c:v libx264 -preset ultrafast out/clips/clip_"$x".mkv < /dev/null
}
merge() {
echo "merging mkv's..";
ls -Q out/clips | sort -V | grep -E "\.mkv" | sed -e "s/^/file /g" > video_files.txt;
ffmpeg -f concat -safe 0 -i video_files.txt out/out.mkv;
rm video_files.txt
}
# getsubs function will run the chain that does the whole process
if ! [ -z "$MERGE" ]; then
merge;
exit;
fi
if [ "$SINGLEFILE" == true ]; then
echo "doing one file";
getsubs "$FILE";
fi
if [ "$SINGLEFILE" == false ]; then
for f in "$DIRECTORY"/*.mkv; do
getsubs "$f";
done;
fi