-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranscode_subfolders.sh
More file actions
427 lines (355 loc) · 11.3 KB
/
Transcode_subfolders.sh
File metadata and controls
427 lines (355 loc) · 11.3 KB
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#!/bin/bash
# stop if error occurs
set -e
Help()
{
# Display Help
echo "Depending on the resolution, selects crf and preset"
echo "Predefined for quality, but if something is mediocre, then CRF-23 medium for 720p-1080p should be specified manually"
echo "CRF-22 slow on 1080p 24-series gives ~9 gigs, CRF-23 medium gives ~5 gigs"
echo
echo "Syntax: $0 [-r]"
echo "$0 -r"
echo "options:"
echo "r remove source"
echo
echo "Folder name tags (don't work for subfolders):"
echo "[P-%%] [CRF-%%] [force] [skip]"
echo ""
echo "[to720] - downscale to 720p"
echo "[anime] or [film] - encoding preset params (default anime)"
echo ""
echo "[opus] - convert audio to opus"
echo "[surround] - enable convert surround audio to opus"
echo "[kbps-%%] - default opus bitrate"
echo "[rl-%%,%%] - remove selected lang tags from audio tracks"
echo "[-flac] force convert FLAC to Opus (ignored opus and surround flags)"
echo
exit
}
del=false
surround=false
# [ $# -eq 0 ] && Help # if no parameters, will show help
while getopts "r" o; do
case "${o}" in
r)
del=true
;;
\?) # incorrect option
echo "Error: Invalid option"
Help
;;
esac
done
# removes n strings from the positional parameters list. Thus shift $((OPTIND-1)) removes all the options that have been parsed by getopts from the
# parameters list, and so after that point, $1 will refer to the first non-option argument passed to the script.
shift $((OPTIND-1))
log_file=`date +"%Y%m%dT%H%M"`
[ ! -d "./logs" ] && mkdir ./logs
touch ./logs/$log_file.log
touch ./logs/${log_file}_problems.log
exec &> >(tee -a "./logs/$log_file.log")
function log_err() {
echo "$1" | tee -a ./logs/${log_file}_problems.log
}
function fnDelete {
if [ -f "$1" ]; then
if [[ "$del" == "true" ]]; then
rm "$1"
echo "$1 is removed"
fi
fi
}
opus=false
flac_to_opus=false
remove_langs=()
audio_bitrate="96"
function BitrateForChannelLayout()
{
local channel_layout="$1"
local source_bitrate="$2"
local source_codec="$3"
local force_s="$4"
if [[ "$source_codec" =~ "opus" ]]; then
echo "N/A" # skip track
return
fi
if [[ "$channel_layout" =~ "7.1" && ($surround == "true" || $force_s == "true") ]]; then
if [[ $audio_bitrate -gt 96 ]]; then
echo $((96*5))
else
echo $(($audio_bitrate*5))
fi
elif [[ "$channel_layout" =~ "5.1" && ($surround == "true" || $force_s == "true") ]]; then
if [[ $audio_bitrate -gt 96 ]]; then
echo $((96*3))
else
echo $(($audio_bitrate*3))
fi
elif [ "$channel_layout" == "stereo" ]; then
if [[ $source_bitrate =~ ^[0-9]+$ && $source_bitrate -gt 160 && $audio_bitrate -lt 128 ]]; then
echo $(($audio_bitrate+32))
else
echo "$audio_bitrate"
fi
else
echo "N/A"
fi
}
function GenerateMapAudio()
{
local input_file="$1"
local to_opus="$2"
local flac_convert="$3"
local command=""
local audio_streams=$(ffprobe -v error -select_streams a -show_entries stream=index -of csv=p=0 "$input_file")
local lang_tags=( $(ffprobe -v quiet -select_streams a -show_entries stream_tags=language -of csv=p=0 "$input_file") )
local audio_index=-1
local codec_index=-1
for audio_stream in $audio_streams; do
audio_index=$(($audio_index + 1))
codec_index=$(($codec_index + 1))
local map_option="-map 0:a:$audio_index"
local codec_option="-c:a:$codec_index copy"
local channel_layout=$(ffprobe -v error -select_streams a:$audio_index -show_entries stream=channel_layout -of default=noprint_wrappers=1:nokey=1 "$input_file")
local source_bitrate=$(ffprobe -v error -select_streams a:$audio_index -show_entries stream=bit_rate -of default=noprint_wrappers=1:nokey=1 "$input_file")
if [[ "$source_bitrate" =~ ^[0-9]+$ ]]; then
source_bitrate=$(($source_bitrate / 1000))
fi
local source_codec=$(ffprobe -v quiet -select_streams a:$audio_index -show_entries stream=codec_name -of csv="p=0" "$input_file")
# Check if language is to be removed
local language=${lang_tags[$audio_index]}
if [[ ! ${#remove_langs[@]} -eq 0 && -n $language && " ${remove_langs[@]} " =~ " $language " ]]; then
codec_index=$(($codec_index - 1))
continue
fi
# Add bitrate and format options based on channel layout
local bitrate=$(BitrateForChannelLayout "$channel_layout" "$source_bitrate" "$source_codec")
if [[ "$bitrate" != "N/A" ]] && [[ "$to_opus" == "true" ]]; then
codec_option="-c:a:$codec_index libopus -b:a:$codec_index ${bitrate}k -filter:a:$codec_index \"aformat=channel_layouts='7.1|5.1|stereo'\""
fi
if [[ "$source_codec" == "flac" ]] && [[ "$flac_convert" == "true" ]]; then
local bitrate=$(BitrateForChannelLayout "$channel_layout" "160" "$source_codec" "true")
codec_option="-c:a:$codec_index libopus -b:a:$codec_index ${bitrate}k -filter:a:$codec_index \"aformat=channel_layouts='7.1|5.1|stereo'\""
fi
# Add current audio map and codec to command
command+=" $map_option $codec_option"
done
# Add subtitle streams to command
command+=" -map 0:s? -c:s copy -map 0:t? -c:t copy -map 0:d? -c:d copy -map -0:d:m:handler_name:\"SubtitleHandler\""
echo "$command"
}
crf_overwrite=""
preset_overwrite=""
skip=false
force=false
to720=false
preset_params=""
shopt -s globstar
for i in ./**/*; do
echo "ITER = $i"
ext=${i##*.}
if [[ -d "$i" ]]; then
preset_params=""
if [[ "$i" == *"[CRF-"* ]]; then
crf_number=$(echo "$i" | grep -oP "\[CRF-\K\d+" | head -1)
if [[ -n "$crf_number" ]]; then
crf_overwrite="$crf_number"
else
crf_overwrite="" # default value
fi
else
crf_overwrite="" # default value
fi
regex="\[P-(\S+)\]"
if [[ "$i" =~ $regex ]]; then
if [[ -n "${BASH_REMATCH[1]}" ]]; then
preset_overwrite="${BASH_REMATCH[1]}"
else
preset_overwrite="" # default value
fi
else
preset_overwrite="" # default value
fi
if [[ "$i" == *"[skip]"* ]]; then
skip=true
echo "THIS FOLDER WILL BE SKIPPED (folder tag)"
else
skip=false
fi
if [[ "$i" == *"[force]"* ]]; then
force=true
echo "THIS FOLDER WILL BE FORCE-CONVERTED (folder tag)"
else
force=false
fi
if [[ "$i" == *"[to720]"* ]]; then
to720=true
echo "THIS FOLDER WILL BE DOWNSCALLED TO 720 (folder tag)"
else
to720=false
fi
if [[ "$i" == *"[film]"* ]]; then
preset_params="film"
echo "THIS FOLDER WILL CONVERT WITH FILM PARAMS (folder tag)"
fi
if [[ "$i" == *"[anime]"* ]]; then
preset_params="anime"
echo "THIS FOLDER WILL CONVERT WITH ANIME PARAMS (folder tag)"
fi
if [[ "$i" == *"[opus]"* ]]; then
opus=true
echo "THIS FOLDER WILL CONVERT TO OPUS (folder tag)"
if [[ "$i" == *"[surround]"* ]]; then
surround=true
echo "surround audio will be converted to opus (folder tag)"
else
surround=false
fi
else
opus=false
fi
if [[ "$i" == *"[-flac]"* ]]; then
flac_to_opus=true
echo "THIS FOLDER WILL CONVERT FLAC TO OPUS (folder tag)"
else
flac_to_opus=false
fi
if [[ "$i" == *"[kbps-"* ]]; then
audio_bitrate_number=$(echo "$i" | grep -oP "\[kbps-\K\d+" | head -1)
if [[ -n "$audio_bitrate_number" ]]; then
audio_bitrate="$audio_bitrate_number"
else
audio_bitrate="96" # default value
fi
else
audio_bitrate="96" # default value
fi
regex="\[rl-(\S+)\]" # put the regex in a variable because some patterns won't work if included literally
if [[ "$i" =~ $regex ]]; then
if [[ -n "${BASH_REMATCH[1]}" ]]; then
IFS=',' read -r -a remove_langs <<< "${BASH_REMATCH[1]}"
else
remove_langs=() # default value
fi
else
remove_langs=() # default value
fi
fi
if [ "$ext" == "MP4" -o "$ext" == "mp4" -o "$ext" == "avi" -o "$ext" == "mkv" ]; then
if [[ "$i" == *"_k."* ]] || [ -f "${i%.*}_k.mkv" ]; then
echo "File found! Skip";
continue
fi
if [[ "$skip" == "true" ]]; then
continue
fi
if [ -f "$i" ]; then
height=`ffprobe -v error -select_streams v:0 -show_entries stream=height -of csv=s=x:p=0 "$i" | cut -d 'x' -f 1`
codec=`ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "$i"`
if [[ "$codec" == "h265" ]] || [[ "$codec" == "hevc" ]] || [[ "$codec" == "av1" ]] || [[ "$codec" == "vp9" ]]; then
if [[ "$force" == "false" ]]; then
echo "Already used hign compressed codec: $codec! Skip";
continue
fi
fi
SCALE=()
if [[ "$to720" == "true" ]] && [[ $height -gt 720 ]]; then
height=720
SCALE=("-filter:v:0" "zscale=-2:720:filter=spline36")
fi
if [[ "$preset_params" == "" ]]; then
preset_params="anime"
echo "Will use default preset (anime)"
fi
params=""
case $preset_params in
film)
echo "Film preset params."
params="no-sao=1:aq-mode=2"
crf="25"
preset="medium"
if [[ $height -lt 1190 ]]; then
params="strong-intra-smoothing=0:no-sao=1:aq-mode=3"
crf="23"
preset="medium"
fi
if [[ $height -lt 721 ]]; then
params="strong-intra-smoothing=0:limit-sao=1:aq-mode=3"
crf="23"
preset="slow"
fi
if [[ $height -lt 481 ]]; then
params="strong-intra-smoothing=0:limit-sao=1:aq-mode=3"
crf="22"
preset="slow"
fi
;;
anime)
echo "Anime preset params."
params="no-sao=1:tskip=1:psy-rd=1.5:psy-rdoq=1:ref=4:aq-mode=3"
crf="24"
preset="medium"
if [[ $height -lt 1081 ]]; then
params="no-sao=1:tskip=1:psy-rd=1.5:psy-rdoq=1:ref=4:aq-mode=3"
crf="22"
preset="medium"
fi
if [[ $height -lt 721 ]]; then
params="no-sao=1:tskip=1:psy-rd=1.5:psy-rdoq=1:ref=5:aq-mode=3"
crf="21"
preset="slow"
fi
if [[ $height -lt 481 ]]; then
params="limit-sao=1:tskip=1:psy-rd=1.5:psy-rdoq=1:ref=5:aq-mode=3"
crf="20"
preset="slow"
fi
;;
*)
echo "Error: Preset params are not selected."
log_err "Error: Preset params are not selected. $i"
exit;
;;
esac
if [[ -n $crf_overwrite ]]; then
crf=$crf_overwrite
fi
if [[ -n $preset_overwrite ]]; then
preset=$preset_overwrite
fi
echo "CRF = $crf"
echo "params = $params"
echo "preset = $preset"
echo "Scale = ${SCALE[@]}"
if [[ "$opus" == "true" ]]; then
echo "Converting audio to opus"
echo "Default bitrate ${audio_bitrate}k "
fi
if [[ "$flac_to_opus" == "true" ]]; then
echo "flac tracks will be forcibly converted to opus"
fi
if [[ ! ${#remove_langs[@]} -eq 0 ]]; then
echo "Tracks (${remove_langs[@]}) will be removed"
fi
map_audio=$(GenerateMapAudio "$i" "$opus" "$flac_to_opus")
threads=18
command="ffmpeg -stats_period 3 -threads \"$(($threads / 2))\" -i \"$i\" ${SCALE[@]} -max_muxing_queue_size 2064 -map 0:v -c:v copy -c:v:0 libx265 -preset \"$preset\" -x265-params \"$params:pools=$threads\" -crf \"$crf\" -pix_fmt yuv420p10le $map_audio \"${i%.*}_k.mkv\""
#fi
echo "Executing: $command"
eval "$command"
# Check the return code
if [ $? -ne 0 ]; then
log_err "Conversion error: file \"$i\""
else
if [[ $(find "${i%.*}_k.mkv" -size -50M 2>/dev/null) ]]; then
log_err "Suspiciously small file size: file \"${i%.*}_k.mkv\""
else
fnDelete "$i"
fi
echo "Done"
fi
date +"%x %X %z"
fi
fi
done