-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranscode_subfolders_custom.sh
More file actions
326 lines (276 loc) · 8.43 KB
/
Transcode_subfolders_custom.sh
File metadata and controls
326 lines (276 loc) · 8.43 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
#!/bin/bash
# stop if error occurs
set -e
Help()
{
# Display Help
echo "Encodes with specified crf, can reduce fps and resolution"
echo
echo "Syntax: $0 [-q <15-40>] [-p <h265 preset>] [-f <fps>] [-m <0-3>] [-d <1-3>] [-h <height>] [-a] [-l <lang1,lang2>] [-o] [-b <opus_bitrate>] [-s]"
echo "$0 -q 21 -p medium -f 25 -h 720 -m 2 -d 2 -o -s -b 96 -l jpn,ukr"
echo "options:"
echo "q CRF between 15 and 40"
echo "f Convert to selected fps"
echo "p preset, default slow"
echo "a anime codec preset"
echo "m aq-mode, 0-3, default 2"
echo "d sao-mode, 1-3 (1:sao, 2:limit-sao, 3:no-sao), default 2"
echo "h scale to select height"
echo "l remove audio tracks with selected lang tags"
echo "o convert audio to opus"
echo "s enable convert surround audio"
echo "b target opus bitrate if source bitrate is unknown, default 96"
echo
echo "Folder name tags (don't work for subfolders):"
echo "[skip]"
echo "[-flac] force convert FLAC to Opus (ignored opus and surround flags)"
echo
exit
}
crf=0
FPS=""
preset="slow"
aq_mode=2
sao_mode=2
scale=""
remove_langs=()
opus=false
flac_to_opus=false
audio_bitrate="96"
surround=false
anime=false
[ $# -eq 0 ] && Help
while getopts "q:f:p:m:d:h:l:osb:a" o; do
case "${o}" in
q)
if [ ${OPTARG} -gt 14 ]; then
crf=${OPTARG}
else
echo "CRF too small"
exit 0
fi
;;
f)
FPS=${OPTARG}
;;
p)
preset=${OPTARG}
;;
m)
if [ ${OPTARG} -ge 0 ] && [ ${OPTARG} -le 3 ]; then
aq_mode=${OPTARG}
else
echo "Invalid aq-mode value. Should be between 1 and 3."
exit 0
fi
;;
d)
if [ ${OPTARG} -ge 1 ] && [ ${OPTARG} -le 3 ]; then
sao_mode=${OPTARG}
else
echo "Invalid sao-mode value. Should be between 1 and 3."
exit 0
fi
;;
h)
scale=${OPTARG}
;;
l)
IFS=',' read -r -a remove_langs <<< "$OPTARG"
;;
b)
audio_bitrate=${OPTARG}
;;
o)
opus=true
;;
s)
surround=true
;;
a)
anime=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))
if [ "$crf" -lt 14 ]; then
echo "CRF not selected"
exit 0
fi
params=""
if [ "$sao_mode" -eq 1 ]; then
params="sao=1"
elif [ "$sao_mode" -eq 2 ]; then
params="limit-sao=1"
elif [ "$sao_mode" -eq 3 ]; then
params="no-sao=1"
fi
if [ "$aq_mode" -ge 1 ]; then
params+=":aq-mode=$aq_mode"
fi
filter=""
if [[ "$scale" -ge 1 ]] || [[ "$FPS" -ge 1 ]]; then
filter=("-filter:v:0")
f_opts=()
if [ "$scale" -ge 1 ]; then
f_opts+=("zscale=-2:$scale:filter=spline36")
fi
if [ "$FPS" -ge 1 ]; then
f_opts+=("fps=$FPS")
fi
opts_list=""
for opt in "${f_opts[@]}"; do
opts_list+="$opt,"
done
# Remove the trailing ","
opts_list=${opts_list%,}
filter+=($opts_list)
fi
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 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"
echo "$command"
}
if [[ "$anime" == "true" ]]; then
params+=":tskip=1:psy-rd=1.5:psy-rdoq=1:ref=5"
fi
echo "CRF = $crf"
echo "Preset = $preset"
echo "h265 params = $params"
echo "Filter = ${filter[@]}"
if [[ "$opus" == "true" ]]; then
echo "Converting audio to opus"
echo "Default bitrate ${audio_bitrate}k "
fi
if [[ ! ${#remove_langs[@]} -eq 0 ]]; then
echo "Tracks (${remove_langs[@]}) will be removed"
fi
skip=false
shopt -s globstar
for i in ./**/*; do
echo "ITER = $i"
ext=${i##*.}
if [[ -d "$i" ]]; then
if [[ "$i" == *"[skip]"* ]]; then
skip=true
echo "THIS FOLDER WILL BE SKIPPED (folder tag)"
else
skip=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
fi
if [[ "$skip" == "true" ]]; then
continue
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 [ -f "$i" ]; then
map_audio=$(GenerateMapAudio "$i" "$opus" "$flac_to_opus")
threads=18
command="ffmpeg -stats_period 3 -threads \"$(($threads / 2))\" -i \"$i\" -max_muxing_queue_size 2064 -map 0:v -c:v copy -c:v:0 libx265 ${filter[@]} -preset \"$preset\" -x265-params \"$params:pools=$threads\" -crf \"$crf\" -pix_fmt yuv420p10le $map_audio \"${i%.*}_k.mkv\"";
echo "Executing: $command"
eval "$command"
# Check the return code
if [ $? -ne 0 ]; then
log_err "Suspiciously small file size: file \"$i\""
else
echo "Done"
fi
if [[ $(find "${i%.*}_k.mkv" -size -50M 2>/dev/null) ]]; then
log_err "Suspiciously small file size: file \"${i%.*}_k.mkv\""
fi
fi
fi
done