-
Notifications
You must be signed in to change notification settings - Fork 0
/
handbrakex
executable file
·372 lines (296 loc) · 11.4 KB
/
handbrakex
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
#!/usr/bin/perl
use strict;
use warnings;
# This script does not use Getopt::Long or others because it has special
# processing to swap presets with their definitions.
# Also, Getopt::Long can't deal with optional arguments.
# HandBrakeCLI itself can't deal with combined single-dash arguments.
my $name = 'handbrakex';
my $ver = $name.' 0.3+20151230 by Adam Katz, licensed under the GNU GPL v2+';
my $HANDBRAKE = "HandBrakeCLI";
sub do_help {
print <<end_help;
A wrapper around HandBrakeCLI with advanced preset handling.
Usage: $name [options] -o <file>
-h, --help Display this help
--handbrake <file> The location of the HandBrakeCLI binary
--log <file> An output file to send logs of the encode progress
--no-color Prevent coloring output (default: auto)
--noop Do nothing, just spit out the final command
--option-example A detailed example of presets and option collisions
--preset-names Output all preset names (more concise than --preset-list)
--real-help Get help for any HandBrakeCLI option
--real-version Get the version of HandBrakeCLI
-V, --version Display the version of $name (this wrapper)
(All other options can be referenced via --real-help)
Multiple presets may be used. Repeated options override previous options,
including within --encopts/-x strings. See --option-example for more detail.
$ver
Learn more at https://github.com/adamhotep/media-scripts
end_help
}
sub do_option_example {
print <<end_option_example;
$name can handle multiple presets as well as multiple options.
Multiple instances of --encopts/-x are folded together. For example:
$name -Z nocrop -Z preset1 -Z preset2 --crop 8:8:0:0 -i DVD.iso -o movie.mkv
Suppose that after preset mapping, that becomes:
$name --crop 0:0:0:0 --encoder x265 --encoder-preset slow --encopts \\
psy-rd=0.3:b-adapt=2:rc-lookahead=40:subme=3:deblock=0 \\
--encoder x265 --encoder-preset medium -x psy-rd=0.4:me=3:deblock=1 \\
--crop 8:8:0:0 -i DVD.iso -o movie.mkv
Only the last of each option is used, including --encopts/-x, so this becomes:
$HANDBRAKE --encoder x265 --encoder-preset medium --crop 8:8:0:0 \\
--encopts psy-rd=0.4:b-adapt=2:rc-lookahead=40:subme=3:deblock=1:me=3 \\
-i DVD.iso -o movie.mkv
WARNING: Different encoders have different options, so you may have issues when
combining presets. For example, x265 won't like x264's deblock=0,1
(Note: $HANDBRAKE is sent all redundant command line options since
it will handle them properly; this wrapper only collates the --encopts string.)
end_option_example
}
my $log = "";
sub record {
return 1 unless $log;
my $date = scalar(localtime(time));
my $time = $date;
$time =~ s/.*(\d\d:\d\d:\d\d).*/$1/;
$_ = join(" ", @_);
s/\`date\`/$date/g;
s/\`time\`/$time/g;
chomp;
print LOG "$_\n";
}
# variables in ALL_CAPS are used as globals (subroutines use and modify them)
my $LOCAL_PRESETS = sprintf("%s/.%s.presets", $ENV{HOME}, $name);
my $all_presets = "";
my %presets = ();
my @sorted_presets = ();
my $input = "";
my $output = "";
my $do_nothing = 0;
my $colorize = 0;
my $list_presets = 0;
my $verbose = 0;
########################################
# Gather presets
sub gather_presets {
my $preset_list = "";
if (open(OFFICIAL_PRESETS, "$HANDBRAKE --preset-list 2>/dev/null |")) {
while(<OFFICIAL_PRESETS>) {
chomp;
$preset_list .= $_ . "\n";
}
}
$preset_list .= <<'end_x_presets';
< Extra
+ 30to24: --deinterlace bob --detelecine --vfr
+ animation: --encoder x265 --quality 20.0 --encoder-preset medium --encopts "psy-rd=0.4:deblock=1" --vfr --detelecine --nlmeans light --nlmeans-tune animation
+ h264: --verbose --format mkv --markers --encoder x264 --quality 20.0 --encopts "weightb=0:psy-rd=1|0.2:partitions=all:trellis=2:ref=6:bframes=5:b-adapt=2:direct=auto:subme=10:rc-lookahead=60:me=umh:merange=26:deblock=0,1" --vfr --loose-anamorphic --modulus 8 --aencoder vorbis --aq 4.0 --mixdown 5point1 --normalize-mix 1 --arate auto --drc 0.0 --subtitle 1,2,3,4,5,6,7,8,9,10 --native-language eng
+ h264-sm: --verbose --format mkv --markers --encoder x264 --quality 23.6 --encopts "weightb=0:psy-rd=1|0.2:partitions=all:trellis=2:ref=6:bframes=5:b-adapt=2:direct=auto:subme=10:rc-lookahead=60:me=umh:merange=26:deblock=1,2" --vfr --loose-anamorphic --modulus 16 --denoise weak --aencoder vorbis --aq 0.0 --mixdown dpl2 --normalize-mix 1 --arate auto --drc 0.0 --subtitle 1,2,3,4,5,6,7,8,9,10 --native-language eng
+ hevc: --verbose --format mkv --markers --encoder x265 --quality 19.0 --encoder-preset medium --encopts "psy-rd=0.3:ref=5:bframes=5:b-adapt=2:me=3:rc-lookahead=40:subme=3:deblock=0" --vfr --loose-anamorphic --modulus 8 --aencoder vorbis --aq 4.0 --mixdown 5point1 --normalize-mix 1 --arate auto --drc 0.0 --subtitle 1,2,3,4,5,6,7,8,9,10 --native-language eng
+ hevc-sm: --verbose --format mkv --markers --encoder x265 --quality 22.0 --encoder-preset medium --encopts "psy-rd=0.3:ref=5:bframes=4:b-adapt=2:me=3:rc-lookahead=25:subme=2:deblock=1" --vfr --loose-anamorphic --modulus 16 --aencoder vorbis --aq 0.0 --mixdown dpl2 --normalize-mix 1 --arate auto --drc 0.0 --subtitle 1,2,3,4,5,6,7,8,9,10 --native-language eng
+ nocrop: --crop 0:0:0:0
>
< Custom
end_x_presets
if (-s $LOCAL_PRESETS) {
$preset_list .= qq(# These are taken from "$LOCAL_PRESETS"\n\n);
open(LOCAL_PRESETS, "<", $LOCAL_PRESETS) or die $!;
while(<LOCAL_PRESETS>) {
chomp;
$preset_list .= $_ . "\n";
}
} else {
$preset_list .= qq(# You can add your own presets to "$LOCAL_PRESETS"\n);
}
$preset_list .= "\n>\n";
# Parse presets
for (split("\n", $preset_list)) {
chomp;
next unless /^\s*\+\s+([^:]+):\s*([^#]+)$/;
my ($preset, $def) = ($1, $2);
if ($presets{$preset}) {
print STDERR "WARNING: multiple presets named $preset, using last one.\n";
}
$presets{$preset} = $def;
push(@sorted_presets, $preset);
}
# If direct output & colors are encouraged, color presests
if (-t 1 and ($ENV{PS1} and $ENV{PS1} =~ /\/
or $ENV{TERM} =~ /color/ or $ENV{LS_COLORS} or $ENV{GREP_COLOR}
or $ENV{GREP_OPTIONS} =~ /--colou?r(?!=never)/i)) {
$colorize = 1;
}
return $preset_list;
}
$all_presets = gather_presets;
# Done gathering presets
########################################
########################################
# Parse arguments
# This is done by hand for flexibility, see the comments up top
my @new_args = ();
# return the next element or else an empty string
sub next_elem {
my $i = shift;
$i++;
if ($i < scalar @ARGV) { return $ARGV[$i]; }
else { return ""; }
}
for (my $arg = 0; $arg < scalar @ARGV; $arg++) {
$_ = $ARGV[$arg];
#if (not /^-(?!-?$)/) { last; }
if (/^--real-help/i) { unshift(@new_args, "--help"); }
elsif (/^--real-ver/i) { unshift(@new_args, "--version"); }
elsif (/^(?:-h|--(?i)help)/) { do_help; exit 0; }
elsif (/^--option-ex/i) { do_option_example; exit 0; }
elsif (/^(?:-V|--(?i)ver(?:$|s))/) { print "$ver\n"; exit 0; }
elsif (/^--hand-?brake(?:=|$)(.*)/i) {
$HANDBRAKE = $1;
unless ($HANDBRAKE) {
$HANDBRAKE = next_elem $arg;
$arg++;
}
die qq("$HANDBRAKE" is not executable) unless -x $HANDBRAKE;
$all_presets = gather_presets;
}
elsif (/^(-i|--input=?)(.*)$/) {
($_, $input) = ($1, $2);
if ($input) {
push(@new_args, "--input", qq/"$input"/);
} elsif (next_elem $arg) {
$input = next_elem $arg;
push(@new_args, $_, qq/"$input"/);
$arg++;
}
}
elsif (/^--log[^=]*(?:=|$)(.*)/) {
if ($1 =~ /./) { # allow --log=0
$log = $1;
} elsif (next_elem $arg and next_elem $arg !~ /^-/) {
$log = next_elem $arg;
$arg++;
} else {
$log = "//default log location//";
}
}
elsif (/^--no-?colou?r/i) { $colorize = 0; }
elsif (/^--no-?op/i) { $do_nothing = 1; }
elsif (/^(-o|--output=?)(.*)$/) {
($_, $output) = ($1, $2);
if ($output) {
push(@new_args, "--output", qq/"$output"/);
} elsif (next_elem $arg) {
$output = next_elem $arg;
push(@new_args, $_, qq/"$output"/);
$arg++;
}
}
elsif (/^(-z|--(?i)preset-list)/) { push(@new_args, $1); $list_presets = 1; }
elsif (/^--preset-name/i) { print join("\n", @sorted_presets) . "\n"; exit 0 }
elsif (/^(?:-Z|(?i)--preset(?:=|$))(.*)/) {
my $preset = $1;
unless ($preset) {
$preset = next_elem $arg;
$arg++;
}
unless ($presets{$preset}) {
die qq(Undefined preset "$preset");
}
# Properly traverse quotations and retain spacing
$_ = sprintf(" %s ", $presets{$preset});
while(/\s*([^\s"']*(?:'[^']*'[^\s']*|"[^"]*"[^\s"]*)*)/g) {
push(@new_args, $1);
}
}
elsif (/^(?:-v|--verbos[^=]*=?)(\d*)$/) {
$verbose = $1; # verbose may be 0 (to disable)
if ($verbose !~ /./ and next_elem $arg =~ /^\d+$/) {
$verbose = next_elem $arg;
$arg++;
}
push(@new_args, "--verbose", $verbose) if $verbose;
}
else {
push(@new_args, qq/"$_" /);
}
}
my %encopts = my %opt_num = my @args = ();
for (my $arg = 0; $arg < scalar @new_args; $arg++) {
if ($new_args[$arg] =~ /"?-(?:-encopts|x)\b/) {
$arg++;
$new_args[$arg] =~ s/^"(.*)"\s*$/$1/; # strip quotes
# parse encopts: key1=value1:key2=value2 -> (key1)(value1), (key2)(value2)
while ($new_args[$arg] =~ /([^:=]+)=([^:]+)/g) { # allows '=' in values
# intentionally override previous keys so we can combine multiple presets
$encopts{$1} = $2; # key = value
$opt_num{$1} ||= scalar keys %encopts;
}
} else {
push(@args, $new_args[$arg]);
}
}
if (%encopts) {
my $encopt_string = "";
# sort by the first occurence of each encoder option
for my $key (sort { $opt_num{$a} <=> $opt_num{$b} } keys %opt_num) {
$encopt_string .= ":" if $encopt_string;
$encopt_string .= sprintf("%s=%s", $key, $encopts{$key});
}
push(@args, "--encopts", qq/"$encopt_string"/);
}
if ($input eq "") {
for (qw( /dev/dvd /dev/dvdrw /dev/cdrom /dev/cdrw /dev/dvd0 /dev/dvd1
/dev/cdrom0 /dev/cdrom1 /dev/sr0 /dev/sr1), "") {
if (-r $_) {
$input = $_;
last;
}
}
}
if ($input eq "") {
die "Failed to autodetect input device. Specify it with --input (see --help)";
} elsif (not -r $input) {
die qq/Input device "$input" cannot be read. Is it valid?/;
}
$HANDBRAKE .= qq/ --input "$input"/;
if ($log) {
$HANDBRAKE .= " --verbose";
if ($log eq "//default log location//") {
$log = $output;
$log =~ s/\.[^.]+$//;
my @time = localtime(time);
$log = sprintf("%s %04d-%02d-%02d %02d-%02d-%02d.log", $log,
$time[5] + 1900, $time[4] + 1, $time[3], $time[2], $time[1], $time[0]);
}
print qq/Logging to "$log"\n/;
open(LOG, "> $log") or die $!;
record "[`time`] $name wrapper script initiated at `date`";
record $ver;
}
if ($list_presets) {
if ($colorize) {
$all_presets =~ s/^(\s*\+\s+)([^:]+)(:[^\n#]+)/$1\e[1;32m$2\e[0;0m$3/gm;
$all_presets =~ s/(#[^\n]*)/\e[0;37m$1\e[0;0m/gm;
}
print $all_presets;
exit 0;
}
$HANDBRAKE .= " " . join(" ", @args) . "\n";
record "[`time`] $name will run: $HANDBRAKE";
if ($do_nothing) {
record "[`time`] $name: done (no-op run)";
print $HANDBRAKE;
} else {
open(RUN, "$HANDBRAKE 2>&1 |") or die $!;
while(<RUN>) {
record $_;
if ($colorize) {
s/((?:error|warn|skip)(?:.?(?:ed|ing))?s?)/\e[1;31m$1\e[0;0m/g;
}
print unless $log and not $verbose and not /\[[0-9:]{8}\]/;
}
close RUN;
exit $? >> 8;
}