-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdrp
executable file
·2170 lines (2068 loc) · 64.2 KB
/
drp
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
# drp (DVD Rip Prep) bash script - Feb. 6 2025
#
# This script attempts to take an MKV file produced by MakeMKV from a DVD source
# and output a new file without telecine or interlacing and at the correct frame
# rate. The goal here is to do the minimum amount of processing necessary, since
# just generally applying detelecine and deinterlacing where it isn't necessary
# might reduce the image quality. In most cases, the script should give you a
# video that consists of more than 99% progressive frames without introducing
# motion irregularities resulting from dropped or duplicated frames.
# Settings
WRITELOGS=1
CONVPAL=1
ALLOWLOSS="0.07"
HTCTHRESH="2.5"
INTLTHRESH="50"
OUTPUTJOIN=1
OUTPUTSEGS=0
MERGEMKV=1
RATELOCK="0.0001"
STRICT=1
AUTOMAP=1
KEEPTEMP=0
FROUTS=(24 30 60 120)
DEBUG=0
DEBUGSLOW=0
MAPPER="$(dirname "$0")/smap"
OUTDIR="CHANGEME"
# Best not to change the following settings unless you know what you're doing
TEMPROOT="$OUTDIR/00DRP"
TEMPDIR="$OUTDIR/00DRP/$(date +%s)"
LOGDIR="$TEMPDIR/LOGS"
STAGE1DIR="$TEMPDIR/STAGE1"
STAGE2DIR="$TEMPDIR/STAGE2"
STAGE3DIR="$TEMPDIR/STAGE3"
PRELOG="/tmp/prelog.$(date +%s)"
FILMFPS="$(echo "24000 / 1001" |bc -l)"
VIDFPS="$(echo "30000 / 1001" |bc -l)"
FDBLFPS="$(echo "48000 / 1001" |bc -l)"
IVIDFPS="$(echo "60000 / 1001" |bc -l)"
MAXFPS="$(echo "120000 / 1001" |bc -l)"
PALFPS="25"
IPALFPS="50"
IPALCONVFPS="$(echo "48000 / 1001" |bc -l)"
# End of settings
function setup {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function setup."
echo "Checking dependencies..."
if ! command -v ffmpeg >/dev/null 2>&1; then
echo "ERROR: Can't find ffmpeg."
exit 1
fi
if ! command -v ffprobe >/dev/null 2>&1; then
echo "ERROR: Can't find ffprobe."
exit 1
fi
if ! command -v mkvmerge >/dev/null 2>&1; then
echo "ERROR: Can't find mkvmerge."
exit 1
fi
if ! command -v bc >/dev/null 2>&1; then
echo "ERROR: Can't find bc."
exit 1
fi
echo "Checking directories..."
if [ ! -d "$OUTDIR" ] && [ "$dryrun" != 1 ]; then
echo "ERROR: OUTDIR ($OUTDIR) does not exist."
exit 1
fi
if [ ! -d "$TEMPROOT" ]; then
mkdir "$TEMPROOT" || exit 1
fi
if [ ! -d "$TEMPDIR" ]; then
mkdir "$TEMPDIR" || exit 1
fi
if [ ! -d "$LOGDIR" ] && [ "$dryrun" != 1 ]; then
mkdir "$LOGDIR" || exit 1
fi
if [ ! -d "$STAGE1DIR" ] && [ "$dryrun" != 1 ]; then
mkdir "$STAGE1DIR" || exit 1
fi
if [ ! -d "$STAGE2DIR" ] && [ "$dryrun" != 1 ]; then
mkdir "$STAGE2DIR" || exit 1
fi
if [ ! -d "$STAGE3DIR" ] && [ "$dryrun" != 1 ]; then
mkdir "$STAGE3DIR" || exit 1
fi
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function setup."
}
function reportsettings {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function reportsettings."
echo "Reporting settings..."
if [ "$WRITELOGS" = 1 ]; then
echo "Logging is enabled." |tee -a "$PRELOG"
else
echo "Logging is disabled" |tee -a "$PRELOG"
fi
if [ "$CONVPAL" = 1 ]; then
echo "PAL conversion is enabled." |tee -a "$PRELOG"
else
echo "PAL conversion is disabled" |tee -a "$PRELOG"
fi
if [ "$anim" = 1 ]; then
echo "Animation mode is enabled." |tee -a "$PRELOG"
else
echo "Animation mode is disabled" |tee -a "$PRELOG"
fi
if [ "$OUTPUTJOIN" = 1 ]; then
echo "Outputting rejoined video is enabled." |tee -a "$PRELOG"
else
echo "Outputting rejoined video is disabled." |tee -a "$PRELOG"
fi
if [ "$OUTPUTSEGS" = 1 ]; then
echo "Outputting unjoined segments is enabled." |tee -a "$PRELOG"
else
echo "Outputting unjoined segments is disabled." |tee -a "$PRELOG"
fi
if [ "$MERGEMKV" = 1 ]; then
echo "Merging output with the original MKV is enabled." |tee -a "$PRELOG"
else
echo "Merging output with the original MKV is disabled" |tee -a "$PRELOG"
fi
if [ "$AUTOMAP" = 1 ]; then
echo "Automatic mapping is enabled, using $MAPPER." |tee -a "$PRELOG"
else
echo "Automatic mapping is disabled" |tee -a "$PRELOG"
fi
if [ "$dryrun" = 1 ]; then
echo "Doing a dry run..." |tee -a "$PRELOG"
fi
if [ "$rerun" = 1 ]; then
echo "Continuing previous run in directory $intmp." |tee -a "$PRELOG"
fi
echo "OUTDIR is $OUTDIR" |tee -a "$PRELOG"
echo "TEMPDIR is $TEMPDIR" |tee -a "$PRELOG"
echo "LOGDIR is $LOGDIR" |tee -a "$PRELOG"
echo "STAGE1DIR is $STAGE1DIR" |tee -a "$PRELOG"
echo "STAGE2DIR is $STAGE2DIR" |tee -a "$PRELOG"
echo "STAGE3DIR is $STAGE3DIR" |tee -a "$PRELOG"
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function reportsettings."
}
function cleanup {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function cleanup."
echo "Cleaning up temporary files and directories..."
if [ -d "$STAGE1DIR" ] && [ "$dryrun" != 1 ]; then
cd "$STAGE1DIR" || exit 1
if [ "$(pwd)" == "$STAGE1DIR" ]; then
for file in *; do
rm -f "$file"
done
fi
fi
if [ -d "$STAGE2DIR" ] && [ "$dryrun" != 1 ]; then
cd "$STAGE2DIR" || exit 1
if [ "$(pwd)" == "$STAGE2DIR" ]; then
for file in *; do
rm -f "$file"
done
fi
fi
if [ -d "$STAGE3DIR" ] && [ "$dryrun" != 1 ]; then
cd "$STAGE3DIR" || exit 1
if [ "$(pwd)" == "$STAGE3DIR" ]; then
for file in *; do
rm -f "$file"
done
fi
fi
if [ -d "$TEMPDIR" ]; then
cd "$TEMPDIR" || exit 1
if [ "$(pwd)" == "$TEMPDIR" ]; then
for file in *; do
if [ ! -d "$file" ]; then
rm -f "$file"
fi
done
fi
fi
if [ -f /tmp/idet.out ]; then
rm -f /tmp/idet.out
fi
if [ -f /tmp/idet.grep ]; then
rm -f /tmp/idet.grep
fi
cd "$rundir" || exit 1
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function cleanup."
}
function getresolution {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function getresolution."
local infile="$1"
invres=$(ffprobe -v error -select_streams v:0 -show_entries stream=height -of default=noprint_wrappers=1:nokey=1 "$infile")
echo "Vertical Resolution: $invres" |tee -a "$logfile"
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function getresolution."
}
function getstandard {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function getstandard."
if [ "$clistandard" == "NTSC" ] || [ "$clistandard" == "PAL" ]; then
standard="$clistandard"
elif [ "$invres" -gt "573" ] && [ "$invres" -lt "579" ]; then
standard="PAL"
elif [ "$invres" -gt "477" ] && [ "$invres" -lt "483" ]; then
standard="NTSC"
else
standard="UNKNOWN"
fi
echo "Video Standard: $standard" |tee -a "$logfile"
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function getstandard."
}
function gettrueframerate {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function gettrueframerate."
local infile="$1"
indurs=$(ffprobe -v error -select_streams v:0 -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$infile")
showindurs="$(printf "%.2f\n" "$indurs")"
echo "Duration: $showindurs" |tee -a "$logfile"
inframes=$(ffprobe -v error -select_streams v:0 -count_packets -show_entries stream=nb_read_packets -of csv=p=0 "$infile")
echo "Frame Count: $inframes" |tee -a "$logfile"
infrate=$(echo "$inframes / $indurs" |bc -l)
showinfrate="$(printf "%.3f\n" "$infrate")"
echo "Calculated Frame Rate: $showinfrate" |tee -a "$logfile"
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function gettrueframerate."
}
function getinterlace {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function getinterlace."
local infile="$1"
echo "Counting interlaced frames..." |tee -a "$logfile"
if [ -f "$TEMPDIR/idet.out" ]; then
rm -f "$TEMPDIR/idet.out"
fi
if [ -f "$TEMPDIR/idet.grep" ]; then
rm -f "$TEMPDIR/idet.grep"
fi
ffmpeg -an -sn \
-hide_banner \
-stats \
-filter:v idet \
-f null - \
-i "$infile" 2>"$TEMPDIR/idet.out"
grep -m3 "Parsed_idet" "$TEMPDIR/idet.out" >"$TEMPDIR/idet.grep"
idetneither=$(sed -n 1,1p "$TEMPDIR/idet.grep" |tr -d ' ' |cut -d: -f3 |cut -dT -f1)
idettop=$(sed -n 1,1p "$TEMPDIR/idet.grep" |tr -d ' ' |cut -d: -f4 |cut -dB -f1)
idetbottom=$(sed -n 1,1p "$TEMPDIR/idet.grep" |tr -d ' ' |cut -d: -f5)
idetsftff=$(sed -n 2,2p "$TEMPDIR/idet.grep" |tr -d ' ' |cut -d: -f3 |cut -dB -f1)
idetsfbff=$(sed -n 2,2p "$TEMPDIR/idet.grep" |tr -d ' ' |cut -d: -f4 |cut -dP -f1)
idetsfprog=$(sed -n 2,2p "$TEMPDIR/idet.grep" |tr -d ' ' |cut -d: -f5 |cut -dU -f1)
idetsfundet=$(sed -n 2,2p "$TEMPDIR/idet.grep" |tr -d ' ' |cut -d: -f6)
idetmftff=$(sed -n 3,3p "$TEMPDIR/idet.grep" |tr -d ' ' |cut -d: -f3 |cut -dB -f1)
idetmfbff=$(sed -n 3,3p "$TEMPDIR/idet.grep" |tr -d ' ' |cut -d: -f4 |cut -dP -f1)
idetmfprog=$(sed -n 3,3p "$TEMPDIR/idet.grep" |tr -d ' ' |cut -d: -f5 |cut -dU -f1)
idetmfundet=$(sed -n 3,3p "$TEMPDIR/idet.grep" |tr -d ' ' |cut -d: -f6)
echo "Non-repeating Frames: $idetneither" |tee -a "$logfile"
echo "Top Fields: $idettop" |tee -a "$logfile"
echo "Bottom Fields: $idetbottom" |tee -a "$logfile"
echo "TFF Fields: $idetsftff, $idetmftff"
echo "BFF Fields: $idetsfbff, $idetmfbff"
echo "Progressive Frames: $idetsfprog, $idetmfprog"
echo "Undetermined Frames: $idetsfundet, $idetmfundet"
case "$idettop" in
''|*[!0-9]*) idettop=0 ;;
*) ;;
esac
case "$idetbottom" in
''|*[!0-9]*) idetbottom=0 ;;
*) ;;
esac
htcfields=$((idettop + idetbottom))
pcthtc=$(echo "($htcfields / $inframes) * 100" |bc -l)
idetsfintl=$(echo "$idetsftff + $idetsfbff" |bc -l)
idetmfintl=$(echo "$idetmftff + $idetmfbff" |bc -l)
idetallintl=$(echo "$idetsfintl + $idetmfintl" |bc -l)
intlfields=$(echo "$idetallintl / 2" |bc -l)
pctintl=$(echo "($intlfields / $inframes) * 100" |bc -l)
showpctintl="$(printf "%.2f\n" "$pctintl")"
echo "Interlaced Percent: $showpctintl" |tee -a "$logfile"
showpcthtc="$(printf "%.2f\n" "$pcthtc")"
echo "Telecine Percent: $showpcthtc" |tee -a "$logfile"
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function getinterlace."
}
function getnomframerate {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function getnomframerate."
local infile="$1"
if [ "$stage" == 3 ]; then
if [ "$(echo "$infrate < 26" |bc -l)" -eq 1 ]; then
nomfrate="24"
else
nomfrate="30"
fi
else
pallow="$(echo "$PALFPS - ($RATELOCK * $PALFPS)" |bc -l)"
palhigh="$(echo "$PALFPS + ($RATELOCK * $PALFPS)" |bc -l)"
filmlow="$(echo "$FILMFPS - ($RATELOCK * $FILMFPS)" |bc -l)"
filmhigh="$(echo "$FILMFPS + ($RATELOCK * $FILMFPS)" |bc -l)"
vidlow="$(echo "$VIDFPS - ($RATELOCK * $VIDFPS)" |bc -l)"
vidhigh="$(echo "$VIDFPS + ($RATELOCK * $VIDFPS)" |bc -l)"
smcount=0
if [ "$mapped" = 1 ]; then
while read -r; do
smcount=$((smcount + 1))
done <"$mapfile"
fi
if [ "$smcount" -gt 0 ]; then
nomfrate="Mix"
elif [ "$standard" == "PAL" ]; then
if [ "$(echo "$infrate > $pallow" |bc -l)" -eq 1 ] && [ "$(echo "$infrate < $palhigh" |bc -l)" -eq 1 ]; then
if [ "$(echo "$pctintl < 10" |bc -l)" -eq 1 ]; then
nomfrate="25"
else
nomfrate="50"
fi
fi
elif [ "$standard" == "NTSC" ]; then
if [ "$(echo "$infrate >= $filmlow" |bc -l)" -eq 1 ] && [ "$(echo "$infrate <= $filmhigh" |bc -l)" -eq 1 ]; then
if [ "$(echo "$pctintl < 1" |bc -l)" -eq 1 ]; then
nomfrate="24"
else
nomfrate="Mix"
fi
elif [ "$(echo "$infrate >= $vidlow" |bc -l)" -eq 1 ] && [ "$(echo "$infrate <= $vidhigh" |bc -l)" -eq 1 ]; then
if [ "$(echo "$pctintl < 5" |bc -l)" -eq 1 ] && [ "$(echo "$pctintl < 1" |bc -l)" -eq 1 ]; then
nomfrate="30"
elif [ "$(echo "$pctintl < 80" |bc -l)" -eq 1 ] && [ "$(echo "$pctintl < 10" |bc -l)" -eq 1 ]; then
nomfrate="Mix"
else
nomfrate="30"
fi
else
nomfrate="Mix"
fi
else
nomfrate="Bad"
fi
fi
echo "Nominal Frame Rate: $nomfrate" |tee -a "$logfile"
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function getnomframerate."
}
function analyzentsc {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function analyzentsc."
local infile="$1"
if [ "$inframes" -le 6 ]; then
echo "Diagnosis: Undiagnosable short packet." |tee -a "$logfile"
echo "$infile,$invres,$infrate,$pctintl,$pcthtc,Short packet" >>"$diagfile"
runmethod="passthrough"
elif [ "$nomfrate" == "24" ]; then
if [ "$(echo "$pctintl > 1" |bc -l)" -eq 1 ]; then
echo "Diagnosis: Soft telecined with interlaced borders or transitions." |tee -a "$logfile"
echo "$infile,$invres,$infrate,$pctintl,$pcthtc,Partially interlaced video" >>"$diagfile"
runmethod="softinterlaced"
else
echo "Diagnosis: Soft telecined video at 24 FPS." |tee -a "$logfile"
echo "$infile,$invres,$infrate,$pctintl,$pcthtc,Soft Telecine 24FPS" >>"$diagfile"
runmethod="softtelecine"
fi
elif [ "$nomfrate" == "30" ]; then
if [ "$stage" = 1 ] && [ "$STRICT" = 1 ]; then
echo "Running the mixed method because STRICT processing is enabled." |tee -a "$logfile"
runmethod="mixed"
elif [ "$(echo "$pcthtc >= $HTCTHRESH" |bc -l)" -eq 1 ]; then
echo "Diagnosis: Hard telecined video at 24 FPS." |tee -a "$logfile"
echo "$infile,$invres,$infrate,$pctintl,$pcthtc,Hard Telecine 24FPS" >>"$diagfile"
runmethod="hardtelecine"
elif [ "$(echo "$pctintl > $INTLTHRESH" |bc -l)" -eq 1 ]; then
echo "Diagnosis: True, interlaced NTSC video at 30FPS." |tee -a "$logfile"
echo "$infile,$invres,$infrate,$pctintl,$pcthtc,True Interlaced 30FPS" >>"$diagfile"
runmethod="ntscinterlaced"
elif [ "$(echo "$pcthtc < 1" |bc -l)" -eq 1 ] && [ "$(echo "$pctintl < 1" |bc -l)" -eq 1 ]; then
echo "Diagnosis: Incorrectly deinterlaced hard telecine or black/still transition." |tee -a "$logfile"
echo "$infile,$invres,$infrate,$pctintl,$pcthtc,Bad HDTC" >>"$diagfile"
runmethod="baddetelecine"
else
echo "Diagnosis: Mixed/offset telecine or interlaced." |tee -a "$logfile"
echo "$infile,$invres,$infrate,$pctintl,$pcthtc,Mixed HTC+Intl" >>"$diagfile"
runmethod="hardtelecine"
fi
elif [ "$nomfrate" == "Mix" ]; then
echo "Diagnosis: Mixed progressive, telecined, and/or interlaced video." |tee -a "$logfile"
echo "$infile,$invres,$infrate,$pctintl,$pcthtc,Mixed Methods" >>"$diagfile"
echo "$infile" >>maplist.txt
runmethod="mixed"
else
echo "ERROR: This doesn't appear to be NTSC or PAL SD video. You can try forcing with -p NTSC|PAL flag." |tee -a "$logfile";echo
echo "$infile,$invres,$infrate,$pctintl,$pcthtc,Invalid video type" >>"$diagfile"
exit 1
fi
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function analyzentsc."
}
function analyzepal {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function analyzepal."
local infile="$1"
if [ "$inframes" -le 6 ]; then
echo "Diagnosis: Undiagnosable short packet." |tee -a "$logfile"
echo "$infile,$invres,$infrate,$pctintl,$pcthtc,Short packet" >>"$diagfile"
runmethod="passthrough"
elif [ "$(echo "$pctintl > 10" |bc -l)" -eq 1 ]; then
echo "Diagnosis: PAL interlaced video at 25FPS." |tee -a "$logfile"
echo "$infile,$invres,$infrate,$pctintl,$pcthtc,PAL Interlaced 25FPS" >>"$diagfile"
runmethod="palinterlaced"
else
echo "Diagnosis: PAL progressive video at 25FPS." |tee -a "$logfile"
echo "$infile,$invres,$infrate,$pctintl,$pcthtc,PAL Progressive 25FPS" >>"$diagfile"
runmethod="palprogressive"
fi
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function analyzepal."
}
function mixed {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function mixed."
local infile="$1"
local infile2="$2"
[ $DEBUG = 0 ] || echo "DEBUG: Variable infile is $infile."
[ $DEBUG = 0 ] || echo "DEBUG: Variable infile2 is $infile2."
filmframes=0
vidframes=0
inname="$(echo "$infile" |cut -d. -f1)"
reportname="${inname}-mixed-report.txt"
reportfile="$OUTDIR/$reportname"
if [ "$stage" = 1 ]; then
stage=2
[ $DEBUG = 0 ] || echo "DEBUG: Variable stage is $stage."
makerootfile "$infile"
getmap "$infile"
makesteppedsegs "$infile"
prepsegments "$infile" "$rootfile"
stage=3
runsegments
stage=4
outputsegs
segfail=0
fi
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function mixed."
}
function checkduration {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function checkduration."
incdfile="$1"
outcdfile="$2"
incddurs=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$incdfile")
outcddurs=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$outcdfile")
rtdiff=$(echo "$incddurs" - "$outcddurs" |bc -l)
if [ "$(echo "$rtdiff < 0" |bc -l)" -eq 1 ]; then
absdiff=$(echo "$rtdiff * -1" |bc -l)
else
absdiff="$rtdiff"
fi
pctdiff=$(echo "($absdiff / $incddurs) * 100" |bc -l)
if [ "$rtdiff" == "0" ]; then
echo "Output duration matches input. That's good." |tee -a "$logfile"
tryalt=0
else
echo "WARNING: Output and input durations do not match: out $outcddurs vs in $incddurs" |tee -a "$logfile"
tryalt=1
fi
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function checkduration."
}
function baddtc1 {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function baddtc1."
local infile="$1"
inname="$(echo "$infile" |cut -d. -f1)"
outpart="${inname}-matched-baddtc1-ffv1"
outname="${outpart}.mkv"
outfile="$STAGE3DIR/$outname"
ffmpeg -n \
-hide_banner \
-v warning \
-stats \
-i "$infile" \
-an -sn \
-vf "decimate" \
-r "$infrate" \
-c:v ffv1 "$outfile"
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function baddtc1."
}
function baddtc2 {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function baddtc2."
local infile="$1"
inname="$(echo "$infile" |cut -d. -f1)"
outpart="${inname}-24fps-baddtc2-ffv1"
outname="${outpart}.mkv"
outfile="$STAGE3DIR/$outname"
ffmpeg -n \
-hide_banner \
-v warning \
-stats \
-i "$infile" \
-an -sn \
-vf "decimate" \
-r "$FILMFPS" \
-c:v ffv1 "$outfile"
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function baddtc2."
}
function baddtc3 {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function baddtc3."
local infile="$1"
inname="$(echo "$infile" |cut -d. -f1)"
outpart="${inname}-30fps-baddtc3-ffv1"
outname="${outpart}.mkv"
outfile="$STAGE3DIR/$outname"
ffmpeg -n \
-hide_banner \
-v warning \
-stats \
-i "$infile" \
-an -sn \
-vf "decimate" \
-r "$VIDFPS" \
-c:v ffv1 "$outfile"
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function baddtc3."
}
function baddtc4 {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function baddtc4."
local infile="$1"
inname="$(echo "$infile" |cut -d. -f1)"
outpart="${inname}-48fps-baddtc4-ffv1"
outname="${outpart}.mkv"
outfile="$STAGE3DIR/$outname"
ffmpeg -n \
-hide_banner \
-v warning \
-stats \
-i "$infile" \
-an -sn \
-vf "decimate" \
-r "$FDBLFPS" \
-c:v ffv1 "$outfile"
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function baddtc4."
}
function baddtc5 {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function baddtc5."
local infile="$1"
inname="$(echo "$infile" |cut -d. -f1)"
outpart="${inname}-60fps-baddtc5-ffv1"
outname="${outpart}.mkv"
outfile="$STAGE3DIR/$outname"
ffmpeg -n \
-hide_banner \
-v warning \
-stats \
-i "$infile" \
-an -sn \
-vf "decimate" \
-r "$IVIDFPS" \
-c:v ffv1 "$outfile"
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function baddtc5."
}
function baddtc6 {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function baddtc6."
local infile="$1"
inname="$(echo "$infile" |cut -d. -f1)"
outpart="${inname}-120fps-baddtc6-ffv1"
outname="${outpart}.mkv"
outfile="$STAGE3DIR/$outname"
ffmpeg -n \
-hide_banner \
-v warning \
-stats \
-i "$infile" \
-an -sn \
-vf "decimate" \
-r "$MAXFPS" \
-c:v ffv1 "$outfile"
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function baddtc6."
}
function baddtc7 {
# If all else failed, this is probably just black screen
[ $DEBUG = 0 ] || echo "DEBUG: Entering function baddtc6."
local infile="$1"
inname="$(echo "$infile" |cut -d. -f1)"
outpart="${inname}-passthrough-baddtc7-ffv1"
outname="${outpart}.mkv"
outfile="$STAGE3DIR/$outname"
ffmpeg -n \
-hide_banner \
-v warning \
-stats \
-i "$infile" \
-an -sn \
-c:v ffv1 "$outfile"
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function baddtc7."
}
function baddetelecine {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function baddetelecine."
local infile="$1"
bestdiff=100
tryalt=0
filmframes=$((filmframes + inframes))
if [ "$stage" -eq 1 ]; then
methodlist=(baddtc2 baddtc3 baddtc4 baddtc5 baddtc6)
else
methodlist=(baddtc1 baddtc2 baddtc3 baddtc4 baddtc5 baddtc6 baddtc7)
fi
local submethod
for submethod in "${methodlist[@]}"; do
echo "Trying submethod $submethod..."
"$submethod" "$infile"
checkduration "$infile" "$outfile"
if [ "$(echo "$pctdiff < $bestdiff" |bc -l)" -eq 1 ]; then
bestfile="$outfile"
bestdiff="$pctdiff"
bestcddurs="$outcddurs"
showdiff="$(printf "%.3f\n" "$rtdiff")"
fi
if [ "$stage" = 1 ] && [ "$(echo "$pctdiff <= $ALLOWLOSS" |bc -l)" -eq 1 ]; then
break
fi
if [ "$tryalt" = 1 ] && [ "$submethod" != "baddtc7" ]; then
echo "Submethod $submethod produced a bad duration."
elif [ "$tryalt" = 1 ]; then
echo "No method produced a good duration. Using the best bad result: $bestcddurs vs. $incddurs"
echo -n ",$submethod" >>"$reportfile"
else
echo "Submethod $submethod produced a good duration: $bestcddurs vs. $incddurs"
echo -n ",$submethod" >>"$reportfile"
break
fi
done
if [ "$(echo "$bestdiff > 0" |bc -l)" -eq 1 ]; then
rename="$(echo "$bestfile" |cut -d. -f1)"
refile="${rename}-dt${showdiff}.mkv"
mv "$bestfile" "$refile"
bestfile="$refile"
fi
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function baddetelecine."
}
function hdtc0 {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function hdtc1."
local infile="$1"
inname="$(echo "$infile" |cut -d. -f1)"
outpart="${inname}-matched-hdtc0-ffv1"
outname="${outpart}.mkv"
outfile="$STAGE3DIR/$outname"
ffmpeg -n \
-hide_banner \
-v warning \
-stats \
-i "$infile" \
-an -sn \
-vf "fieldmatch,decimate,bwdif=mode=send_frame:deint=interlaced" \
-r "$infrate" \
-c:v ffv1 "$outfile"
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function hdtc0."
}
function hdtc1 {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function hdtc1."
local infile="$1"
inname="$(echo "$infile" |cut -d. -f1)"
outpart="${inname}-24fps-hdtc1-ffv1"
outname="${outpart}.mkv"
outfile="$STAGE3DIR/$outname"
ffmpeg -n \
-hide_banner \
-v warning \
-stats \
-i "$infile" \
-an -sn \
-vf "fieldmatch,decimate,bwdif=mode=send_frame:deint=interlaced" \
-r "$FILMFPS" \
-c:v ffv1 "$outfile"
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function hdtc1."
}
function hdtc2 {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function hdtc2."
local infile="$1"
inname="$(echo "$infile" |cut -d. -f1)"
outpart="${inname}-30fps-hdtc2-ffv1"
outname="${outpart}.mkv"
outfile="$STAGE3DIR/$outname"
ffmpeg -n \
-hide_banner \
-v warning \
-stats \
-i "$infile" \
-an -sn \
-vf "fieldmatch,decimate,bwdif=mode=send_field:deint=interlaced" \
-r "$VIDFPS" \
-c:v ffv1 "$outfile"
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function hdtc2."
}
function hdtc3 {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function hdtc3."
local infile="$1"
inname="$(echo "$infile" |cut -d. -f1)"
outpart="${inname}-60fps-hdtc3-ffv1"
outname="${outpart}.mkv"
outfile="$STAGE3DIR/$outname"
ffmpeg -n \
-hide_banner \
-v warning \
-stats \
-i "$infile" \
-an -sn \
-vf "fieldmatch,decimate,bwdif=mode=send_field:deint=interlaced" \
-r "$IVIDFPS" \
-c:v ffv1 "$outfile"
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function hdtc3."
}
function hdtc4 {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function hdtc4."
local infile="$1"
inname="$(echo "$infile" |cut -d. -f1)"
outpart="${inname}-120fps-hdtc4-ffv1"
outname="${outpart}.mkv"
outfile="$STAGE3DIR/$outname"
ffmpeg -n \
-hide_banner \
-v warning \
-stats \
-i "$infile" \
-an -sn \
-vf "fieldmatch,decimate,bwdif=mode=send_field:deint=interlaced" \
-r "$MAXFPS" \
-c:v ffv1 "$outfile"
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function hdtc4."
}
function hdtc5 {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function hdtc5."
local infile="$1"
inname="$(echo "$infile" |cut -d. -f1)"
outpart="${inname}-24fps-hdtc5-ffv1"
outname="${outpart}.mkv"
outfile="$STAGE3DIR/$outname"
ffmpeg -n \
-hide_banner \
-v warning \
-stats \
-i "$infile" \
-an -sn \
-vf "fieldmatch=mode=pc,bwdif=mode=send_field:deint=interlaced" \
-r "$FILMFPS" \
-c:v ffv1 "$outfile"
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function hdtc5."
}
function hdtc6 {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function hdtc6."
local infile="$1"
inname="$(echo "$infile" |cut -d. -f1)"
outpart="${inname}-30fps-hdtc6-ffv1"
outname="${outpart}.mkv"
outfile="$STAGE3DIR/$outname"
ffmpeg -n \
-hide_banner \
-v warning \
-stats \
-i "$infile" \
-an -sn \
-vf "fieldmatch=mode=pc,bwdif=mode=send_field:deint=interlaced" \
-r "$VIDFPS" \
-c:v ffv1 "$outfile"
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function hdtc6."
}
function hdtc7 {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function hdtc7."
local infile="$1"
inname="$(echo "$infile" |cut -d. -f1)"
outpart="${inname}-60fps-hdtc6-ffv1"
outname="${outpart}.mkv"
outfile="$STAGE3DIR/$outname"
ffmpeg -n \
-hide_banner \
-v warning \
-stats \
-i "$infile" \
-an -sn \
-vf "fieldmatch=mode=pc,bwdif=mode=send_field:deint=interlaced" \
-r "$IVIDFPS" \
-c:v ffv1 "$outfile"
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function hdtc7."
}
function hdtc8 {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function hdtc8."
local infile="$1"
inname="$(echo "$infile" |cut -d. -f1)"
outpart="${inname}-120fps-hdtc8-ffv1"
outname="${outpart}.mkv"
outfile="$STAGE3DIR/$outname"
ffmpeg -n \
-hide_banner \
-v warning \
-stats \
-i "$infile" \
-an -sn \
-vf "fieldmatch=mode=pc,bwdif=mode=send_field:deint=interlaced" \
-r "$MAXFPS" \
-c:v ffv1 "$outfile"
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function hdtc8."
}
function hdtc9 {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function hdtc9."
local infile="$1"
if [ "$inframes" -le 24 ]; then
# If it is short, pass it through
inname="$(echo "$infile" |cut -d. -f1)"
outpart="${inname}-passthrough-hdtc9-ffv1"
outname="${outpart}.mkv"
outfile="$STAGE3DIR/$outname"
ffmpeg -n \
-hide_banner \
-v warning \
-stats \
-i "$infile" \
-an -sn \
-c:v ffv1 "$outfile"
else
inname="$(echo "$infile" |cut -d. -f1)"
outpart="${inname}-120fps-hdtc9-ffv1"
outname="${outpart}.mkv"
outfile="$STAGE3DIR/$outname"
ffmpeg -n \
-hide_banner \
-v warning \
-stats \
-i "$infile" \
-an -sn \
-vf "fieldmatch=mode=pc,bwdif=mode=send_field:deint=interlaced" \
-r "$MAXFPS" \
-c:v ffv1 "$outfile"
fi
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function hdtc9."
}
function hardtelecine {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function hardtelecine."
local infile="$1"
bestdiff=100
tryalt=0
filmframes=$((filmframes + inframes))
if [ "$stage" -eq 1 ]; then
methodlist=(hdtc1 hdtc2 hdtc3 hdtc4 hdtc5 hdtc6 hdtc7 hdtc8)
else
methodlist=(hdtc0 hdtc1 hdtc2 hdtc3 hdtc4 hdtc5 hdtc6 hdtc7 hdtc8 hdtc9)
fi
local submethod
for submethod in "${methodlist[@]}"; do
echo "Trying submethod $submethod..."
"$submethod" "$infile"
checkduration "$infile" "$outfile"
if [ "$(echo "$pctdiff < $bestdiff" |bc -l)" -eq 1 ]; then
bestfile="$outfile"
bestdiff="$pctdiff"
bestcddurs="$outcddurs"
showdiff="$(printf "%.3f\n" "$rtdiff")"
fi
if [ "$stage" = 1 ] && [ "$(echo "$pctdiff <= $ALLOWLOSS" |bc -l)" -eq 1 ]; then
break
fi
if [ "$tryalt" = 1 ] && [ "$submethod" != "hdtc9" ]; then
echo "Submethod $submethod produced a bad duration."
elif [ "$tryalt" = 1 ]; then
echo "No method produced a good duration. Using the best bad result: $bestcddurs vs. $incddurs"
echo -n ",$submethod" >>"$reportfile"
else
echo "Submethod $submethod produced a good duration: $bestcddurs vs. $incddurs"
echo -n ",$submethod" >>"$reportfile"
break
fi
done
if [ "$(echo "$bestdiff > 0" |bc -l)" -eq 1 ]; then
rename="$(echo "$bestfile" |cut -d. -f1)"
refile="${rename}-dt${showdiff}.mkv"
mv "$bestfile" "$refile"
bestfile="$refile"
fi
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function hardtelecine."
}
function ntsci0 {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function ntsci1."
local infile="$1"
inname="$(echo "$infile" |cut -d. -f1)"
outpart="${inname}-matched-ntsci0-ffv1"
outname="${outpart}.mkv"
outfile="$STAGE3DIR/$outname"
ffmpeg -n \
-hide_banner \
-v warning \
-stats \
-i "$infile" \
-an -sn \
-vf "bwdif=mode=send_field" \
-r "$infrate" \
-c:v ffv1 "$outfile"
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function ntsci0."
}
function ntsci1 {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function ntsci1."
local infile="$1"
inname="$(echo "$infile" |cut -d. -f1)"
outpart="${inname}-30fps-ntsci1-ffv1"
outname="${outpart}.mkv"
outfile="$STAGE3DIR/$outname"
ffmpeg -n \
-hide_banner \
-v warning \
-stats \
-i "$infile" \
-an -sn \
-vf "bwdif=mode=send_frame" \
-r "$VIDFPS" \
-c:v ffv1 "$outfile"
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function ntsci1."
}
function ntsci2 {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function ntsci2."
local infile="$1"
inname="$(echo "$infile" |cut -d. -f1)"
outpart="${inname}-60fps-ntsci2-ffv1"
outname="${outpart}.mkv"
outfile="$STAGE3DIR/$outname"
ffmpeg -n \
-hide_banner \
-v warning \
-stats \
-i "$infile" \
-an -sn \
-vf "bwdif=mode=send_frame" \
-r "$IVIDFPS" \
-c:v ffv1 "$outfile"
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function ntsci2."
}
function ntsci3 {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function ntsci3."
local infile="$1"
inname="$(echo "$infile" |cut -d. -f1)"
outpart="${inname}-120fps-ntsci3-ffv1"
outname="${outpart}.mkv"
outfile="$STAGE3DIR/$outname"
ffmpeg -n \
-hide_banner \
-v warning \
-stats \
-i "$infile" \
-an -sn \
-vf "bwdif=mode=send_frame" \
-r "$MAXFPS" \
-c:v ffv1 "$outfile"
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function ntsci3."
}
function ntsci4 {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function ntsci1."
local infile="$1"
inname="$(echo "$infile" |cut -d. -f1)"
outpart="${inname}-30fps-ntsci4-ffv1"
outname="${outpart}.mkv"
outfile="$STAGE3DIR/$outname"
ffmpeg -n \
-hide_banner \
-v warning \
-stats \
-i "$infile" \
-an -sn \
-vf "bwdif=mode=send_field" \
-r "$VIDFPS" \
-c:v ffv1 "$outfile"
[ $DEBUG = 0 ] || echo "DEBUG: Leaving function ntsci4."
}
function ntsci5 {
[ $DEBUG = 0 ] || echo "DEBUG: Entering function ntsci5."
local infile="$1"
inname="$(echo "$infile" |cut -d. -f1)"
outpart="${inname}-60fps-ntsci5-ffv1"
outname="${outpart}.mkv"
outfile="$STAGE3DIR/$outname"
ffmpeg -n \
-hide_banner \
-v warning \