-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathahs.sh
executable file
·405 lines (369 loc) · 12.4 KB
/
ahs.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#!/bin/bash
#ahs.sh
###############################################################################
# AutoHandbrakeScript
# Copyright (C) 2009-2011 Curtis Lee Bolin <curtlee2002(at)gmail.com>
#
# 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/>.
###############################################################################
fileType=( avi flv iso mov mp4 mpeg mpg ogg ogm ogv wmv m2ts rmvb rm 3gp m4a 3g2 mj2 asf divx vob mkv )
readonly DEFAULT_VIDEO_SETTINGS="--encoder x264 --two-pass --turbo --decomb --loose-anamorphic"
#--preset slow
#readonly DEFAULT_X264_SETTINGS="--encopts b-adapt=2:direct=auto:me=umh:rc-lookahead=50:ref=5:subme=8"
#--preset slower
#readonly DEFAULT_X264_SETTINGS="--encopts b-adapt=2:direct=auto:me=umh:partitions=all:rc-lookahead=60:ref=8:subme=9:trellis=2"
#--preset veryslow
readonly DEFAULT_X264_SETTINGS="--encopts b-adapt=2:bframes=8:direct=auto:me=umh:merange=24:partitions=all:ref=16:subme=10:trellis=2:rc-lookahead=60"
readonly DEFAULT_AUDIO_SETTINGS="--audio 1 --aencoder vorbis --aq 0.3 --mixdown dpl2 --arate 48 --drc 2.5"
readonly DEFAULT_CHAPTER_SETTINGS="--markers"
readonly DEFAULT_CONTAINER_TYPE="mkv"
readonly DEFAULT_CONTAINER_SETTINGS="--format $DEFAULT_CONTAINER_TYPE"
readonly DEFAULT_LOG_FILE="handbrake.log"
readonly DEFAULT_PROCESSED_DIRECTORY="processed"
readonly DEFAULT_OUTPUT_DIRECTORY="output"
readonly DEFAULT_SKIP_DIRECTORY="skip"
readonly EMPTY_STRING=''
subtitleSettings=""
outputDirectory="$DEFAULT_OUTPUT_DIRECTORY"
otherSettings=""
mode=""
audioNumber=""
encoderCopy=""
titleOptions=""
titleCount=""
fileName=""
scanList=""
cropFlag=false
audioCopy=false
Logger ()
{
echo "$(date +'[ %d %b %Y %H:%M:%S ]') :: $*" | tee -a "$DEFAULT_PROCESSED_DIRECTORY"/"$DEFAULT_LOG_FILE"
}
CheckVideo ()
{
videoWidth=${scanList#*job->width:}
videoWidth=${videoWidth%,\ job->*}
videoHeight=${scanList#*,\ job->height:}
videoHeight=${videoHeight%[*}
videoHeight=${videoHeight%S*}
videoBitRate="$(echo sqrt\($videoWidth*$videoHeight\) | bc)"
}
CheckAudio ()
{
audioChannelList=${scanList#*+\ audio\ tracks:}
audioChannelList=${audioChannelList%+\ subtitle\ tracks:*}
audioChannelList=${audioChannelList// /} # replace " " with ""
audioCount="1"
for item in $(echo $audioChannelList | sed -e 's/+/\n/g')
do
echo $item
if $(echo $item | grep "AC3" | grep "5.1" | grep -q "384000bps") || $audioCopy
then
if [ "$audioCount" == "1" ]
then
audioTracks="$audioCount"
audioEncoder="copy:ac3"
audioBitrate="384"
audioQuality="$EMPTY_STRING"
audioSampleRate="48"
drc="2.5"
else
audioTracks="$audioTracks,$audioCount"
audioEncoder="$audioEncoder,copy:ac3"
audioBitrate="$audioBitrate,384"
audioQuality="$audioQuality,$EMPTY_STRING"
audioSampleRate="$audioSampleRate,48"
drc="$drc,2.5"
fi
elif $(echo $item | grep -q -e "5.1")
then
if [ "$audioCount" == "1" ]
then
audioTracks="$audioCount"
audioEncoder="ffac3"
audioBitrate="384"
audioQuality="$EMPTY_STRING"
audioSampleRate="48"
drc="2.5"
else
audioTracks="$audioTracks,$audioCount"
audioEncoder="$audioEncoder,ffac3"
audioBitrate="$audioBitrate,384"
audioQuality="$audioQuality,$EMPTY_STRING"
audioSampleRate="$audioSampleRate,48"
drc="$drc,2.5"
fi
else
if [ "$audioCount" == "1" ]
then
audioTracks="$audioCount"
audioEncoder="vorbis"
audioBitrate="$EMPTY_STRING"
audioQuality="0.3"
audioSampleRate="48"
drc="2.5"
else
audioTracks="$audioTracks,$audioCount"
audioEncoder="$audioEncoder,vorbis"
audioBitrate="$audioBitrate,$EMPTY_STRING"
audioQuality="$audioQuality,0.3"
audioSampleRate="$audioSampleRate,48"
drc="$drc,2.5"
fi
fi
((audioCount++))
done
audioSettings="--audio $audioTracks --aencoder $audioEncoder --aq $audioQuality --ab $audioBitrate --arate $audioSampleRate --drc $drc"
}
CheckSubtitles ()
{
subtitleChannelList=${scanList#*+\ subtitle\ tracks:}
subtitleChannelList=${subtitleChannelList%HandBrake\ has\ exited.*}
subtitleChannelCount=$(echo $subtitleChannelList | sed -e 's/+/\n/g' | wc -l)
if [ "$subtitleChannelCount" == "0" ]
then
subtitleSettings=""
else
subtitleNumbers="1"
((subtitleChannelCount--)) # to not count the first blank line
if [ "$subtitleChannelCount" != "1" ]
then
for (( i=2; i<="$subtitleChannelCount"; i++ ))
do
subtitleNumbers="$subtitleNumbers,$i"
done
fi
subtitleSettings="--subtitle $subtitleNumbers"
fi
}
FileTranscode ()
{
[ ! -d "$DEFAULT_PROCESSED_DIRECTORY" ] && mkdir -p "$DEFAULT_PROCESSED_DIRECTORY"
[ ! -d "$outputDirectory" ] && mkdir -p "$outputDirectory"
[ "${inputFileName##*.}" != "iso" ] && titleOptions=""
mv "$inputFileName" "$DEFAULT_PROCESSED_DIRECTORY"/
scanList=$(HandBrakeCLI --scan $titleOptions --input "$DEFAULT_PROCESSED_DIRECTORY"/"$inputFileName" 2>&1)
CheckVideo
CheckAudio
CheckSubtitles
Logger "Encoding $inputFileName to $videoName.$DEFAULT_CONTAINER_TYPE ..."
HandBrakeCLI $DEFAULT_VIDEO_SETTINGS $DEFAULT_X264_SETTINGS --vb $videoBitRate $audioSettings $subtitleSettings $DEFAULT_CONTAINER_SETTINGS $DEFAULT_CHAPTER_SETTINGS $otherSettings --input "$(pwd)"/"$DEFAULT_PROCESSED_DIRECTORY"/"$inputFileName" --output "$(pwd)"/"$outputDirectory"/"$videoName"."$DEFAULT_CONTAINER_TYPE"
Logger "Encoding Completed."
}
FileSearch ()
{
for inputFileName in *
do
if [ -f "$inputFileName" ] # test if it is a true file
then
fileNameExt="${inputFileName##*.}" # extracts the extension from the file name
for (( i=0 ; i!=${#fileType[@]} ; i++))
do
if [ "${fileType[$i]}" == "$fileNameExt" ]
then
videoName=${inputFileName%.*} # extracts the video name from the file name
$encodeCommand
break
fi
done
fi
done
}
IsoTranscode ()
{
[ ! -d "$DEFAULT_PROCESSED_DIRECTORY" ] && mkdir -p "$DEFAULT_PROCESSED_DIRECTORY"
[ ! -d "$outputDirectory" ] && mkdir -p "$outputDirectory"
mv "$inputFileName" "$DEFAULT_PROCESSED_DIRECTORY"/
outputDirectory="${inputFileName%.*}"
titleCount=$(HandBrakeCLI --scan --input "$DEFAULT_PROCESSED_DIRECTORY"/"$inputFileName" 2>&1 | grep "scan: DVD has")
titleCount="${titleCount:25}"
titleCount="${titleCount%\ title(s)}"
for (( count=1; count <= "$titleCount"; count++ ))
do
[ "$count" -lt "10" ] && number=0$count || number=$count
[ $mode == "TitleMode" ] && titleOptions="--title $count" && otherSettings="$titleOptions" && videoName="Title$number"
[ $mode == "ChapterMode" ] && otherSettings="--chapters $count $titleOptions" && videoName="Chapter$number"
FileTranscode
done
}
ChapterMode ()
{
fileType=( iso )
[ -z titleOptions ] && titleOptions="--main-feature"
if [ -n "$fileName" ]
then
inputFileName="$fileName"
IsoTranscode
else
encodeCommand="IsoTranscode"
FileSearch
fi
}
TitleMode ()
{
fileType=( iso )
if [ -n "$fileName" ]
then
inputFileName="$fileName"
if [ -n "$titleOptions" ]
then
otherSettings="$titleOptions"
FileTranscode
else
titleOptions="--main-feature"
otherSettings="$titleOptions"
IsoTranscode
fi
else
encodeCommand="IsoTranscode"
FileSearch
fi
}
DirectoryMode ()
{
startingDiretory=$(pwd)
encodeCommand="FileTranscode"
FileSearch
for directoryName in *
do
if [[ -d "$directoryName" && "$directoryName" != "$DEFAULT_PROCESSED_DIRECTORY" && "$directoryName" != "$DEFAULT_OUTPUT_DIRECTORY" && "$directoryName" != "$DEFAULT_SKIP_DIRECTORY" ]]
then
cd "$directoryName"
FileSearch
#######################################################################
for directoryName in *
do
if [[ -d "$directoryName" && "$directoryName" != "$DEFAULT_PROCESSED_DIRECTORY" && "$directoryName" != "$DEFAULT_OUTPUT_DIRECTORY" && "$directoryName" != "$DEFAULT_SKIP_DIRECTORY" ]]
then
cd "$directoryName"
FileSearch
cd ../
fi
done
#######################################################################
cd ../
fi
done
cd "$startingDiretory"
}
SimpleDirectoryMode ()
{
encodeCommand="FileTranscode"
FileSearch
}
FileMode ()
{
[ ! -d "$DEFAULT_PROCESSED_DIRECTORY" ] && mkdir -p "$DEFAULT_PROCESSED_DIRECTORY"
inputFileName="$fileName"
videoName="${inputFileName%.*}"
FileTranscode
}
ErrorFound ()
{
[ -z "$error" ] && error="Unrecognized option: $@"
echo
echo "**********Error with options**********"
echo
echo "Error: $error"
echo
echo "**************************************"
PrintUsage
exit 1
}
PrintUsage ()
{
echo
echo "Usage: $0 [OPTION]"
cat << EOF
Modes:
-C, --chapter [TITLE]
Transcodes each chapter of the main feature title or title number given
of the iso files in that directory or file given as input.
-T, --title [TITLE] copy
Transcodes each title or title number given
of the iso files in that directory or file given as input.
If copy is set then audio tracks are copied instead of trancoded.
-D, --directory
Transcodes files two directories deep.
With no mode selected all video files in the directory will be transcoded
and main feature title of an iso files unless an input file is given.
Other Options:
-i, --input [FILE]
If a file name is given, only that file will be encoded
-c, --crop <T:B:L:R>
Manually sets the cropping
Top:Bottom:Left:Right
-h, --help
Prints this help information.
EOF
}
if [ -z "$1" ]
then
SimpleDirectoryMode
else
until [ -z "$1" ]; do
# use a case statement to test vars. we always test
# test $1 and shift at the end of the for block.
case $1 in
-h|--help)
PrintUsage
exit 0
;;
-C|--chapter )
# shift, so the string after -c or --chapter becomes our new $1
[ -n "$mode" ] && error="Only one mode can be selected." && ErrorFound
shift
[ -n "$1" -a "$1" != "-*" ] && titleOptions="--title $1"
mode="ChapterMode"
;;
-T|--title )
[ -n "$mode" ] && error="Only one mode can be selected." && ErrorFound
[ -n "$2" -a "${2:0:1}" != "-" -a "$2" != "copy" ] && shift && titleOptions="--title $1"
[ -n "$2" -a "copy" ] && shift && audioCopy=true
mode="TitleMode"
;;
-D|--directory )
[ -n "$mode" ] && error="Only one mode can be selected." && ErrorFound
[ -n "$2" -a "$2" = "-i" ] && error="Can't use input file with directory mode" && ErrorFound
mode="DirectoryMode"
;;
-c|--crop )
[ $cropFlag ] && error="Only one crop can be set." && ErrorFound
cropFlag=true
shift
otherSettings="$otherSettings --crop $1"
;;
-i|--input )
shift
if [ -n "$1" -a "${1:0:1}" != "-" ] # "${1:0:1}" gets the first charater of string $1
then
fileName="$1"
else
error="$1 is not a valid input file."
ErrorFound
fi
;;
* )
ErrorFound $@
;;
esac
shift
if [ "$#" = "0" ]; then
break
fi
done
fi
[ -z "$mode" ] && mode="SimpleDirectoryMode"
$mode
exit 0