-
Notifications
You must be signed in to change notification settings - Fork 0
/
recode-vid.sh
executable file
·1803 lines (1752 loc) · 43.6 KB
/
recode-vid.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/sh
usage() {
PROG="$1"
echo "USAGE: $PROG [opts] in out
or: $PROG [opts] -id in [out]
$PROG opts:
-ac X use audio codec X [libvorbis]
-af F append audio filter F
-afpre F prepend audio filter F
-aid X audio stream selector [default|0]
-alang L set audio stream language L
-arate R set audio rate R
-asd S set asyncts min_delta to S [0.3]
-h H scale to height H, for example:
-h 480 means scale to 480 lines exactly
-h 720- to 720 at most (downscale if necessary)
-h 400+ to 400 at least (upscale if necessary)
-hqdn3d L:C:T set hqdn3d parameters [2:1:2]
-id print info on audio/subs IDs
-n don't overwrite output file [-y]
-noass don't use \"ass\" filter for subs
-psar preserve original SAR
-sdir D external subtitles subdirectory
-sid X subtitles stream selector [default|0]
-subcp X assume codepage X for input subtitles [auto]
-subss X force subs style X (e.g. 'fontsize=24')
-tvol T set volume threshold at T dB [-0.1]
-vc X use video codec X [libx264]
-vf F append video filter F
-vfpre F prepend video filter F
-vid N select Nth video stream [0]
-vol A increase volume by A dB
-w W scale to width W (same semantics as -h H)
-x264opts O set x264 options [subme=9:ref=4:bframes=1:
me=umh:partitions=all:no8x8dct:
b-pyramid=strict:bluray-compat]
-y overwrite output file [-y]"
exit 1
}
# check file for utf8 encoding: 0-utf8 1-other
check_fileencoding() {
perl -ws -- - "$1" "$2" <<EOF
use Encode qw(encode decode);
my (\$fn, \$enc, \$thresh) = @ARGV;
die "check_fileencoding: filename is required\\n"
if not defined \$fn;
my \$bom;
\$enc = "utf8" if not defined \$enc;
\$thresh = 0.01 if not defined \$thresh;
open my \$fh, "<", \$fn or die \$!;
my (\$bad, \$total) = (0, 0);
while (<\$fh>) {
if (\$total == 0 and \$enc =~ /\\Autf-?8\\z/i) {
my \$b2 = substr(\$_, 0, 2);
if (\$b2 eq "\\xff\\xfe") {
\$bom = "utf16le BOM \\"fffe\\" detected";
} elsif (\$b2 eq "\\xfe\\xff") {
\$bom = "utf16be BOM \\"feff\\" detected";
};
};
\$total += length(\$_);
decode(\$enc, \$_, sub {\$bad++; return "\\x{FFFD}"});
};
close \$fh;
if (defined \$bom) {
printf "%s: %s\\n", \$fn, \$bom;
exit 1;
};
if (\$bad / \$total < \$thresh) {
if (\$bad) {
printf "%s: %i bad char%s (%.2f%%), assuming %s\\n",
\$fn, \$bad, ((\$bad > 1) ? "s" : ""),
\$bad * 100 / \$total, \$enc;
};
exit 0;
} else {
exit 1;
};
EOF
return $?
}
# Prepend missing [Event] and Format headers in broken ASS streams:
prepend_missing_event_and_format() {
perl -ws -- - "$1" "$2" <<'EOF'
open my $infh, "<", $ARGV[0] or die $!;
my $outfh;
if (not open $outfh, ">", $ARGV[1]) {
my $err = $!;
close $infh;
die $err;
};
my $section = "";
my $sectlines = 0;
my $lastevfmt;
while (<$infh>) {
if (/\A[\r\n]*\z/) {
$section = "";
$sectlines = -1;
} elsif (/\A\s*\[\s*(.*\S)\s*\]\s*\z/) {
$section = lc($1);
$sectlines = -1;
} elsif ($section eq "events" and $sectlines == 0
and /\A\s*format\s*:/i) {
$lastevfmt = $_;
} elsif ($section eq "" and $sectlines == 0
and /\A\s*(comment|dialog(:?ue)?)\s*:/i) {
print {$outfh} "[Events]\n";
$section = "events";
if (defined $lastevfmt) {
print {$outfh} $lastevfmt;
} else {
print {$outfh} "Format: Layer, Start, End, Style, Name, "
."MarginL, MarginR, MarginV, Effect, Text\n";
};
};
$sectlines++;
print {$outfh} $_;
};
close $outfh;
close $infh;
EOF
return $?
}
SCRIPT_BNAME="${0##*/}"
SCRIPT_BNAME_S="`perl -wsle \
'$ARGV[0] =~ s/[^A-Za-z0-9_.-]/_/g; print $ARGV[0]' \
-- "$SCRIPT_BNAME"`"
TMPF="/tmp/${SCRIPT_BNAME_S}.$$"
TMP_OUT="${TMPF}.out"
TMP_PASS="" ; # temporary file for 2- or 3-pass encoding
TMP_SUBS="" ; # temporary file or symlink for subtitles
die() {
rm -rf "${TMPF}".*
E=1
if [ 0 -lt $# ] && expr "z$1" : 'z[0-9][0-9]*$' >/dev/null ; then
E="$1"
shift
fi
if [ 0 -lt $# ] ; then
echo "ERROR:" "$@" 1>&2
fi
exit "$E"
}
eint() {
rm -rf "${TMPF}".*
# revert to default INT handler and resend INT to self:
trap - INT
kill -INT $$
}
trap eint INT
# video/subtitles params:
VID="0:v:0"
OUTVC="" ; # user forced output video codec
HQDN3D="2:1:2" ; # parameters for hqdn3d filter
X264OPTS="subme=9:ref=4:bframes=1:me=umh:partitions=all:no8x8dct"
X264OPTS="$X264OPTS:b-pyramid=strict:bluray-compat"
SID="" ; # subtitles stream id
SIDX="" ; # subtitles stream selector expression
SIDTYPE="" ; # subtitles type: "ass" or "srt"
SUBCP="" ; # subtitles codepage
SDIR="" ; # external subtitles subdirectory
SUBSS="" ; # forced subtitles style
SUBS_FILTER="" ; # "ass" "or subtitles"
VF_SUBS="" ; # "subtitles"/"ass" video filter with params
SCALEW=""
SCALEH=""
NORMALIZE_SAR=1
VF_SCALE=""
VF_OTHER="" ; # other video filters to append
VFPRE_OTHER="" ; # other video filters to prepend
PIXFMT=""
EMBEDDED_FONTS=1 ; # use embedded fonts
# audio params:
OUTAC="" ; # user forced output audio codec
AID="" ; # audio stream id
AIDX="" ; # audio stream selector expression
ARATE=""
ASD="0.3" ; # asyncts min_delta [0.3s]. You need to increase it
# if you get messages like "[Parsed_asyncts_0 @
# 0x1719be0] Non-monotonous timestamps, dropping
# whole buffer." XXX
ALANG="" ; # output audio language tag
ADD_VOL="" ; # additional audio volume
THRESH_VOL="-0.1" ; # volume threshold for autoincrease
AF_VOL="" ; # "volume" audio filter
AF_OTHER="" ; # other audio filters to append
AFPRE_OTHER="" ; # other audio filters to prepend
# other params:
ANALYZEDURATION="-analyzeduration 9223370000000000000"
# analyzeduration: [0 - 9.22337e+18]
PROBESIZE="-probesize 9223370000000000000"
# probesize: [32 - 9.22337e+18]
ID_MODE="" ; # "identify AIDs/SIDs" mode (-id)
OVWR_OUT="-y" ; # "overwrite output file" option
ARG_CNT=0 ; # arguments counter
CUR_OPT="none" ; # current option
# in/out/tail argument groups:
GRPC=0 ; # argument groups counter
ARGC=0 ; # group arguments counter
INC=0 ; # input groups counter
OUTC=0 ; # output groups counter
TGRPN="" ; # tail group number
# video/audio/subs streams:
VIDC=0 ; # video streams counter
AIDC=0 ; # audio streams counter
SIDC=0 ; # subtitles streams counter
FIDC=0 ; # embedded/attached fonts counter
incr() {
if ! expr "z$1" : 'z[A-Za-z_][A-Za-z_0-9]*$' >/dev/null ; then
die "invalid variable name - $1"
fi
eval "$1=\"\`expr 1 + \"\$$1\"\`\""
}
next_grp() {
eval "G${GRPC}TYP=\"\$1\""
eval "G${GRPC}ARGC=\"\$ARGC\""
incr GRPC
ARGC=0
}
add_in_file() {
fname="$1"
bname="${fname##*/}"
dir="${fname%/*}"
if [ "z$dir" = "z$fname" ] ; then
dir="."
fi
dir="$dir/"
if ! [ -e "$fname" ] ; then
die "$fname doesn't exist"
fi
if ! [ -f "$fname" ] && ! [ -l "$fname" ] ; then
die "$fname isn't a file or symlink"
fi
if ! [ -d "$dir" ] ; then
die "$dir isn't a dir"
fi
eval "IN${INC}GRP=\"\$GRPC\""
eval "IN${INC}=\"\$fname\""
eval "IN${INC}BNAME=\"\$bname\""
eval "IN${INC}DIR=\"\$dir\""
next_grp "IN${INC}"
incr INC
TGRPN=""
}
add_out_file() {
case "$1" in
*/)
if [ "z$INC" = "z0" ] ; then
die "at least one input file name required"\
"to generate output file name in $1"
fi
last_in="`expr \( "$INC" \) - 1`"
eval "in_bname=\"\$IN{$last_in}BNAME\""
# XXX: assume matroska (.mkv) format for the output file:
fname="$1${in_bname%.*}.mkv"
;;
*) fname="$1" ;;
esac
dir="${fname%/*}"
if [ "z$dir" = "z$fname" ] ; then
dir="."
fi
dir="$dir/"
if ! [ -d "$dir" ] ; then
die "$dir isn't a dir"
fi
eval "OUT${OUTC}GRP=\"\$GRPC\""
eval "OUT${OUTC}=\"\$fname\""
case "$fname" in
*.[mM][pP]4)
#eval "OUT${OUTC}AC=\"libfaac\""
eval "OUT${OUTC}AC=\"aac\""
eval "OUT${OUTC}VC=\"libx264\""
eval "OUT${OUTC}MUX=\"mp4\""
;;
*.[wW][eE][bB][mM])
eval "OUT${OUTC}AC=\"libvorbis\""
eval "OUT${OUTC}VC=\"vp8\""
eval "OUT${OUTC}MUX=\"webm\""
;;
*.[mM][kK][vV]|*)
eval "OUT${OUTC}AC=\"libvorbis\""
eval "OUT${OUTC}VC=\"libx264\""
eval "OUT${OUTC}MUX=\"matroska\""
;;
esac
if [ "z$OUTAC" != "z" ] ; then
eval "OUT${OUTC}AC=\"\$OUTAC\""
fi
if [ "z$OUTVC" != "z" ] ; then
eval "OUT${OUTC}VC=\"\$OUTVC\""
fi
next_grp "OUT${OUTC}"
incr OUTC
TGRPN=""
}
append_grp2cmd() {
if ! expr "z$1" : 'z[0-9][0-9]*$' >/dev/null ; then
die "invalid group number - $1"
fi
if ! expr "z$2" : 'z[A-Za-z_][A-Za-z_0-9]*$' >/dev/null ; then
die "invalid variable name - $2"
fi
eval "agrpargc=\"\$G${1}ARGC\""
aj=0
while [ "$aj" -lt "$agrpargc" ] ; do
eval "$2=\"\$$2 \\\"\\\$G${1}A$aj\\\"\""
incr aj
done
if [ "z$3" = "zfull" ] ; then
eval "agrptyp=\"\$G${1}TYP\""
case "$agrptyp" in
IN*) eval "$2=\"\$$2 -i \\\"\\\$$agrptyp\\\"\"" ;;
OUT*) eval "$2=\"\$$2 \\\"\\\$$agrptyp\\\"\"" ;;
esac
fi
}
append_ins2cmd() {
if ! expr "z$1" : 'z[A-Za-z_][A-Za-z_0-9]*$' >/dev/null ; then
die "invalid variable name - $1"
fi
ai=0
while [ "$ai" -lt "$INC" ] ; do
eval "$1=\"\$$1 -i \\\"\\\$IN$ai\\\"\""
incr ai
done
}
append_ingrps2cmd() {
if ! expr "z$1" : 'z[A-Za-z_][A-Za-z_0-9]*$' >/dev/null ; then
die "invalid variable name - $1"
fi
ai=0
while [ "$ai" -lt "$INC" ] ; do
eval "ag=\"\$IN${ai}GRP\""
append_grp2cmd "$ag" "$1" "full"
incr ai
done
}
append_tgrp2cmd() {
if ! expr "z$1" : 'z[A-Za-z_][A-Za-z_0-9]*$' >/dev/null ; then
die "invalid variable name - $1"
fi
if [ "z$TGRPN" != "z" ] ; then
append_grp2cmd "$TGRPN" "$1"
fi
}
# parse_stream_id AID 0:1
parse_stream_id() {
if ! expr "z$1" : 'z[A-Za-z_][A-Za-z_0-9]*$' >/dev/null ; then
die "invalid variable name - $1"
fi
fre='\([0-9][0-9]*:\)\{0,1\}' ; # optional file id
hre='\([0-9][0-9]*\|0x[0-9a-fA-F]\{1,\}\)' ; # dec/hex
wre='[A-Za-z_][A-Za-z_0-9]*' ; # word
if [ "z$A" = "znone" ] ; then
# none
eval "$1=\"\$2\""
elif expr "z$A" : 'z[0-9][0-9]*:[0-9][0-9]*$' \
>/dev/null ; then
# 0:0
eval "$1=\"\$2\""
elif expr "z$A" : "z$fre[avs]\\(:[0-9][0-9]*\\)\\{0,1\\}\$" \
>/dev/null ; then
# 1:v:1, a:2, 2:s, a
eval "$1=\"\$2\""
elif expr "z$A" : "z$fre[p]\\(:[0-9][0-9]*\\)\\{1,2\\}\$" \
>/dev/null ; then
# 3:p:1:3, p:2:4, 4:p:3, p:4
eval "$1=\"\$2\""
elif expr "z$A" : "z$fre\\(#\\|i:\\):$hre\$" \
>/dev/null ; then
# #0x1100, i:0x1101, #231, i:232, 5:#0x1102, 6:i:0x1103,
# 7:#234, 8:i:235
eval "$1=\"\$2\""
elif expr "z$A" : "z$fre[m]:$wre\\(:.*\\)\\{0,1\\}\$" \
>/dev/null ; then
# m:LANGUAGE:jpn, 9:m:LANGUAGE:rus, m:ENCODER, 10:m:ENCODER
eval "$1=\"\$2\""
else
# 0, rus, !default, 5:rus!default & so on
eval "${1}X=\"\$2\""
fi
}
bc2() {
bc <<EOF
$1
$2
EOF
}
# closest_std WH SZ CONSTR MAXDIFF R
#
# WH indicates W or H ("w", "width", "h", "height")
# SZ original size (e.g. "701")
# CONSTR std size constraint (e.g. "720-", "700+", "")
# MAXDIFF max diff between orig SZ and std size (e.g. "16")
# R round to R pixels if std size is not found
#
# return closest standard width/height found conforming to the
# given CONSTR & MAXDIFF, or SZ rounded to R
closest_std() {
# Minimum diff so far:
_m=""
# Closest std size so far:
_c=""
while read _f _s; do
# Skip empty/invalid sizes:
case "$_s" in [1-9]*x[1-9]*);; *) continue;; esac
# Strip xH suffix or Wx prefix
case "$1" in
w|W|width) _s="${_s%x*}";;
h|H|height) _s="${_s#*x}";;
esac
# Skip std sizes not fitting into constraint:
case "$3" in
*-) if [ "$_s" -gt "${3%[+-]}" ]; then continue; fi;;
*+) if [ "$_s" -lt "${3%[+-]}" ]; then continue; fi;;
esac
# Get diff:
_d="`expr "$_s" - "$2"`"
# Compare abs(_d) with abs(_m):
if [ "z$_m" = "z" ] || [ "${_d#-}" -lt "${_m#-}" ]; then
_m="$_d"
_c="$_s"
fi
done <<EOF
ntsc 720x480
pal 720x576
qntsc 352x240
qpal 352x288
sntsc 640x480
spal 768x576
film 352x240
ntsc-film 352x240
sqcif 128x96
qcif 176x144
cif 352x288
4cif 704x576
16cif 1408x1152
qqvga 160x120
qvga 320x240
vga 640x480
svga 800x600
xga 1024x768
uxga 1600x1200
qxga 2048x1536
sxga 1280x1024
qsxga 2560x2048
hsxga 5120x4096
wvga 852x480
wxga 1366x768
wsxga 1600x1024
wuxga 1920x1200
woxga 2560x1600
wqsxga 3200x2048
wquxga 3840x2400
whsxga 6400x4096
whuxga 7680x4800
cga 320x200
ega 640x350
hd480 852x480
hd720 1280x720
hd1080 1920x1080
2k 2048x1080
2kflat 1998x1080
2kscope 2048x858
4k 4096x2160
4kflat 3996x2160
4kscope 4096x1716
nhd 640x360
hqvga 240x160
wqvga 400x240
fwqvga 432x240
hvga 480x320
qhd 960x540
2kdci 2048x1080
4kdci 4096x2160
uhd2160 3840x2160
uhd4320 7680x4320
- 720x400
EOF
if [ "z$_m" != "z" ] && [ "${_m#-}" -gt "$4" ] ; then
_c=""
fi
case "z$_c" in z)
# Set _c as SZ rounded to multiple of R:
_c=`bc2 "scale=0" "$2 / $5 * $5"`
# Check rounding against CONSTR and add ±R if needed:
case "$3" in
*-) if [ "$_c" -gt "${3%-}" ]; then
_c=`expr "$_c" - "$5"`
fi;;
*+) if [ "$_c" -lt "${3%+}" ]; then
_c=`expr "$_c" + "$5"`
fi;;
esac
esac
printf %s "$_c"
unset _c _m _s _f
}
parse_args() {
while [ 0 -lt $# ] ; do
A="$1"
shift
case "$CUR_OPT" in
-ac)
OUTAC="$A"
CUR_OPT="none"
;;
-af)
AF_OTHER="$AF_OTHER,$A"
CUR_OPT="none"
;;
-afpre)
AFPRE_OTHER="$AFPRE_OTHER$A,"
CUR_OPT="none"
;;
-aid)
parse_stream_id AID "$A"
CUR_OPT="none"
;;
-alang)
ALANG="$A"
CUR_OPT="none"
;;
-arate)
ARATE="$A:"
CUR_OPT="none"
;;
-asd)
ASD="$A"
CUR_OPT="none"
;;
-h)
if ! expr "z$A" : 'z[0-9][0-9]*[+-]\?$' \
>/dev/null ; then
die "invalid height $A"
fi
SCALEH="$A"
CUR_OPT="none"
;;
-hqdn3d)
if expr "z$A" : \
'z[0-9][0-9]*:[0-9][0-9]*:[0-9][0-9]*$' \
>/dev/null ; then
HQDN3D="$A"
else
die "invalid -hqdn3d $A"
fi
CUR_OPT="none"
;;
-pix_fmt)
PIXFMT="$A"
CUR_OPT="none"
;;
-sid)
parse_stream_id SID "$A"
CUR_OPT="none"
;;
-subcp)
SUBCP="$A"
CUR_OPT="none"
;;
-subsd|-sdir)
SDIR="$A"
CUR_OPT="none"
;;
-subss)
SUBSS="$A"
# XXX: when -subss contains commas or colons,
# it must be enclosed in single quotes:
case "$SUBSS" in
\'*) ;; #XXX'
*[:,]*) SUBSS="'$SUBSS'" ;;
esac
CUR_OPT="none"
;;
-tvol)
THRESH_VOL="$A"
CUR_OPT="none"
;;
-vc)
OUTVC="$A"
CUR_OPT="none"
;;
-vf)
VF_OTHER="$VF_OTHER,$A"
CUR_OPT="none"
;;
-vfpre)
VFPRE_OTHER="$VFPRE_OTHER$A,"
CUR_OPT="none"
;;
-vid)
case "$A" in
*:v:*|*:\#*:*|none) VID="$A" ;;
*:*:*) die "invalid -vid $A" ;;
\#*:*) VID="0:$A" ;;
*) VID="0:v:$A" ;;
esac
CUR_OPT="none"
;;
-vol)
ADD_VOL="$A"
CUR_OPT="none"
;;
-w)
if ! expr "z$A" : 'z[0-9][0-9]*[+-]\?$' \
>/dev/null ; then
die "invalid width $A"
fi
SCALEW="$A"
CUR_OPT="none"
;;
-x264opts)
X264OPTS="$A"
CUR_OPT="none"
;;
-*)
case "$CUR_OPT" in
-b:v)
# use 2pass encoding when desired
# bitrate is known
TMP_PASS="${TMPF}.pass"
;;
-crf)
# don't use 2pass encoding with -crf
TMP_PASS=""
;;
esac
case "$CUR_OPT" in
-i) add_in_file "$A" ;;
*) eval "G${GRPC}A${ARGC}=\"\$CUR_OPT\""
incr ARGC
eval "G${GRPC}A${ARGC}=\"\$A\""
incr ARGC
;;
esac
CUR_OPT="none"
;;
none)
case "$A" in
-id)
ID_MODE="1"
;;
-n) OVWR_OUT="-n" ;;
-noass)
SUBS_FILTER="subtitles"
;;
-psar)
NORMALIZE_SAR=0
;;
-y) OVWR_OUT="-y" ;;
-fix_sub_duration)
eval "G${GRPC}A${ARGC}=\"\$A\""
incr ARGC
;;
-*)
CUR_OPT="$A"
;;
*)
CUR_OPT="none"
case $ARG_CNT in
0)
add_in_file "$A"
ARG_CNT="1"
;;
1)
add_out_file "$A"
ARG_CNT="2"
;;
*)
usage "${0##*/}"
;;
esac
;;
esac
;;
esac
done
if [ "z$ARGC" != "z0" ] ; then
TGRPN="$GRPC"
next_grp "tail"
fi
if [ "z$ID_MODE" = "z1" ] ; then
if [ "z$IN0" = "" ] ; then
usage "${0##*/}"
fi
else
if [ "z$ARG_CNT" != "z2" ] ; then
usage "${0##*/}"
fi
fi
}
# Read data with leading whitespace:
readw() {
readw_ifs0="$IFS"
IFS="
"
read "$@"
readw_ret="$?"
IFS="$readw_ifs0"
return "$readw_ret"
}
# Match stream vs selector:
match() {
match="$2"
matchtype=':'
while [ "z$match" != "z" ] ; do
matchtail="${match#*[:!]}"
matchhead="${match%%[:!]*}"
if [ "z$matchhead" != "z" ] ; then
if expr "z$matchhead" : 'z[0-9][0-9]*$' >/dev/null ; then
matchno="$matchhead"
else
case "$1" in
*"$matchhead"*) matchres=':';;
*) matchres='!';;
esac
if [ "z$matchres" != "z$matchtype" ] ; then
return 1
fi
fi
fi
# set type of match for the next iteration:
if [ "z$matchhead" = "z${match%%!*}" ] ; then
matchtype='!'
else
matchtype=':'
fi
if [ "z$matchtail" = "z$match" ] ; then
match=""
else
match="$matchtail"
fi
done
return 0
}
# Run ffmpeg commandline (first argument) with stderr/stdout redirected
# to the filename passed as 2nd argument. If ffmpeg fails, send its
# logged output to stderr and die().
run_ffmpeg() {
case "$3" in
[012]|"") ;;
[34]|*) echo "$1";;
esac
case "$3" in
[01]) ;;
""|[234]|*) eval "echo $1";;
esac
if [ "z$2" != "z" ] ; then
case "$3" in
[0123]|"") eval "$1 </dev/null >\"\$2\" 2>&1";;
[4]|*) eval "$1 </dev/null 2>&1 | tee \"\$2\"";;
esac
else
case "$3" in
[0123]|"") eval "$1 </dev/null >/dev/null 2>&1";;
[4]|*) eval "$1 </dev/null";;
esac
fi
E="$?"
if [ "z$E" != "z0" ] ; then
case "$3" in
0)
# XXX: ignore ffmpeg errors when verbosity is 0
;;
[123]|"")
if [ "z$2" != "z" ] ; then
cat "$2" 1>&2
fi
die "$E"
;;
[4]|*) die "$E";;
esac
fi
}
ifs0="$IFS"
IFS=":"
for f in bc cat cp expr ffmpeg find grep iconv mkdir mv perl pwd rm \
sed sort tee ; do
x=0
for p in $PATH ; do
if [ -x "$p/$f" ] ; then
x=1
fi
done
if [ "z$x" = "z0" ] ; then
die "'$f' not found"
fi
done
IFS="$ifs0"
parse_args "$@"
# Detect internal & external stream IDs:
if [ "z$AID" != "znone" ] || [ "z$SID" != "znone" ] \
|| [ "z$VID" != "znone" ] || [ "z$ID_MODE" = "z1" ] \
|| [ "z$EMBEDDED_FONTS" = "z1" ]; then
# Detect internal video/audio/subtitles stream IDs:
ffmpeg="ffmpeg -hide_banner $ANALYZEDURATION $PROBESIZE"
append_ins2cmd ffmpeg
# This will fail with "At least one output file must be specified",
# but produce nice listing of all input streams:
run_ffmpeg "$ffmpeg" "$TMP_OUT" 0
state=""
while readw L ; do
# Stream #0:1(rus): Audio: aac (HE-AAC), 44100 Hz, 5.1,
# fltp (default)
#
# Stream #0:1[0x1100]: Audio: pcm_bluray (HDMV / 0x564D4448),
# 48000 Hz, stereo, s16, 1536 kb/s
#
# Stream #0:1: Audio: mp3 (U[0][0][0] / 0x0055), 48000 Hz,
# stereo, s16p, 160 kb/s
# Metadata:
# title : Suzaku
case "z$L" in
"z Stream #"*": Video: "*);&
"z Stream #"*": Video: "*)
state="Video"
;;
"z Stream #"*": Audio: "*);&
"z Stream #"*": Audio: "*)
state="Audio"
;;
"z Stream #"*": Subtitle: "*);&
"z Stream #"*": Subtitle: "*)
state="Subtitle"
;;
"z Stream #"*": Attachment: "*);&
"z Stream #"*": Attachment: "*)
state="Attachment"
;;
"z Stream #"*": Data: "*);&
"z Stream #"*": Data: "*)
state="Data"
;;
"z Metadata:");&
"z Metadata:")
if ! expr "z$state" : 'z[AVSF]ID[0-9][0-9]*DESC$' \
>/dev/null ; then
state="meta"
unset desc
unset id
fi
;;
"z "????????????????": "*)
if expr "z$state" : 'z[AVSF]ID[0-9][0-9]*DESC$' \
>/dev/null ; then
key="${L%%:*}"
val="${L#*: }"
i=0
while [ "$i" -lt 22 ] ; do
key="${key% }"
key="${key# }"
incr i
done
# E.g. if state=VID0DESC, key:val will be appended
# to VID0DESC:
eval "$state=\"\$$state, \$key:\$val\""
case "$key" in
filename)
eval "${state%DESC}FNAME=\"\$val\""
;;
esac
unset key
unset val
fi
;;
*) state=""
unset desc
unset id
;;
esac
case "$state" in
Video|Audio|Subtitle|Attachment)
case "z$L" in
"z S"*)desc="${L# Stream #}";;
"z S"*) desc="${L# Stream #}";;
esac
# idx is extended id, with (lang) and [hex] suffixes
# if present:
idx="${desc%%: $state: *}"
desc="${desc#*: $state: }"
# id is plain input_no:stream_no id:
id="${idx%%(*}"
id="${id%%\[*}"
if ! expr "z$id" : 'z[0-9][0-9]*:[0-9][0-9]*$' \
>/dev/null ; then
die "invalid input stream <$id> in '$L'"
fi
# input no:
input_no="${id%:*}"
# stream no:
stream_no="${id#*:}"
eval "bname=\"\$IN${input_no}BNAME\""
;;
esac
case "$state" in
Video)
eval "I${input_no}N${stream_no}=\"VID$VIDC\""
eval "VID$VIDC=\"\$id\""
eval "VID${VIDC}DESC=\"\$bname, int.stream#\$idx," \
"\$desc\""
unset idx
case "$desc" in *" (default)") VIDDEF="$VIDC" ;; esac
state="VID${VIDC}DESC"
incr VIDC
;;
Audio)
eval "I${input_no}N${stream_no}=\"AID$AIDC\""
eval "AID$AIDC=\"\$id\""
eval "AID${AIDC}DESC=\"\$bname, int.stream#\$idx," \
"\$desc\""
unset idx
case "$desc" in *" (default)") AIDDEF="$AIDC" ;; esac
state="AID${AIDC}DESC"
incr AIDC
;;
Subtitle)
eval "I${input_no}N${stream_no}=\"SID$SIDC\""
eval "SID$SIDC=\"\$id\""
case "$desc" in
ass[,\ ]*|ass)
eval "SID${SIDC}TYPE=\"ass\"";;
subrip[,\ ]*|subrip)
desc="srt${desc#subrip}"
eval "SID${SIDC}TYPE=\"srt\"";;
esac
case "$desc" in *" (default)") SIDDEF="$SIDC" ;; esac
eval "SID${SIDC}DESC=\"\$bname, int.stream#\$idx," \
"\$desc\""
unset idx
state="SID${SIDC}DESC"
incr SIDC
;;
Attachment)
case "$desc" in
[ot]tf)
eval "I${input_no}N${stream_no}=\"FID$FIDC\""
eval "FID$FIDC=\"\$id\""
eval "FID${FIDC}TYPE=\"\$desc\""
eval "FID${FIDC}DESC=\"\$bname," \
"int.stream#\$idx, \$desc\""
state="FID${FIDC}DESC"
# Append $FIDC to list of fids of the
# corresponding INPUT file:
eval "fids=\"\$IN${input_no}FIDS\""
if [ "z$fids" != "z" ] ; then
fids="$fids "
fi
fids="${fids}$FIDC"
eval "IN${input_no}FIDS=\"\$fids\""
unset fids
incr FIDC
;;
esac
unset idx
;;
esac
done <"$TMP_OUT"
# Find external audio/subtitles streams:
i=0
inxc="$INC" ; # input + ext files counter
while [ "$i" -lt "$INC" ] ; do
eval "ibname=\"\$IN${i}BNAME\""
eval "idir=\"\$IN${i}DIR\""
if [ "z$ADIR" != "z" ] ; then
# XXX: run "find" for audio files only once for ADIR:
if [ "z$i" = "z0" ] ; then
adir="$ADIR"
else
adir=""