-
Notifications
You must be signed in to change notification settings - Fork 0
/
2ch-webm-resizer.pl
709 lines (543 loc) · 23.3 KB
/
2ch-webm-resizer.pl
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
#!/usr/bin/perl -w
use strict;
use warnings;
use POSIX;
use IPC::Open3;
use File::stat;
use Getopt::Long qw(:config pass_through);
use Pod::Usage;
use File::Basename;
use DirHandle;
$SIG{INT} = sub { die "Caught a sigint $!" };
$SIG{TERM} = sub { die "Caught a sigterm $!" };
my $man = 0;
my $help = 0;
my $additional_filename_num;
my $stop_processing;
my $frames_to_encode;
my $frames_to_skip;
my $seconds_to_skip;
my $finalsize;
my $target_size;
my $target_frame_rate;
my $no_third_step;
my $no_resize = 0;
my $filename;
my $filename_without_extension;
my $path;
my $extension;
my $encoded_video_size;
my $time_start;
my $time_finish;
my $cpu_used;
my $deadline;
our $ffmpeg_debug_params;
# Function definition
my $max_temp_frames;
my $temp_size;
my $accepted_percent_change;
my $temp_slices;
my $concat_string = '';
my $force_width;
my $no_audio;
my $audiotrack;
my $opus_cut;
my $total_slices;
my $sem;
my @thr;
our %filenames;
our %sizes;
our %timing;
our %encoding_params;
my $ffmpeg_params;
our $ffmpeg_binary = "ffmpeg";
our $ffprobe_binary = "ffprobe";
our $mkvmerge_binary = "mkvmerge";
our %videoInfo;
$time_start = time;
GetOptions( 'help|?' => \$help, man => \$man ) or pod2usage(2);
pod2usage(1) if $help;
pod2usage( -exitval => 0, -verbose => 2 ) if $man;
pod2usage("$0: No files given.") if ( ( @ARGV == 0 ) && ( -t STDIN ) );
GetOptions( "ss=s" => \$timing{"start_from"},
"to=s" => \$timing{"end_to"},
"target=n" => \$sizes{"target_video_size"},
"max_temp_frames=n" => \$max_temp_frames,
"temp_slices=n" => \$temp_slices,
"temp_size=n" => \$temp_size,
"accepted_percent_change=n" => \$accepted_percent_change,
"opus=n" => \$encoding_params{"opus_bitrate"},
"no_resize" => \$no_resize,
"threads=n" => \$encoding_params{"threads"},
"qmin=n" => \$encoding_params{"qmin"},
"crf=n" => \$encoding_params{"crf"},
"qmax=n" => \$encoding_params{"qmax"},
"aq_mode=n" => \$encoding_params{"aq_mode"},
"width=n" => \$force_width,
"cpu_used=n" => \$encoding_params{"cpu_used"},
"deadline=s" => \$deadline,
"ffmpeg_params=s" => \$ffmpeg_params,
"quality=s" => \$encoding_params{"quality"},
"no_audio" => \$no_audio,
"audio=s" => \$audiotrack,
"verbose" => \$encoding_params{"verbose"},
);
$force_width //= -1;
print $time_start;
$opus_cut //= '';
#params//="-static-thresh 0 -arnr-maxframes 7 -arnr-strength 5";
$ffmpeg_params //= "";
print "\n-------------------------------START---------------------------------------\n";
print "\nStarted: " . strftime( "%Y-%m-%d %H:%M:%S", localtime($time_start) ) . "\n\n";
$no_resize //= 1;
$encoding_params{"threads"} //= 4;
$temp_slices //= 4;
$max_temp_frames //= 800;
$temp_size //= 25;
$encoding_params{"opus_bitrate"} //= 70;
$encoding_params{"qmin"} //= 8;
$encoding_params{"qmax"} //= 60;
$encoding_params{"crf"} //= 15;
$encoding_params{"cpu_used"} //= -1;
$no_audio //= 0;
$deadline //= "1000000";
$no_resize = 1;
$sizes{"target_video_size"} //= 6144;
$accepted_percent_change //= 13;
$encoding_params{"verbose"} //= '';
$encoding_params{"quality"} //= "good";
$encoding_params{"aq_mode"} //= 2;
$ffmpeg_debug_params //= '-loglevel quiet';
if ( $encoding_params{"verbose"} ne '' )
{
$ffmpeg_debug_params = '';
}
if ( $no_audio > 0 )
{
$encoding_params{"opus_bitrate"} = 0;
}
if ( $force_width > 0 )
{
$no_resize = 1;
}
if ( $encoding_params{"threads"} > $temp_slices )
{
print "\nConsider setting -temp_slices=" . $encoding_params{"threads"} . " for optimal multithreading performance\n";
}
pod2usage("\n$0: Invalid Arguments.\n") unless ( @ARGV == 1 );
$filename = $ARGV[0];
unless ( -e $filename ) {
die("File Doesn't Exist!\n");
}
$filenames{"filename"} = $filename;
$filenames{"filename"} =~ s/[^A-Za-z0-9\/\-_\.]//g;
if ( $filenames{"filename"} ne $filename )
{
die("Please remove any special chars from the filename for your own good");
}
( $filenames{"filename_without_extension"}, $filenames{"path"}, $filenames{"extension"} ) = fileparse( $filename, qr/\.[^.]*/ );
if ( $filenames{"path"} eq '' )
{
$filenames{"path"} = '.';
}
$filenames{"audiotrack_filename"} //= $filenames{"filename"};
my $audiotrack_fileclean = $filenames{"audiotrack_filename"};
$audiotrack_fileclean =~ s/[^A-Za-z0-9\/\-_\.]//g;
if ( $filenames{"audiotrack_filename"} ne $audiotrack_fileclean )
{
die("Please remove any special chars from the audio track filename for your own good");
}
DeleteTempFiles();
%videoInfo = GetVideoInfo( $filenames{"filename"} );
my $pre_init_cut = '';
my ( $hours, $minutes, $seconds );
if ( $timing{"start_from"} )
{
if ( $timing{"start_from"} !~ /^(([0-9]?[0-9]):([[0-5]?[0-9]):([0-5]?[0-9])(.([0-9]([0-9]?[0-9]?)?))?)$/ ) { pod2usage( "$0: Wrong time format for -ss argument (" . $timing{"start_from"} . ").\n" ) }
( $hours, $minutes, $seconds ) = split( /[:]/, $timing{"start_from"} );
if ( ( ( $hours * 3600 ) + ( $minutes * 60 ) + $seconds ) > $videoInfo{'durationsecs'} ) { die( "Start time -ss " . $timing{"start_from"} . " is invalid, source video size is just $videoInfo{'duration'}\n" ); }
$timing{"start_from"} = ( ( $hours * 3600 ) + ( $minutes * 60 ) + $seconds );
$pre_init_cut = 'true';
}
$hours = 0;
$minutes = 0;
$seconds = 0;
if ( $timing{"end_to"} )
{
if ( $timing{"end_to"} !~ /^(([0-9]?[0-9]):([[0-5]?[0-9]):([0-5]?[0-9])(.([0-9]([0-9]?[0-9]?)?))?)$/ ) { pod2usage( "$0: Wrong time format for -to argument (" . $timing{"end_to"} . ").\n" ) }
( $hours, $minutes, $seconds ) = split( /[:]/, $timing{"end_to"} );
$timing{"end_to"} = ( ( $hours * 3600 ) + ( $minutes * 60 ) + $seconds );
$pre_init_cut = 'true';
}
if ( !( $pre_init_cut eq '' ) )
{
PrecutVideo();
%videoInfo = GetVideoInfo( $filenames{"filename"} );
}
CalculateSizes( $main::filenames{"filename"} );
$additional_filename_num = "-bitrate". $main::sizes{"target_bitrate"} . "-opus" . $encoding_params{"opus_bitrate"} . "-" . strftime( "%Y%m%d-%H%M%S", localtime );
SliceVideoByThreads();
EncodeVideo();
MergeSlices();
EncodeAudio();
# returns media information in a hash
system("stty sane");
$time_finish = time;
print "\n\nFinished: " . strftime( "%Y-%m-%d %H:%M:%S", localtime($time_finish) ) . "\n";
print "-------------------------------FINISHED---------------------------------------\n";
print "Total encoding time: " . ( $time_finish - $time_start ) . " seconds\n";
DeleteTempFiles();
sub percent_change {
my ( $from, $to ) = @_;
return unless $from;
return if int($from) == 0;
if ( $from == 0 && $to == 0 ) {
return 0;
}
my $diff = ( ( $to - $from ) / abs($from) ) * 100;
return $diff;
}
sub GetVideoInfo {
my %finfo = (
'duration' => "00:00:00.00",
'durationsecs' => "0",
'bitrate' => "0",
'vcodec' => "",
'vformat' => "",
'acodec' => "",
'asamplerate' => "0",
'achannels' => "0",
);
my $file = shift;
# escaping characters
$file =~ s/(\W)/\\$1/g;
open3( "</dev/null", \*ERPH, \*ERPH, $main::ffprobe_binary . " -select_streams v -show_format -show_streams -hide_banner $file" ) or die "can't run $main::ffprobe_binary\n";
my @res = <ERPH>;
# parse ffmpeg output
foreach (@res) {
if (m!Duration: ([0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9])!) {
$finfo{'duration'} = $1;
}
if (m!width=(\d*)!) {
$finfo{'width'} = $1;
}
if (m!height=(\d*)!) {
$finfo{'height'} = $1;
}
if (m!r_frame_rate=(\d*/\d*)!) {
my $framerate_float = eval $1;
$finfo{'frame_rate'} = sprintf( "%.2f", $framerate_float );
}
if (m!size=(\d*)!) {
$finfo{'size'} = $1;
}
# duration
if (m!Duration: ([0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9])!) {
$finfo{'duration'} = $1;
}
# bitrate
if (m!bitrate: (\d*) kb/s!) {
$finfo{'bitrate'} = $1;
}
# vcodec and vformat
if (/Video: (\w*), (\w*),/) {
$finfo{'vcodec'} = $1;
$finfo{'vformat'} = $2;
}
if (m!nb_frames=(\d*)!) {
$finfo{'nb_frames'} = $1;
}
# Stream #0.1(und): Audio: aac, 48000 Hz, 1 channels, s16, 64 kb/s
# acodec, samplerate, stereo and audiorate
if (m!Audio: (\w*), (\d*) Hz, (\d*)!) {
$finfo{'acodec'} = $1;
$finfo{'asamplerate'} = $2;
$finfo{'achannels'} = $3;
}
}
my $tenths = substr( $finfo{'duration'}, 9, 2 );
my $seconds = substr( $finfo{'duration'}, 6, 2 );
my $minutes = substr( $finfo{'duration'}, 3, 2 );
my $hours = substr( $finfo{'duration'}, 0, 2 );
$finfo{'durationsecs'} = ( $tenths * .01 ) + $seconds + ( $minutes * 60 ) + ( $hours * 360 );
if ( $finfo{'nb_frames'} eq "N/A" )
{
$finfo{'nb_frames'} = $finfo{'durationsecs'} * $finfo{'frame_rate'};
}
if ( $finfo{'nb_frames'} eq "" )
{
$finfo{'nb_frames'} = $finfo{'durationsecs'} * $finfo{'frame_rate'};
}
return %finfo;
}
sub PrecutVideo {
my $keyframe_data;
my $end_keyframe_time;
my $start_keyframe_time;
print "Cutting work area from video " . $main::filenames{"filename"} . " by keyframes with parameters " . $main::timing{"start_from"} . " " . $main::timing{"end_to"} . "\n\n";
$start_keyframe_time = `ffprobe -show_frames -select_streams v:0 -print_format csv $main::filenames{"filename"} | grep -B 1 'frame,video,1' | grep 'frame,video,0' | awk -F ',' '\$5 < $main::timing{start_from}' | tail -n 1 | awk -F ',' '{printf \$5}'`;
$end_keyframe_time = `ffprobe -show_frames -select_streams v:0 -print_format csv $main::filenames{"filename"} | grep -B 1 'frame,video,1' | grep 'frame,video,0' | awk -F ',' '\$5 > $main::timing{end_to}' | head -n 1 | awk -F ',' '{printf \$5}'`;
system( $main::ffmpeg_binary,
"-stats",
"-i", $main::filenames{"filename"},
"-ss", $start_keyframe_time,
"-t", $end_keyframe_time,
"-vcodec", "copy",
"-hide_banner",
"-y",
"-acodec", "copy",
"-async", "1",
$main::filenames{"filename_without_extension"} . "-temp-work" . $main::filenames{"extension"},
);
$main::filenames{"filename"} = $main::filenames{"filename_without_extension"} . "-temp-work" . $main::filenames{"extension"};
print "\nDone\nWorking with " . $main::filenames{"filename"} . "\n";
}
sub CalculateSizes
{
my $videofilename = shift(@_);
print $videofilename;
%main::videoInfo = GetVideoInfo($videofilename);
$main::sizes{"target_bitrate"} = floor(( ( $main::sizes{"target_video_size"} - ( ( $main::videoInfo{"durationsecs"} * $main::encoding_params{"opus_bitrate"} ) / 8 ) ) * 8 ) / $main::videoInfo{"durationsecs"});
print "Video Data:\n\n";
print "duration: " . $main::videoInfo{'duration'} . "\n";
print "durationsecs: " . $main::videoInfo{'durationsecs'} . "\n";
print "bitrate: " . $main::videoInfo{'bitrate'} . "\n";
print "vcodec: " . $main::videoInfo{'vcodec'} . "\n";
print "vformat: " . $main::videoInfo{'vformat'} . "\n";
print "acodec: " . $main::videoInfo{'acodec'} . "\n";
print "asamplerate: " . $main::videoInfo{'asamplerate'} . "\n";
print "achannels: " . $main::videoInfo{'achannels'} . "\n";
print "width: " . $main::videoInfo{'width'} . "\n";
print "height: " . $main::videoInfo{'height'} . "\n";
print "nb_frames: " . $main::videoInfo{'nb_frames'} . "\n";
print "size: " . $main::videoInfo{'size'} . "\n";
print "frame_rate: " . $main::videoInfo{'frame_rate'} . "\n\n";
print "\n\n---\nTarget Video Size with " . $main::encoding_params{"opus_bitrate"} . " Opus bitrate: " . ( $main::sizes{"target_video_size"} - ( ( $main::videoInfo{"durationsecs"} * $main::encoding_params{"opus_bitrate"} ) / 8 ) ) . "\n";
print "Target Video Bitrate with " . $main::encoding_params{"opus_bitrate"} . " Opus bitrate: " . $main::sizes{"target_bitrate"} . "\n---\n\n";
}
sub SliceVideoByKeyFrames {
print "\n\n---\nCreating slices from video $filenames{'filename'}\n--\n\n";
system( $main::ffmpeg_binary,
"-i", $main::filenames{"filename"},
"-acodec", "copy",
"-f", "segment",
"-vcodec", "copy",
"-reset_timestamps", "1",
"-map", "0",
"-async", "1",
$main::filenames{"filename_without_extension"} . "-temp-source-slice-%03d" . $main::filenames{"extension"} );
my $file;
my $dh = DirHandle->new( $main::filenames{"path"} ) or die "Can't open " . $main::filenames{"path"} . " : $!\n";
my @slices = grep( /$filenames{"filename_without_extension"}-temp-source-slice-[0-9]*$main::filenames{"extension"}$/, $dh->read() );
$main::filenames{"total_slices"} = scalar @slices;
$main::filenames{"slices"} = @slices;
}
sub SliceVideoByThreads
{
$main::sizes{"slice_duration_sec"} = ceil( $main::videoInfo{durationsecs} / $main::encoding_params{"threads"} );
print "ffprobe -show_frames -select_streams v:0 -print_format csv $main::filenames{filename} 2>/dev/null | awk -F, '\$4==1 && \$6 - previous >= $main::sizes{slice_duration_sec} {printf(\"%d,\", NR); previous=\$6}' ";
my $threaded_key_frames = `ffprobe -show_frames -select_streams v:0 -print_format csv $main::filenames{filename} 2>/dev/null |awk -F, '\$4==1 && \$6 - previous >= $main::sizes{slice_duration_sec} {printf("%d,", NR); previous=\$6}'`;
my @keyframes = split( ',', $threaded_key_frames );
$threaded_key_frames = join( ',', @keyframes );
print "\n\n$threaded_key_frames";
system( $main::ffmpeg_binary,
'-i',
$main::filenames{'filename'},
'-vcodec',
'copy',
'-map', '0',
'-an',
'-y',
'-f', 'segment',
'-segment_frames', $threaded_key_frames,
$main::filenames{'filename_without_extension'} . "-temp-work-slice%03d" . $main::filenames{'extension'},
);
my $dh = DirHandle->new( $main::filenames{"path"} ) or die "Can't open " . $main::filenames{"path"} . " : $!\n";
my @work_slices = grep( /$main::filenames{filename_without_extension}-temp-work-slice[0-9]*$main::filenames{extension}$/, $dh->read() );
$main::filenames{"work_slices"} = @work_slices;
$main::sizes{"total_work_slices"} = scalar @work_slices;
}
sub EncodeVideo
{
my $padded;
print "\n\n\n\n------------------------FFMPEG PARAMS-----------------------------------\n";
print "\nStarted: " . strftime( "%Y-%m-%d %H:%M:%S", localtime($time_start) ) . "\n\n";
print("Encoding with the following params:\n");
print join( " ", (
'-stats',
'-hide_banner',
# '-cpu-used', $main::encoding_params{"cpu_used"},
'-aq-mode', $main::encoding_params{"aq_mode"},
'-c:v', 'libvpx-vp9',
'-y',
'-an',
'-b:v', $main::sizes{"target_bitrate"} . "k",
'-strict', '-2',
'-quality', $main::encoding_params{"quality"},
'-qmin', $main::encoding_params{'qmin'},
'-crf', $main::encoding_params{'crf'},
'-qmax', $main::encoding_params{'qmax'},
'-vf', 'scale=' . $force_width . ':-1',
'-sws_flags', 'spline',
'-lag-in-frames', '16',
'-pass', '1',
'-f', 'webm',
) );
print "\n-----------------------------------------------------------------------\n\n\n\n";
for ( my $i = 0 ; $i < $main::sizes{"total_work_slices"} ; $i++ ) {
my $pid = fork();
if ( $pid == 0 ) {
$padded = sprintf "%03d", $i;
exec( $main::ffmpeg_binary,
'-stats',
'-i', $main::filenames{'filename_without_extension'} . "-temp-work-slice" . $padded . $main::filenames{'extension'},
'-hide_banner',
# '-cpu-used', $main::encoding_params{"cpu_used"},
'-passlogfile', $main::filenames{filename_without_extension} . '-temp-work-passlog' . $padded,
'-aq-mode', $main::encoding_params{"aq_mode"},
'-c:v', 'libvpx-vp9',
'-y',
'-an',
'-b:v', $main::sizes{"target_bitrate"} . "k",
'-strict', '-2',
'-quality', $main::encoding_params{"quality"},
'-qmin', $main::encoding_params{'qmin'},
'-crf', $main::encoding_params{'crf'},
'-qmax', $main::encoding_params{'qmax'},
'-vf', 'scale=' . $force_width . ':-1',
'-sws_flags', 'spline',
'-lag-in-frames', '16',
'-pass', '1',
'-f', 'webm',
'/dev/null' );
}
elsif ( !defined $pid ) {
warn "Fork $i failed: $!\n";
}
}
1 while wait() >= 0;
for ( my $i = 0 ; $i <= $main::sizes{"total_work_slices"} ; $i++ ) {
my $pid = fork();
if ( $pid == 0 ) {
$padded = sprintf "%03d", $i;
exec( $main::ffmpeg_binary,
'-stats',
'-i', $main::filenames{'filename_without_extension'} . "-temp-work-slice" . $padded . $main::filenames{'extension'},
'-hide_banner',
# '-cpu-used', $main::encoding_params{"cpu_used"},
'-passlogfile', $main::filenames{filename_without_extension} . '-temp-work-passlog' . $padded,
'-aq-mode', $main::encoding_params{"aq_mode"},
'-c:v', 'libvpx-vp9',
'-y',
'-an',
'-b:v', $main::sizes{"target_bitrate"} . "k",
'-strict', '-2',
'-quality', $main::encoding_params{"quality"},
'-qmin', $main::encoding_params{'qmin'},
'-crf', $main::encoding_params{'crf'},
'-qmax', $main::encoding_params{'qmax'},
'-vf', 'scale=' . $force_width . ':-1',
'-sws_flags', 'spline',
'-lag-in-frames', '16',
'-pass', '2',
'-auto-alt-ref', '1',
$main::filenames{"filename_without_extension"} . "-temp-noresize-slice-" . $padded . ".webm" );
}
elsif ( !defined $pid ) {
warn "Fork $i failed: $!\n";
}
}
1 while wait() >= 0;
}
sub MergeSlices
{
my $file;
my $concat_string = '';
my $i = 0;
my $dh = DirHandle->new( $main::filenames{"path"} ) or die "Can't open " . $main::filenames{"path"} . " : $!\n";
my @files = grep( /$main::filenames{"filename_without_extension"}-temp-noresize-slice-.*$/, $dh->read() );
my $padded;
foreach $file (@files) {
$padded = sprintf "%03d", $i;
$concat_string = $concat_string . " " . $main::filenames{"filename_without_extension"} . "-temp-noresize-slice-" . $padded . ".webm +";
$i++;
}
print $concat_string;
system( "mkvmerge -q -o " . $main::filenames{"filename_without_extension"} . "-temp-result-webm.webm $concat_string" );
}
sub DeleteTempFiles {
my $file;
my $dh = DirHandle->new( $main::filenames{"path"} ) or die "Can't open $path : $!\n";
my @files = grep( /$filenames{"filename_without_extension"}-temp-.*$/, $dh->read() );
foreach $file (@files) {
unlink $main::filenames{"path"} . $file or warn "Could not delete temporary $file: $!";
}
DeletePassLogs();
}
sub DeletePassLogs {
my $file;
my $dh = DirHandle->new( $main::filenames{"path"} ) or die "Can't open $path : $!\n";
my @files = grep( /$filenames{"filename_without_extension"}.*\.log$/, $dh->read() );
foreach $file (@files) {
unlink $main::filenames{"path"} . $file or warn "Could not delete log $file: $!";
}
}
sub CalculateAudioBitrate {
my $result_webm_filename = $main::filenames{"filename_without_extension"} . "-temp-result-webm.webm";
my $st = stat($result_webm_filename);
my $webm_size = $st->size;
return floor( ( ( ( $main::sizes{"target_video_size"} * 1024 ) - $webm_size ) * 8 ) / $main::videoInfo{'durationsecs'} );
}
sub EncodeAudio {
my $multiplier = shift(@_);
$multiplier //= 1;
my $audio_filename = $main::filenames{"audiotrack_filename"};
print "Encoding audio with " . floor( CalculateAudioBitrate() * $multiplier ) . " bitrate:\n";
system( "ffmpeg -v quiet -stats -i " . $main::filenames{"audiotrack_filename"} . " -c:a libopus -b:a " . floor( CalculateAudioBitrate() * $multiplier ) . " -vbr on -vn -sn -y -hide_banner " . $main::filenames{"filename_without_extension"} . "-temp-result-opus.mkv" );
print "Muxing:\n";
system( "mkvmerge -q -A " . $main::filenames{"filename_without_extension"} . "-temp-result-webm.webm " . $main::filenames{"filename_without_extension"} . "-temp-result-opus.mkv -o " . $main::filenames{"filename_without_extension"} . "-result" . $additional_filename_num . ".webm" );
my $finalsize = ceil( ( stat( $main::filenames{"filename_without_extension"} . "-result" . $additional_filename_num . ".webm" )->size ) / 1024 );
print "\n\n\n--------\n";
print "Final Video + Audio Size is $finalsize KB\n";
if ( $finalsize > $main::sizes{"target_video_size"} )
{
print "Final sizing failed\n";
EncodeAudio( $multiplier * ( ( $main::sizes{"target_video_size"} / $finalsize ) * 0.99 ) );
}
}
__END__
=encoding utf-8
=head1 NAME
sample - Using Getopt::Long and Pod::Usage
=head1 SYNOPSIS
Multithreaded VP9 encoding
Данная версия является альфой от 08.04.2015, за обновлениями следите в https://2ch.hk/s/
2ch-webm-resizer.pl [options] [file_to_encode]
Options:
General:
-width (Default: not set) force resizing to a specified width
Size:
-target (Default: 6144) target file size in kilobytes
-opus (Default: 70) opus bitrate to calculate the video size from (used only for calculations)
Quality:
-crf (Default: 15) target quality quantizer
-qmin (Default: 8) maximum allowed quality while encoding with crf
-qmax (Default: 60) minimum allowed quality while encoding with crf
-cpu_used (Default: 1) cpu_used value for encoding (-16...16)
-quality (Default: good)
-aq_mode (Default: 2)
Performance:
-threads (Default: 4) number of ffmpeg encoder threads
Unimplemented:
-help brief help message (not implemented)
-man full documentation (not implemented)
=over 8
=item B<-help>
Print a brief help message and exits.
=item B<-man>
Prints the manual page and exits.
=back
=head1 DESCRIPTION
B<This program> will read the given input file(s) and do something
useful with the contents thereof.
=cut