-
Notifications
You must be signed in to change notification settings - Fork 1
/
media_scripts.sh
executable file
·1333 lines (1230 loc) · 35.6 KB
/
media_scripts.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
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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
## ffmpeg batch script
## processes all files in a directory recursively
shopt -s nullglob # prevent null files
shopt -s globstar # for recursive for loops
## escape characters to set console colors
GREEN='\033[0;32m'
NOCOLOUR='\033[0m' # No Color
## common presets
# 4k preset, based on amazon fire specs
uhd_vopts="-tag:v hvc1 -c:v libx265 -preset medium -b:v 23000k -x265-params profile=main10:level=5.1:high-tier=0:asm=avx512"
uhd_vprofile=""
uhd_twopass="x265"
uhd_container="mkv"
#bluray video
br_vopts="-c:v libx264 -preset slow -b:v 10M"
br_vprofile="-profile:v high -level 4.0"
br_twopass="x264"
br_container="mkv"
# dvd video
dvd_vopts="-c:v libx264 -preset slow -b:v 3M"
dvd_vprofile="-profile:v baseline -level 3.0"
dvd_twopass="x264"
dvd_container="mkv"
# audio presets
surround_aopts="-filter:a loudnorm -c:a eac3 -b:a 640k -ar 48k"
stereo_aopts="-filter:a loudnorm -c:a eac3 -b:a 192k -ar 48k"
# common filter for deinterlacing
deinterlace_filter="-vf \"bwdif\""
## static flags
vmaps="-map 0:v:0"
verbosity="-hide_banner -v fatal -stats"
vobsub_flags="--lang en" # use english language for OCR
global_metadata="-map_metadata -1" # wipe all input metadata as it often has errors
sub_metadata=""
video_metadata=""
audio_metadata=""
other_opts="-nostdin -max_muxing_queue_size 1024 -reserve_index_space 200k"
# place to dump original files once complete
old_files="$(readlink -f "old_files")"
## mode and program flow variables
mode=""
autosubs=false;
map_all_eng=false;
using_libx264=false;
using_libx265=false;
lopts="" # length of time to encode
parallel=false
onefile=false
preview=false
## ffmpeg flags, these are base values and are set up
## by user prompts or auto mode logic
inflags=""
vopts="-c:v copy"
vprofile=""
twopass=none
filters=""
aopts="-c:a copy"
amaps="-map 0:a:0"
smaps=""
container="mkv"
format="matroska"
autosubs=true
verbose_mode=false
################################################################################
# function usage()
# print usage to screen
# TODO update this for recently added functions
################################################################################
usage () {
echo "General Usage:"
echo "media_scripts <in_dir> <out_dir>"
echo "if out_dir is omitted then a directory called finished will be created"
echo "when converting vobsubs to SRT the output files will always be in the same"
echo "directory as the original files."
echo " "
echo "show this help message:"
echo "media_scripts -h"
echo "media_scripts --help"
exit 0
}
################################################################################
# function main()
# top level function, serves to prompt user for what to do, then run a loop
# to process all files in indir
################################################################################
main () {
# check arguments
if [ "$1" == "-h" ]; then
usage
exit 0
elif [ "$1" == "--help" ]; then
usage
exit 0
elif [ "$#" -eq 2 ]; then
outdir="$(readlink -f "$2")"
else
outdir="$(readlink -f "finished")"
fi
# grab input and output directories
indir="$(readlink -f "$1")"
echo ""
# check arguments were given
if [ -f "$indir" ]; then
echo "acting on just one file"
echo "$indir"
onefile=true
elif [ -d "$indir" ]; then
echo "acting on input directory:"
echo "$indir"
onefile=false
else
echo "error, input is neither file nor directory"
echo "$indir"
exit 1
fi
echo "output directory:"
echo $outdir
echo " "
echo "what do you want to do?"
echo " "
echo "FFMPEG options, all auto-embed SRT subtitles if available:"
echo "1) FULL AUTO (experimental)"
echo "2) custom mkv"
echo " "
echo "3) Copy Video & Copy Audio1 (embed srt subtitle)"
echo "4) Copy Video & Encode Audio1 as 5.1 EAC3"
echo "5) 2-pass 10M x264 & Encode Audio1 as 5.1 EAC3 (BR medium preset)"
echo "6) 2-pass 7M x264 & Encode Audio1 as 5.1 EAC3 (BR low preset)"
echo "7) 2-pass 3M x264 deinterlace & Copy Audio1 (DVD preset)"
echo " "
echo "8) remux mp4 to mkv"
echo "9) custom mp4"
echo " "
echo "MKVEXTRACT options"
echo "10) extract first subtitle"
echo "11) extract second subtitle"
echo "12) extract all subtitles"
echo " "
echo "VOBSUB2SRT options"
echo "13) OCR DVD idx/sub to SRT"
echo "14) OCR DVD idx/sub to SRT in Parallel"
echo " "
echo "BDSUP2SUB options"
echo "15) Convert BR sup to idx/sub"
echo "16) Extract and OCR BluRay sup 1"
echo "17) Extract and OCR BluRay sup 2"
echo "18) Extract and OCR BluRay sup 1 forced and sup 2 regular"
echo ""
echo
echo "enter numerical selection"
read n
case $n in
1) # full auto
mode="full_auto"
;;
2) # custom mkv
mode="ffmpeg_custom"
;;
3) #Copy Video & Copy Audio1 (embed srt subtitle)"
vopts="-c:v copy"
amaps="-map 0:a:0"
aopts="-c:a copy"
mode="ffmpeg_preset"
;;
4) # Copy Video & Encode Audio1 as 5.1 EAC3
vopts="-c:v copy"
amaps="-map 0:a:0"
aopts="$surround_aopts"
mode="ffmpeg_preset"
;;
5) # 2-pass 10M x264 & Encode Audio1 as 5.1 EAC3 (BR medium preset)
vopts="$br_vopts"
vprofile="$br_vprofile"
using_libx264=true;
twopass="x264";
amaps="-map 0:a:0"
aopts="$surround_aopts"
mode="ffmpeg_preset"
;;
6) # 2-pass 7M x264 & Encode Audio1 as 5.1 EAC3 (BR low preset)
vopts="-c:v libx264 -preset slow -b:v 7M"
vprofile="$br_vprofile"
using_libx264=true;
twopass="x264";
amaps="-map 0:a:0"
aopts="$surround_aopts"
mode="ffmpeg_preset"
;;
7) # 2-pass 3M x264 deinterlace & Copy Audio1 (DVD medium)
vopts="$dvd_vopts"
filters="-vf \"bwdif\""
vprofile="$dvd_profile"
using_libx264=true;
twopass="x264";
amaps="-map 0:a:0"
aopts="-c:a copy"
mode="ffmpeg_preset"
;;
8) # remux mp4 to mkv
vopts="-c:v copy"
amaps="-map 0:a"
aopts="-c:a copy"
autosubs=false
smaps="-map 0:s\?"
sopts="-c:s srt"
mode="ffmpeg_preset"
;;
9) # custom mp4
container="mp4"
format="mp4"
sopts="-c:s mov_text"
mode="ffmpeg_custom"
other_opts="-nostdin"
;;
# "MKVEXTRACT options"
10) # extract_first_sub
mode="mkvextract"
which_sub="1"
;;
11 ) # extract_second_sub
mode="mkvextract"
which_sub="2"
;;
12 ) # extract_all_subs
mode="mkvextract"
which_sub="all"
;;
# "VOBSUB2SRT options"
13) # OCR DVD subs to SRT
mode="vobsub2srt"
;;
14) # OCR DVD subs to SRT in parallel
mode="vobsub2srt"
parallel=true
;;
# "BDSUP2SUB options"
15) # convert BR sup to idx/sub
mode="bdsup2sub"
;;
16) # Extract and OCR BluRay sub 1
mode="bd2srt1"
;;
17) # Extract and OCR BluRay sub 1
mode="bd2srt2"
;;
18) # Extract and OCR BluRay sup 1 forced and sup 2 regular
mode="bd2srtforcedandreg"
;;
*)
echo "invalid option"
exit;;
esac
# extra questions for custom ffmpeg profile
if [ $mode == "ffmpeg_custom" ]; then
# ask video codec question
echo " "
echo "Which Video codec to use?"
select opt in "copy" "x265_2pass_4k_23mbit_5.1" "x264_2pass_10M_L4.0" "x264_2pass_7M_L4.0" "x264_2pass_4M_L4.0" "x264_2pass_3M_L3.0" "x264_2pass_1M_L3.0" "x264_2pass_1_3M_L3.0" "x264_rf18_L4.0" "x264_rf20_L4.0" "x265_rf21" "x265_2pass_1080_7mbit_4.0"; do
case $opt in
copy )
vcopy="true"
vopts="-c:v copy"
break;;
x265_2pass_4k_23mbit_5.1 )
vopts="$uhd_vopts"
vprofile="$uhd_vprofile"
twopass="$uhd_twopass"
container="$uhd_container"
break;;
x264_2pass_10M_L4.0 )
vopts="-c:v libx264 -preset slow -b:v 10000k"
vprofile="$br_vprofile"
using_libx264=true;
twopass="x264";
break;;
x264_2pass_7M_L4.0 )
vopts="-c:v libx264 -preset slow -b:v 7000k"
vprofile="$br_vprofile"
using_libx264=true;
twopass="x264";
break;;
x264_2pass_4M_L4.0 )
vopts="-c:v libx264 -preset slow -b:v 4000k"
vprofile="$br_vprofile"
using_libx264=true;
twopass="x264";
break;;
x264_2pass_3M_L3.0 )
vopts="$dvd_vopts"
vprofile="$dvd_vprofile"
using_libx264=true;
twopass="x264";
break;;
x264_2pass_1M_L3.0 )
vopts="-c:v libx264 -preset slow -b:v 1000k"
vprofile="$dvd_vprofile"
using_libx264=true;
twopass="x264";
break;;
x264_2pass_1_3M_L3.0 )
vopts="-c:v libx264 -preset slow -b:v 1300k"
vprofile="$dvd_vprofile"
using_libx264=true;
twopass="x264";
break;;
x264_rf18_L4.0 )
vopts="-c:v libx264 -preset slow -crf 18"
vprofile="$br_vprofile"
using_libx264=true;
break;;
x264_rf20_L4.0 )
vopts="-c:v libx264 -preset slow -crf 20"
vprofile="$br_vprofile"
using_libx264=true;
break;;
x265_rf21 )
vopts="-c:v libx265 -preset slow -x265-params profile=main10:crf=21:high-tier=1"
vprofile=""
break;;
x265_2pass_1080_7mbit_4.0 )
vopts="-tag:v hvc1 -c:v libx265 -preset medium -b:v 7000k -x265-params profile=main:level=4.0:high-tier=0"
vprofile=""
twopass="x265"
container="mkv"
break;;
*)
echo "invalid option"
esac
done
if [ "$vcopy" != "true" ]; then
# ask delinterlacing filter question
echo " "
echo "use videofilter?"
echo "cropping takes off 2 pixels from top and bottom which removes"
echo "some interlacing artifacts from old dvds"
select opt in "none" "scale_to_1080" "scale_to_720" "deinterlace" "deinterlace_crop" "hflip"; do
case $opt in
none )
filters=""
break;;
scale_to_1080 )
filters="-vf scale=1920:-1"
break;;
scale_to_720 )
filters="-vf scale=1280:-1"
break;;
deinterlace )
filters="-vf \"bwdif\""
break;;
deinterlace_crop )
filters="-vf \"crop=in_w:in_h-4:0:2, bwdif\""
break;;
hflip )
filters="-vf \"hflip\""
break;;
*)
echo "invalid option"
esac
done
fi
# ask audio tracks question
echo " "
echo "Which audio tracks to use?"
#echo "note, mapping all english audio tracks also maps all english subtitles"
#echo "and subtitle mode is forced to auto"
select opt in "first" "second" "all_english" "all" "first+commentary" "none" ; do
case $opt in
all_english)
amaps="-map 0:a:m:language:eng"
map_all_english=true
break;;
first )
amaps="-map 0:a:0"
break;;
second )
amaps="-map 0:a:1"
break;;
all )
amaps="-map 0:a"
break;;
first+commentary )
amaps="-map 0:a:0 -map 0:a:1"
audio_metadata="-metadata:s:a:1 Title=\"English\" -metadata:s:a:1 Title=\"Commentary\" -metadata:s:a language=eng"
break;;
none )
amaps=""
break;;
*)
echo "invalid option"
esac
done
# ask audio codec question
echo " "
echo "Which audio codec to use?"
select opt in "eac3_5.1" "eac3_2.0" "copy" "none"; do
case $opt in
eac3_5.1 )
aopts="$surround_aopts"
break;;
eac3_2.0 )
aopts="$stereo_aopts"
break;;
copy )
aopts="-c:a copy"
break;;
none )
aopts=""
break;;
*)
echo "invalid option"
esac
done
# ask subtitle question
echo " "
echo "What to do with subtitles?"
echo "Auto will select external srt if available"
echo "otherwise will grab first embedded sub"
select opt in "auto" "keep_first" "keep_second" "internal_forced_external_regular" "keep_all" "none"; do
case $opt in
auto)
autosubs=true
break;;
keep_all )
smaps="-map 0:s"
sopts="-c:s copy"
autosubs=false
break;;
keep_first )
smaps="-map 0:s:0"
sopts="-c:s copy"
autosubs=false
break;;
keep_second )
smaps="-map 0:s:1"
sopts="-c:s copy"
autosubs=false
break;;
internal_forced_external_regular )
smaps="-map 0:s:0 -map 1:s:0"
sopts="-c:s copy -disposition:s:0 +default+forced -metadata:s:s:0 Title=\"forced\" -metadata:s:s:0 language=eng -metadata:s:s:1 Title=\"English\" -metadata:s:s:1 language=eng"
autosubs="forced"
break;;
none )
smaps=""
autosubs=false
break;;
*)
echo "invalid option"
esac
done
# ask run options
echo " "
echo "preview ffmpeg command, do a 1 minute sample, 60 second sample, or run everything now?"
select opt in "preview" "run_now" "run_verbose" "sample10" "sample60" "sample60_middle" "run_now_no_chapters"; do
case $opt in
preview )
preview=true
break;;
run_now )
break;;
run_verbose )
verbose_mode=true
verbosity="-stats"
break;;
sample10 )
verbose_mode=true
lopts="-t 00:00:10.0"
break;;
sample60 )
verbose_mode=true
lopts="-t 00:01:00.0"
break;;
sample60_middle )
verbose_mode=true
lopts="-ss 00:05:00.0 -t 00:01:00.0"
break;;
run_now_no_chapters )
verbose_mode=true
vmaps="$vmaps -map_chapters -1"
break;;
*)
echo "invalid option"
esac
done
## /end if [ $ffmpeg_custom == true ]
## for all other modes just ask if we want to run or not
else
# ask run options when extracting just subtitles
echo " "
echo "preview command, or run now?"
echo "genpts generates pts is DTS is present, run if you get errors"
select opt in "preview" "run_now" "run_verbose" "run_genpts"; do
case $opt in
preview )
verbose_mode=false
preview=true
break;;
run_now )
verbose_mode=false
break;;
run_verbose )
verbose_mode=true
verbosity="-stats"
vobsub_flags="$vobsub_flags --verbose"
break;;
run_genpts )
verbose_mode=true
inflags="-fflags +genpts"
break;;
*)
echo "invalid option"
esac
done
fi
## Finally do stuff!!!
time_start_all=$(date +%s)
## process one file or loop through all input files
if [ $onefile == true ]; then
echo "onefile mode"
process_one_file "$indir"
else
#set IFS to fix spaces in file names
SAVEIFS=$IFS
#IFS=$(echo -en "\n\b")
IFS=$(echo -en "\n\b")
if [ $mode == "vobsub2srt" ]; then
FILES="$(find "$indir" -type f -iname \*.idx | sort)"
elif [ $mode == "bdsup2sub" ]; then
FILES="$(find "$indir" -type f -iname \*.sup | sort)"
else
FILES="$(find "$indir" -type f -iname \*.mkv -o -iname \*.mp4 -o -iname \*.avi | sort)"
fi
echo "files to be processed:"
echo "$FILES"
if [ $parallel == true ]; then
echo "starting parallel"
find "$indir" -type f -iname \*.idx | cut -f 1 -d '.' | parallel vobsub2srt
else
while read ffull; do
process_one_file "$ffull"
done < <(echo "$FILES")
fi
IFS=$SAVEIFS
fi
time_end_all=$(date +%s)
dt_all=$(($time_end_all-$time_start_all))
echo ""
print_exec_time "total execution time:" "$dt_all"
} # end main()
################################################################################
# print_exec_timeprint_eprint_e
# for printing how long it took to execute a segment, prints seconds and h/m/s
# first argument is the prefix string, second is the time in seconds
################################################################################
print_exec_time () {
dt="$2"
((h=$dt/3600))
((m=($dt%3600)/60))
((s=$dt%60))
printf "%s %02d:%02d:%02d\n" "$1" $h $m $s
}
################################################################################
# runs ffmpeg with all settings from variables defined by parent functions
# called by process_one_file()
################################################################################
run_ffmpeg () {
# add container to the end
outfull="$out_no_ext.$container"
# skip if file is already complete
if [ -f "$outfull" ]; then
echo "WARNING already exists: $outfull"
echo "erasing and starting again"
rm -f "$outfull"
fi
local ins=" $inflags -i \"$ffull\""
# if using autosubs, check if externals exist
local foundforced=false
if [ "$autosubs" == "forced" ]; then
ins="$ins -i \"$fpath.srt\""
elif [ $autosubs == true ]; then
if [ -f "$fpath.forced.srt" ]; then
ins="$ins -i \"$fpath.forced.srt\""
local foundforced=true
local smaps="-map 1:s"
local sopts="-c:s srt"
local sub_metadata="-disposition:s:0 +default+forced -metadata:s:s:0 Title=\"forced\" -metadata:s:s:0 language=eng"
fi
if [ -f "$fpath.srt" ]; then
ins="$ins -i \"$fpath.srt\""
if [ $foundforced == true ]; then
smaps="$smaps -map 2:s"
sub_metadata="$sub_metadata -metadata:s:s:1 Title=\"English\" -metadata:s:s:1 language=eng"
else
local smaps="-map 1:s"
local sopts="-c:s srt"
local sub_metadata="-metadata:s:s:0 Title=\"English\" -metadata:s:s:0 language=eng"
fi
else
local smaps="-map 0:s:0"
local sopts="-c:s srt"
local sub_metadata=""
fi
fi
if [ "$container" == "mp4" ]; then
format="mp4"
sopts="-c:s mov_text"
else
format="matroska"
fi
#combine options into ffmpeg string
local maps="$vmaps $amaps $smaps"
local metadata="$global_metadata $video_metadata $audio_metadata $sub_metadata"
# construct ffmpeg command depending on mode
if [ "$twopass" == "x264" ]; then
local command1="nice -n 19 ffmpeg -y -an -sn $verbosity $ins $other_opts $vmaps $vopts $vprofile -pass 1 -passlogfile \"/tmp/$fname\" $lopts $filters -f $format /dev/null"
local command2="nice -n 19 ffmpeg -n $verbosity $ins $other_opts $maps $vopts $vprofile -pass 2 -passlogfile \"/tmp/$fname\" $lopts $filters $aopts $sopts $metadata \"$outfull\""
elif [ "$twopass" == "x265" ]; then
local command1="nice -n 19 ffmpeg -y -an -sn $verbosity $ins $other_opts $vmaps $vopts:pass=1:stats=\"/tmp/$fname\" $lopts $filters -f $format /dev/null"
local command2="nice -n 19 ffmpeg -n $verbosity $ins $other_opts $maps $vopts:pass=2:stats=\"/tmp/$fname\" $lopts $filters $aopts $sopts $metadata \"$outfull\""
elif [ "$twopass" == "none" ]; then
local command1="nice -n 19 ffmpeg -n $verbosity $ins $other_opts $maps $vopts $vprofile $lopts $filters $aopts $sopts $metadata \"$outfull\""
else
echo "ERROR, twopass variable should be none, x264, or x265"
exit 1
fi
# off we go!!
if [ "$preview" == true ]; then
if [ "$twopass" == none ]; then
echo "ffmpeg command:"
echo "$command1"
else
echo "pass 1 command:"
echo "$command1"
echo "pass 2 command"
echo "$command2"
fi
else
mkdir -p "$outdirfull" # make sure output directory exists
echo " "
echo "starting:"
echo "in: $ffull"
echo "out: $outfull"
echo " "
time_start_ffmpeg=$(date +%s)
## single pass execution
if [ "$twopass" == "none" ]; then
if [ "$verbose_mode" == true ]; then
echo "ffmpeg command to run:"
echo "$command1"
echo ""
fi
if eval "$command1"; then
echo "success!"
echo " "
else
echo " "
echo "FFMPEG FAILURE"
echo "tried to run:"
echo "$command1"
exit 1
fi
else
echo "starting pass 1 of 2"
if [ "$verbose_mode" == true ]; then
echo "Pass 1 Command:"
echo "$command1"
echo ""
echo "Pass 2 Command:"
echo "$command2"
echo ""
fi
if eval "$command1"; then
echo "finished pass 1"
else
echo " "
echo "FFMPEG FAILURE"
echo "tried to run:"
echo "$command1"
exit 1
fi
echo "starting pass 2 of 2"
if eval "$command2"; then
# finished, remove log file
rm "/tmp/$fname"*
echo "finished pass 2"
else
echo " "
echo "FFMPEG FAILURE"
echo "tried to run:"
echo "$command2"
exit 1
fi
fi
## cleanup by moving completed original files
mkdir -p "$old_files_full" 2> /dev/null
for f in "$fpath"*
do
echo "moving $f to $old_files_full"
mv "$f" "$old_files" 2> /dev/null
done
time_end_ffmpeg=$(date +%s)
dt_ffmpeg=$((time_end_ffmpeg-time_start_ffmpeg))
print_exec_time "ffmpeg execution time:" "$dt_ffmpeg"
echo "done with $fname"
echo " "
fi
} ## end run_ffmpeg()
################################################################################
# vobsub2srt for OCR converting dvd idx/sub vonsubs to srt
################################################################################
run_vobsub2srt () {
command="vobsub2srt $vobsub_flags \"$fpath\""
if [ $preview == true ]; then
echo "would run:"
echo "$command"
echo " "
else
echo "running vobsub2srt"
# skip if file is already complete
if [ -f "$fpath.srt" ]; then
echo "WARNING already exists: $fpath.srt"
echo "erasing and starting again"
rm -f "fpath.srt"
fi
if eval "$command"; then
# cleanup
rm -f "$fpath.idx"
rm -f "$fpath.sub"
echo " vobsub2srt success!"
echo " "
else
echo " "
echo "vobsub2srt failure"
echo " "
exit 1
fi
fi
}
################################################################################
# bdsup2sub converts bluray image subtitles (sup) to dvd subtitles idx/sub
################################################################################
run_bdsup2sub () {
command="bdsup2sub \"$fpath.sup\" -o \"$fpath.idx\" > /dev/null"
if [ $preview == true ]; then
echo "would run:"
echo "$command"
echo " "
else
echo "running bdsup2sub"
# skip if file is already complete
if [ -f "$fpath.idx" ]; then
echo "WARNING already exists: $fpath.idx"
echo "erasing and starting again"
rm -f "fpath.idx"
fi
if [ -f "$fpath.sub" ]; then
echo "WARNING already exists: $fpath.sub"
echo "erasing and starting again"
rm -f "fpath.sub"
fi
if eval "$command"; then
# done, remove old sup fie
rm "$fpath.sup"
echo "bdsup2sub success!"
echo " "
else
echo " "
echo "bdsup2sub failure"
echo " "
exit
fi
fi
}
################################################################################
# bd2srt extracts bluray sub and converts to srt
# first argument is which subtitle to extract
################################################################################
run_bd2srt() {
run_mkvextract $1
run_bdsup2sub
run_vobsub2srt
}
################################################################################
# mkvextract for extracting subtitles
# first arguemnt is which to extract, "first" "second" or "all"
################################################################################
run_mkvextract () {
echo "running mkvextract tracks: $1"
case "$1" in
1)
which_sub="1"
;;
2)
which_sub="2"
;;
all)
which_sub=""
;;
*)
echo "error: run_mkvextract needs first argument 1,2 or all"
echo "received $1"
exit 1
esac
command="mkvextract tracks \"$ffull\""
# count number of each type of sub so we know if it's necessary
# to number the output files
local numpgs=0
local numsrt=0
local numvob=0
local numother=0
local counter=0
if [ "$which_sub" == "all" ]; then
while read subline
do
if [[ "$subline" == *"SRT"* ]]; then
numsrt=$((numsrt+1))
elif [[ "$subline" == *"PGS"* ]]; then
numpgs=$((numpgs+1))
elif [[ "$subline" == *"VobSub"* ]]; then
numvob=$((numvob+1))
else
numother=$((numother+1))
fi
done < <(mkvmerge -i "$ffull" | grep 'subtitles' ) # process substitution
fi
# Find out which tracks contain the subtitles
while read subline
do
counter=$((counter+1))
# if extracting the second sub, skip the first
if [ "$which_sub" == "2" ]; then
if [ $counter -eq 1 ]; then
continue;
fi
fi
# Grep the number of the subtitle track
tracknumber=`echo $subline | egrep -o "[0-9]{1,2}" | head -1`
# add track to the command
if [[ $subline == *"SRT"* ]]; then
if [[ "$numsrt" -lt 2 ]]; then
command="$command $tracknumber:\"$fpath.srt\""
else
command="$command $tracknumber:\"$fpath.$tracknumber.srt\""
fi
elif [[ "$subline" == *"PGS"* ]]; then
if [[ "$numpgs" -lt 2 ]]; then
command="$command $tracknumber:\"$fpath.sup\""
else
command="$command $tracknumber:\"$fpath.$tracknumber.sup\""
fi
elif [[ "$subline" == *"VobSub"* ]]; then
if [[ "$numvob" -lt 2 ]]; then
command="$command $tracknumber:\"$fpath\""
else
command="$command $tracknumber:\"$fpath.$tracknumber\""
fi
else
if [[ "$numsrt" -lt 2 ]]; then
command="$command $tracknumber:\"$fpath.subtitle\""
else
command="$command $tracknumber:\"$fpath.$tracknumber.subtitle\""
fi
fi
# if only getting the first sub we can stop here
if [ "$which_sub" == "1" ]; then
break;
elif [ "$which_sub" == "2" ]; then
if [ "$counter" == "2" ]; then
break;
fi
fi
done < <(mkvmerge -i "$ffull" | grep 'subtitles' ) # process substitution
# finished constructing command by silencing mkvextract
command="$command > /dev/null 2>&1"
if [ "$preview" == true ]; then
echo "would run:"
echo "$command"
echo " "
else
echo "starting $ffull"
mkdir -p "$outdirfull" # make sure output directory exists
if eval "$command"; then
echo "success!"
echo " "
else
echo " "
echo "mkvextract failure"
echo " "
exit
fi
fi
}
################################################################################
# run_full_auto
# reads file ffull set by parent function process_one_file and does what it
# sees fit to make it plex-friendly
################################################################################
run_full_auto () {
## grab info
#local probe_opts="-v error -of default=noprint_wrappers=1:nokey=1" # common options for ffprobe
local orig_vcodec=`ffprobe -i "$ffull" -v error -of default=noprint_wrappers=1:nokey=1 -select_streams v:0 -show_entries stream=codec_name`
local orig_width=`ffprobe -i "$ffull" -v error -of default=noprint_wrappers=1:nokey=1 -select_streams v:0 -show_entries stream=width`
local orig_field_order=`ffprobe -i "$ffull" -v error -of default=noprint_wrappers=1:nokey=1 -select_streams v:0 -show_entries stream=field_order`
local orig_vbr=`ffprobe -i "$ffull" -v error -of default=noprint_wrappers=1:nokey=1 -select_streams v:0 -show_entries format=bit_rate`
local orig_acodec1=`ffprobe -i "$ffull" -v error -of default=noprint_wrappers=1:nokey=1 -select_streams a:0 -show_entries stream=codec_name`
local orig_acodec2=`ffprobe -i "$ffull" -v error -of default=noprint_wrappers=1:nokey=1 -select_streams a:1 -show_entries stream=codec_name`
local orig_alang1=`ffprobe -i "$ffull" -v error -of default=noprint_wrappers=1:nokey=1 -select_streams a:0 -show_entries stream_tags=language`
local orig_alang2=`ffprobe -i "$ffull" -v error -of default=noprint_wrappers=1:nokey=1 -select_streams a:1 -show_entries stream_tags=language`
local orig_achan1=`ffprobe -i "$ffull" -v error -of default=noprint_wrappers=1:nokey=1 -select_streams a:0 -show_entries stream=channels`
local orig_achan2=`ffprobe -i "$ffull" -v error -of default=noprint_wrappers=1:nokey=1 -select_streams a:1 -show_entries stream=channels`
local orig_scodec1=`ffprobe -i "$ffull" -v error -of default=noprint_wrappers=1:nokey=1 -select_streams s:0 -show_entries stream=codec_name`
local orig_scodec2=`ffprobe -i "$ffull" -v error -of default=noprint_wrappers=1:nokey=1 -select_streams s:1 -show_entries stream=codec_name`
local orig_slang1=`ffprobe -i "$ffull" -v error -of default=noprint_wrappers=1:nokey=1 -select_streams s:0 -show_entries stream_tags=language`
local orig_slang2=`ffprobe -i "$ffull" -v error -of default=noprint_wrappers=1:nokey=1 -select_streams s:1 -show_entries stream_tags=language`
local orig_sforced1=`ffprobe -i "$ffull" -v error -of default=noprint_wrappers=1:nokey=1 -select_streams s:1 -show_entries disposition=forced`
if [ "$orig_scodec1" == "" ]; then orig_scodec1="none"; fi
if [ "$orig_acodec2" == "" ]; then orig_acodec2="none"; fi
if [ "$orig_scodec2" == "" ]; then orig_scodec2="none"; fi
#decide interlace mode
if [ "$orig_field_order" == "tt" ] || [ "$orig_field_order" == "bb" ]; then
interlaced=true
elif [ "$orig_field_order" == "progressive" ]; then
interlaced=false
# HEVC video shows as 'unknown but is really progressive
elif [ "$orig_vcodec" == "hevc" ]; then