-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvc2.sh
More file actions
executable file
·674 lines (588 loc) · 17.8 KB
/
vc2.sh
File metadata and controls
executable file
·674 lines (588 loc) · 17.8 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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
#!/bin/bash
# This script will search for video files in various directories as specified by the command line arguments.
# The script builds an array of incoming files and then probes the file and will convert it to the specified
# format base on the command line arguments.
# Expected incoming directory structure:
# renamed/ <-- This is the search directory.
# ├── features
# ├── mtv
# ├── restricted
# ├── series <-- This is the search directory with subdirectories for each series.
# │ └── Series 1
# | └── S04
# │ └── Series 2
# | └── S08
# │ └── Series 3
# | └── S02
# │ └── Series 4
# | └── S01
# │ └── Series 5
# | └── S07
# │ └── Series 5
# | └── S01 <-- This is where the series video files are located.
# | └── S02
# | └── S03
# ├── other
# ├── video
# The output directory structure will be duplicated from the search directory structure.
# Starting at $videoDir base.
# Program settings
ffmpeg_bin='/usr/local/bin/ffmpeg'
#ffmpeg_bin='/usr/bin/ffmpeg'
#sample_range="-t 10:00"
sample_range="-ss 01:00 -t 06:00"
baseDir='/data2/usenet'
searchDir="$baseDir/renamed"
workDir="$baseDir/tmp"
inFiles="$workDir/inFiles.lst"
logDir='/var/log/convert'
logFile="$logDir/ccvc.log"
traceLog="$logDir/ccvc_trace_$(date +%F).log"
doneDir="$baseDir/done"
videoDir="/video"
tempDir="/video/temp"
user="serviio"
group="video"
# Default video parameters
audio_codec='libfdk_aac'
video_codec='libx264'
hq='false'
lq='false'
usage()
{
cat << EOM
NAME
$0 - video converter
SYNOPSIS
$0 [OPTION]
DESCRIPTION
Re-encodes video files to sane/portable parameters.
-h, --help
This documentation.
--hq
Will re-encode video with high quality settings.
--lq
Will re-encode video with low quality settings.
-m, --movie
Will look for feature length movies in configured directory.
--mv
Will look for music videos in configured directory.
-o, --other
Will look for other type video files in configured directory.
-s, --series
Will look for series shows in configured directory.
-v, --video
Will look for video files in configured directory.
-x. --restrict
Will look for restricted videos in configured directory.
AUTHOR
Written by Richard L. Paxton.
EXAMPLE
The following would search for movie files and re-encode them at high quality.
$0 -m --hq
EOM
exit 1
}
# Process command line arguments
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-h | --help) #Display help
usage
;;
--hq) #Set high quality override
hq='true'
sample_range='' # sample the entire video file
vResize='false'
shift
;;
--lq) #Set low quality override
lq='true'
audioChannels='2'
shift
;;
-m | --movie) #Process movies
aRemix='true'
#audioChannels='2'
inDir="$searchDir/features"
sample_range='-t 15:00' # 15 minute sample range from beginning
target_FPS='24000/1001'
target_QF='.1'
target_aBitrate='160k'
target_sampleRate='48k'
vPreset='medium'
vResize='true'
vTune='film'
shift
;;
--mv) #music video files
aRemix='true'
audioChannels='2'
inDir="$searchDir/mtv"
target_FPS='30'
target_QF='.2'
target_aBitrate='192k'
target_sampleRate='48k'
vPreset='slow'
vResize='true'
vTune='film'
shift
;;
-o | --other) #Process other videos
aRemix='true'
#audioChannels='2'
inDir="$searchDir/other"
target_FPS='24000/1001'
target_QF='.1'
target_aBitrate='160k'
target_sampleRate='48k'
vPreset='slow'
vResize='true'
vTune='film'
shift
;;
-s | --series) #tv series encodes
aRemix='true'
audioChannels='2'
inDir="$searchDir/series"
target_FPS='24000/1001'
target_QF='.08'
target_aBitrate='128k'
target_sampleRate='48k'
vPreset='fast'
vResize='true'
vTune='film'
shift
;;
-v | --video) #video files
aRemix='true'
#audioChannels='2'
inDir="$searchDir/video"
target_FPS='30'
target_QF='.2'
target_aBitrate='192k'
target_sampleRate='48k'
vPreset='slow'
vResize='false'
shift
;;
-x | --restricted) #restricted videos
aRemix='true'
audioChannels='2'
inDir="$searchDir/restricted"
target_FPS='25'
target_QF='.08'
target_aBitrate='92k'
vPreset='fast'
vResize='true'
vTune='film'
shift
;;
*) #Unknown option
echo -e "${CRED}ERROR: 10 - Unknown option '$1'${CNORM}"
usage
;;
esac
done
# Set video overrides if passed
if [[ $hq == 'true' ]]; then
aRemix='false'
target_FPS='60'
target_QF='.2'
target_aBitrate='192k'
target_sampleRate='48k'
vPreset='slow'
vResize='false'
vTune='film'
elif [[ $lq == 'true' ]]; then
aRemix='true'
target_FPS='23.976'
target_QF='.08'
target_aBitrate='128k'
target_sampleRate='48k'
vPreset='fast'
vResize='true'
vTune='film'
fi
# Misc settings
pad=$(printf '%0.1s' "."{1..100})
padlength=100
interval=.5
rPID=""
trap 'deadJim' 1 2 3 15
# Add some colors
CGRN='\033[38;5;040m' # Green
CGRY='\033[38;5;243m' # Grey
CWHT='\033[38;5;254m' # White
CYEL='\033[38;5;184m' # Yellow
CRED='\033[38;5;160m' # Red
CPUR='\033[38;5;165m' # Purple
CBLU='\033[38;5;063m' # Blue
#CDGR='\033[38;5;234m' # Dark
CNORM='\033[0;00m' # Reset
# Define Global variables
typeset baseName outFile vOpts vFilter aOpts aFilter sOpts fullName fileName extension metaFile
## Defined Functions
logIt ()
{
echo "$(date '+%b %d %H:%M:%S') $1" >> "$logFile"
return 0
}
deadJim ()
{
# Display message and reset cursor on trap
kill -9 "$rPID" > /dev/null 2>&1
wait "$rPID" 2>/dev/null
echo ""
Text1="Abort detected, stopping now"
# shellcheck disable=SC2059
printf " ${CRED}${Text1}${CNORM}"
printf '%*.*s' 0 $((padlength - ${#Text1} - 6 )) "$pad"
echo -e "\b\b\c"
echo -e "[${CPUR}KILLED${CNORM}]"
tput cnorm
exit 1
}
traceIt ()
{
# $1 = $LINENO, $2 = function, $3 = status, $4 = description
echo "$(date '+%b %d %H:%M:%S') [$(printf "%.3d" "$1")] $2: [$3] $4" >> "$traceLog"
return 0
}
displayIt ()
{
Text1="$1"
Text2="$2"
if (( $# > 1 )); then
printf " %b" "${CGRY}${Text1}${CBLU}${Text2}${CNORM}"
printf '%*.*s' 0 $((padlength - ${#Text1} - ${#Text2} - 6 )) "$pad"
else
printf " %b" "${CGRY}${Text1}${CNORM}"
printf '%*.*s' 0 $((padlength - ${#Text1} - 6 )) "$pad"
fi
rotate &
rPID=$!
return 0
}
rotate ()
{
while :
do
tput civis
((z++))
#shellcheck disable=SC1003
case $z in
"1") echo -e "-\b\c"
sleep $interval
;;
"2") echo -e '\\'"\b\c"
sleep $interval
;;
"3") echo -e "|\b\c"
sleep $interval
;;
"4") echo -e "/\b\c"
sleep $interval
;;
*) z=0 ;;
esac
done
}
killWait ()
{
FLAG=$1
kill -9 "$rPID"
wait "$rPID" 2>/dev/null
echo -e "\b\b\c"
tput cnorm
case $FLAG in
"0") echo -e "[${CGRN} OK ${CNORM}]"
;;
"1") echo -e "[${CRED}ERROR!${CNORM}]"
;;
"2") echo -e "[${CYEL} WARN ${CNORM}]"
;;
*) echo -e "[${CPUR}UNKWN!${CNORM}]"
;;
esac
return 0
}
getFiles ()
{
## Find following file types within inDir.
## Filter any with .zzz extension.
find "$inDir" -type f -iregex '.*.\(mgp\|mp4\|m4v\|wmv\|avi\|mpg\|mov\|mkv\|flv\|webm\|ts\|f4v\)' \
-fprintf "$inFiles" '%h/%f\n'
sed -i '/\.zzz/d' "$inFiles"
# The following if statements detect where the file is located and sets fType.
i=0
while read -r LINE; do
fileNo=$((i+1))
fullName[i]="$LINE"
fileName[i]=$(basename "${fullName[$i]}")
# Strip search directory from base directory.
baseDir[i]=$(dirname "${fullName[$i]}" | sed -e "s@$searchDir/@@")
tempFile="${fileName[$i]}"
extension[i]="${tempFile##*.}"
baseName[i]="${tempFile%.*}"
outDir[i]="$videoDir/${baseDir[$i]}"
traceIt $LINENO getFiles " info " "== Processing file number: [$(printf '%.3d' $fileNo)] =="
traceIt $LINENO getFiles " info " " fullName: ${fullName[$i]}"
traceIt $LINENO getFiles " info " "directory: ${baseDir[$i]}"
traceIt $LINENO getFiles " info " " fileName: ${fileName[$i]}"
traceIt $LINENO getFiles " info " " baseName: ${baseName[$i]}"
traceIt $LINENO getFiles " info " "extension: ${extension[$i]}"
traceIt $LINENO getFiles " info " " outDir: ${outDir[$i]}"
((i++))
done < $inFiles
rm $inFiles
return 0
}
getMeta ()
{
# Metadata
inFile=$1
fTitle=$(awk -F'[()]' '{print $1}' <<< "$inFile")
fDate=$(awk -F'[()]' '{ print $2 }' <<< "$inFile" | awk '{ print $1 }')
fDate=${fDate:-$(date +%F)}
metaFile="${workDir}/${inFile}.meta"
unset meta_title meta_data meta_synopsis
meta_title=${meta_title:-$fTitle}
meta_date=${meta_date:-$fDate}
meta_synopsis=${meta_synopsis:-'No info'}
meta_composer="the Gh0st"
meta_comment="$ffmpeg_string"
echo ";FFMETADATA1" > "$metaFile"
metaData[0]="title=$meta_title"
metaData[1]="date=$meta_date"
metaData[2]="synopsis=$meta_synopsis"
metaData[3]="comment=$meta_comment"
metaData[4]="composer=$meta_composer"
j=0
while (( j < ${#metaData[*]} ))
do
echo "${metaData[$j]}" >> "$metaFile"
j=$((j+1))
done
return 0
}
normalizeIt ()
{
inFile=$1
normalize=$(cc_norm "$inFile" $ffmpeg_bin "$sample_range")
traceIt $LINENO normalIt " info " "normalize=$normalize"
return 0
}
probeIt ()
{
inFile="$1"
cc_probe "$inFile"
# shellcheck disable=SC1091
source .probe.rc
# shellcheck disable=SC2154
{ echo "> fName=$fName"
echo "> fSize=$fSize"
echo "> duration=$duration"
echo "> vStream=$mainVideo"
echo "> vWidth=$vWidth"
echo "> vHeight=$vHeight"
echo "> vBitrate=$vBitrate"
echo "> vFPS=$vFPS"
echo "> vLanguage=$vLanguage"
echo "> vMap=$vMap"
echo "> aStream=$mainAudio"
echo "> aBitrate=$aBitrate"
echo "> aSample=$aSampleRate"
echo "> aChannels=$aChannels"
echo "> aLanguage=$aLanguage"
echo "> aMap=$aMap"
echo "> sMap=$sMap"
} >> "$traceLog"
rm .probe.rc
return 0
}
setOpts ()
{
## Build video filter string
# shellcheck disable=SC2154 # vMap sourced from probeIt()
vFilter="$vMap "
vFilter+='-vf '
# Check if video needs to be resized.
if [[ $vResize != 'true' ]]; then
traceIt $LINENO setOpts " info " "Skipping video resize due to override."
else
# Resize video based on video width (vWidth sourced from probeIt)
# shellcheck disable=SC2154 # vWidth sourced from probeIt()
if (( vWidth > 1280 )); then
vFilter+="scale=1280:-1,"
scale=$(echo "scale=6; (1280/$vWidth)" | bc)
elif (( vWidth < 720 )); then
vFilter+="scale=720:-1,"
scale=$(echo "scale=6; (720/$vWidth)" | bc)
else
scale=1
fi
fi
# Check measured video FPS to targetFPS.
#shellcheck disable=SC2154 # vFPS sourced from probeIt()
if [[ $(echo "$vFPS >= $target_FPS" |bc -l) ]]; then
FPS=$target_FPS
else
FPS=$vFPS
fi
vFilter+="fps=fps=$FPS"
# This can be used to blur logo maps.
if [[ -e $inDir/${baseName[$l]}.png ]]; then
vFilter+=",removelogo=\"$inDir/${baseName[$l]}.png\""
fi
traceIt $LINENO setOpts " info " "vFilter: $vFilter"
## Build video codec string
vOpts="-c:v $video_codec "
# Calculate video bitrate
#shellcheck disable=SC2154 # vHeight sourced from probeIt()
_vSize=$(printf "%.0f" "$(echo "scale=2; $vHeight*$scale" | bc)")
_hSize=$(printf "%.0f" "$(echo "scale=2; $vWidth*$scale" | bc)")
target_vBitrate=$(printf "%.0f" "$(echo "scale=2; ($target_QF*$_hSize*$_vSize*$FPS)/1000" | bc)")
traceIt $LINENO setOpts " info " "target_vBitrate=$target_vBitrate"
vOpts+="-b:v ${target_vBitrate}k "
vOpts+="-preset $vPreset "
vOpts+="-tune $vTune"
traceIt $LINENO setOpts " info " "vOpts: $vOpts"
## Build audio filter string
#shellcheck disable=SC2154 # aMap sourced from probeIt()
if [[ -n $aMap ]]; then
aFilter="$aMap "
if [[ -n $normalize ]]; then
aFilter+="$normalize"
fi
# Build audio codec string
aOpts="-c:a $audio_codec "
aOpts+="-b:a $target_aBitrate "
if [[ $aRemix != 'true' ]]; then
aOpts+="-ac 2 "
else
#shellcheck disable=SC2154 # aChannels sourced from probeIt()
aOpts+="-ac ${audioChannels:-$aChannels} "
fi
aOpts+="-ar ${target_sampleRate:-48k}"
else
aOpts='-an'
fi
traceIt $LINENO setOpts " info " "aOpts: $aOpts"
## Build subtitle codec string
if [[ -n $sMap ]]; then
sOpts="-c:s mov_text "
sOpts+="-metadata:s:s:0 "
sOpts+="language=eng "
sOpts+="$sMap"
else
sOpts="-sn"
fi
return 0
}
encodeIt ()
{
inFile=$1
if [[ ! -d "${outDir[$l]}" ]]; then
sudo mkdir -p "${outDir[$l]}"
sudo chown $user:$group "${outDir[$l]}"
sudo chmod 0775 "${outDir[$l]}"
fi
if [[ $hq != 1 ]]; then
outFile="${outDir[$l]}/${baseName[$l]}.mp4"
else
outFile="${outDir[$l]}/${baseName[$l]}-∞.mp4"
fi
ffmpeg_string="${ffmpeg_bin} "
#ffmpeg_string="ffmpeg "
ffmpeg_string+="-hide_banner -y "
ffmpeg_string+="-loglevel quiet -stats "
ffmpeg_string+="-i \"$inFile\" "
ffmpeg_string+="-i \"$metaFile\" "
ffmpeg_string+="-map_metadata 1 "
ffmpeg_string+="$vOpts "
ffmpeg_string+="$vFilter "
ffmpeg_string+="$aOpts "
ffmpeg_string+="$aFilter "
ffmpeg_string+="$sOpts "
tempOut="$tempDir/converting.mp4"
#echo -e "\n${CDGR}> ${ffmpeg_string//-loglevel quiet -stats /} $tempOut${CNORM}\n"
traceIt $LINENO encodeIt " CMD " "> $ffmpeg_string $outFile"
echo -e " total time=${CYEL}$duration${CNORM}"
bash -c "$ffmpeg_string $tempOut"
STATUS=$?
if (( STATUS > 0 )); then
logIt "Re-encoding of $inFile failed!"
traceIt $LINENO encodeIt "ERROR!" "STATUS=$STATUS, ffmpeg encode failed."
echo -e "> ${CRED}ERROR: Run the following to see details why:\n${ffmpeg_string//-loglevel quiet -stats /} $tempOut${CNORM}\n"
else
sudo mv -f "$tempOut" "$outFile"
origSize=$(du -b "$inFile" | cut -f1)
newSize=$(du -b "$outFile" | cut -f1)
diff=$(echo "scale=4; (($newSize - $origSize)/$origSize)*100" | bc | sed -r 's/0{2}$//')
origHuman="$(du -h "$inFile" | cut -f1)"
newHuman="$(du -h "$outFile" | cut -f1)"
if (( $(echo "$diff < 0" | bc) )); then
{
echo "---------------------------"
echo -e "Orig Size: $origHuman // New Size: $newHuman // ${CGRN}File decreased by $(echo "- $diff" | bc)%${CNORM}"
echo "---------------------------"
} | tee -a "$logFile"
else
{
echo "---------------------------"
echo -e "Orig Size: $origHuman // New Size: $newHuman // ${CRED}File increased by ${diff}%${CNORM}"
echo "---------------------------"
} | tee -a "$logFile"
fi
# Cleanup temp files and variables
rm "$metaFile" >/dev/null 2>&1
unset sMap aMap vMap
{
mkdir -p "$doneDir/${baseDir[$l]}"
sudo chgrp -R admins "$doneDir/${baseDir[$l]}"
mv "${fullName[$l]}" "$doneDir/${baseDir[$l]}/"
} >> "$traceLog" 2>&1
logIt "outFile = $outFile"
sudo chown $user:$group "$outFile"
sudo chmod 0664 "$outFile"
fi
return $STATUS
}
## MAIN
traceIt $LINENO " MAIN " " info " "*** START OF NEW RUN ***"
echo -e "${CWHT}\nStarting run of ${CPUR}Video Converter 2${CNORM}"
umask 002
displayIt "Collecting list of files to process"
getFiles
killWait $?
l=0
while (( l < ${#fullName[@]})) && (( l < 50 )); do
traceIt $LINENO " MAIN " " info " "START OF LOOP: $((l+1)) of ${#fullName[*]}"
traceIt $LINENO " MAIN " " info " "baseName=${baseName[$l]}"
echo "" >> "$logFile"
logIt "v----------------------------------------------------------------v"
logIt "Start of ${baseName[$l]}"
logIt "------------------------------------------------------------------"
logIt "inFile=${fullName[$l]}"
echo -e "\nFile $((l+1)) of ${#fullName[*]}"
displayIt "Processing: " "${baseDir[$l]}/${baseName[$l]}"
sleep 1
probeIt "${fullName[$l]}"
killWait $?
displayIt "Normalizing audio track"
normalizeIt "${fullName[$l]}"
killWait $?
displayIt "Setting encode filters"
setOpts
killWait $?
getMeta "${baseName[$l]}"
encodeIt "${fullName[$l]}"
logIt "------------------------------------------------------------------"
logIt "End of ${baseName[$l]}"
logIt "^----------------------------------------------------------------^"
traceIt $LINENO " MAIN " " info " "END OF LOOP: $((l+1))"
echo "" >> "$traceLog"
echo -e " ${CGRN}Done${CNORM}"
((l++))
done