blib/lib/Media/Convert/Pipe.pm

filter: Statementstmt 40.5% 58 / 143 Branchbran 17.1% 11 / 64 Conditioncond 7.4% 2 / 27 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 76.9% 10 / 13 Podpod 25.0% 1 / 4 totaltotal 30.8% 82 / 266 CC 46 SCAR 55.9
CC
46
Cov
30%
run
423.5
run_progress
182.0
_build_progress
6.0
CRAP 267.6
↑ Summary blib/lib/Media/Convert/Asset.pm »
1 package Media::Convert::Pipe;
2  
3 13 ▶use JSON::MaybeXS qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use MooseX::ClassAttribute;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 13 ▶use autodie qw/:all/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Media::Convert::Pipe - class to generate ffmpeg command lines with Media::Convert::Asset
13  
14 =head1 SYNOPSIS
15  
16 use Media::Convert::Asset;
17 use Media::Convert::Pipe;
18  
19 my $input_audio = Media::Convert::Asset->new(url => $input_filename_audio);
20 my $input_video = Media::Convert::Asset->new(url => $input_filename_video);
21 my $output = Media::Convert::Asset->new(url => $output_filename);
22 my $map_audio = Media::Convert::Map->new(input => $input_audio, type => "stream", choice => "audio");
23 my $map_video = Media::Convert::Map->new(input => $input_video, type => "stream", choice => "video");
24 my $pipe = Media::Convert::Pipe->new(inputs => [$input_audio, $input_video], map => [ $map_audio, $map_video ], output => $output);
25 $pipe->run();
26  
27 # Or, if progress information is wanted:
28 sub print_progress {
29 my $percentage = shift;
30 print "Transcoding progress: $percentage\r";
31 }
32 $pipe->progress(\&print_progress);
33 $pipe->run();
34  
35 =head1 DESCRIPTION
36  
37 C<Media::Convert::Pipe> is the class in the C<Media::Convert> package
38 which does most of the hard work inside C<Media::Convert>. It generates
39 and runs the ffmpeg command line, capturing the output where required.
40  
41 C<Media::Convert::Pipe> will compare the properties of the input
42 C<Media::Convert::Asset> objects against those of the output
43 C<Media::Convert::Asset> object, and will add the necessary parameters
44 to the ffmpeg command line to convert the audio/video material in the
45 input file to the format required by the output file.
46  
47 =head1 ATTRIBUTES
48  
49 The following attributes are supported by C<Media::Convert::Pipe>.
50  
51 =head2 inputs
52  
53 The objects to read from. Must be C<Media::Convert::Asset> objects. If
54 more than one object is passed, a value for the C<map> attribute may be
55 required to tell ffmpeg which audio/video stream to read from which
56 file.
57  
58 More input objects can be added using the C<add_input> method, and they
59 can all be removed using the C<clear_inputs> object.
60  
61 =cut
62  
63 has 'inputs' => (
64 traits => ['Array'],
65 is => 'ro',
66 isa => 'ArrayRef[Media::Convert::Asset]',
67 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
68 13 clearer => 'clear_inputs',
69 handles => {
70 add_input => 'push',
71 },
72 );
73  
74 =head2 output
75  
76 The object to write to. Must be a C<Media::Convert::Asset> object.
77 Required.
78  
79 If any properties differ between the input objects and the output
80 object (e.g., the codec, pixel format, audio sample frequency, video
81 resolution, etc etc etc), C<Media::Convert::Pipe> will add the necessary
82 command-line options to the ffmpeg command line to convert the video
83 from the input file to the output file.
84  
85 =cut
86  
87 13 has 'output' => (
88 is => 'rw',
89 isa => 'Media::Convert::Asset',
90 required => 1,
91 );
92  
93 =head2 map
94  
95 Array of C<Media::Convert::Map> objects, used to manipulate which audio
96 and video streams exactly will be written to the output file (and in
97 which order, etc).
98  
99 For more info: see L<Media::Convert::Map>.
100  
101 Maps can be cleared with C<clear_map> and added to with C<add_map>.
102  
103 =cut
104  
105 has 'map' => (
106 traits => ['Array'],
107 is => 'ro',
108 isa => 'ArrayRef[Media::Convert::Map]',
109 9 ▶ default => sub {[]},
Subroutine __ANON__: called CC=1 SCAR=0.0
110 13 clearer => 'clear_map',
111 handles => {
112 add_map => 'push',
113 },
114 );
115  
116 =head2 vcopy
117  
118 Boolean. If true, explicitly tell C<Media::Convert> to copy video
119 without transcoding it.
120  
121 Normally, C<Media::Convert> should not request a transcode if all
122 attributes of the input file and the output file are exactly the same.
123 Getting this right may sometimes be problematic, however. In such cases,
124 it can be good to explicitly say that the video should not be
125 transcoded. That's what this property is for.
126  
127 =cut
128  
129 13 has 'vcopy' => (
130 isa => 'Bool',
131 is => 'rw',
132 default => 1,
133 );
134  
135 =head2 acopy
136  
137 The same as C<vcopy>, but for audio rather than video.
138  
139 =cut
140  
141 13 has 'acopy' => (
142 isa => 'Bool',
143 is => 'rw',
144 default => 1,
145 );
146  
147 =head2 vskip
148  
149 Tell C<Media::Convert> that the output file should not contain any
150 video (i.e., that it should skip handling of any video). This is implied
151 if the output container does not support video streams (e.g., the .wav
152 format), but is required if it does.
153  
154 Defaults to off (i.e., do not skip video)
155  
156 =cut
157  
158 13 has 'vskip' => (
159 isa => 'Bool',
160 is => 'rw',
161 default => 0,
162 );
163  
164 =head2 askip
165  
166 The same as C<vskip>, but for audio rather than video.
167  
168 Defaults to off (i.e., do not skip audio)
169  
170 =cut
171  
172 13 has 'askip' => (
173 isa => 'Bool',
174 is => 'rw',
175 default => 0,
176 );
177  
178 =head2 dskip
179  
180 The same as C<vskip>, but for data streams.
181  
182 Defaults to on (i.e., I<do> skip data streams), as some containers do not
183 support data streams.
184  
185 =cut
186  
187 13 has 'dskip' => (
188 isa => 'Bool',
189 is => 'rw',
190 default => 1,
191 );
192  
193 =head2 sskip
194  
195 The same as C<vskip>, but for subtitle streams.
196  
197 Defaults to off (i.e., I<do> skip subtitle streams).
198  
199 =cut
200  
201 13 has 'sskip' => (
202 isa => 'Bool',
203 is => 'rw',
204 default => 0,
205 );
206  
207 =head2 multipass
208  
209 Boolean. If true, the C<run> method performs a two-pass encode, rather
210 than a single-pass encode.
211  
212 Two-pass encodes may generate a better end result, but require more
213 time to perform.
214  
215 =cut
216  
217 13 has 'multipass' => (
218 isa => 'Bool',
219 is => 'rw',
220 default => 0,
221 );
222  
223 =head2 progress
224  
225 Normally, C<Media::Convert::Pipe> shows (and runs) the ffmpeg command.
226 Any output of the ffmpeg command is shown on stdout.
227  
228 If this attribute is set to a coderef, then the following happens:
229  
230 =over *
231  
232 =item The ffmpeg command line that is executed gains "-progress /dev/stdout" parameters
233  
234 =item The output of the ffmpeg command is parsed, and the completion percentage calculated (and all other output suppressed),
235  
236 =item The coderef that was passed to this attribute is executed with the completion percentage as the only parameter whenever the percentage changes.
237  
238 =back
239  
240 =cut
241  
242 13 has 'progress' => (
243 isa => 'CodeRef',
244 is => 'rw',
245 predicate => '_has_progress',
246 lazy => 1,
247 builder => '_build_progress',
248 );
249  
250 1 ▶sub has_progress {
Subroutine has_progress: called CC=2 SCAR=8.1
Pod undocumented
251 1 my $self = shift;
252 1 ▶ return $self->_has_progress || __PACKAGE__->has_class_progress;
Condition33%1/3
$self->_has_progress || __PACKAGE__->has_class_progress
l!l&&r!l&&!r
001
MC/DC0%0/2
$self->_has_progress || __PACKAGE__->has_class_progress
$self->_has_progress __PACKAGE__->has_class_progress
Truth table33%1/3
A || B
A= $self->_has_progressB= __PACKAGE__->has_class_progress
ABresult
1X1
011
000
253 }
254  
255 0 ▶sub _build_progress {
Subroutine _build_progress: not called CC=2 SCAR=17.9
256 0 my $self = shift;
257 0 ▶ if(__PACKAGE__->has_class_progress) {
Branch: 1 branch
BranchTrueFalseTotal
if (__PACKAGE__->has_class_progress) 0 0 0
258 0 return __PACKAGE__->class_progress;
259 }
260 }
261  
262 =head2 class_progress
263  
264 The same as C<progress>, but as a class attribute. If set, then every object
265 created after this attribute was set will have C<progress> set to the same
266 value by default.
267  
268 To remove this again, use C<clear_class_progress>
269  
270 =cut
271  
272 13 class_has class_progress => (
273 isa => 'CodeRef',
274 is => 'rw',
275 predicate => 'has_class_progress',
276 clearer => 'clear_class_progress',
277 );
278  
279 13 has 'has_run' => (
280 isa => 'Bool',
281 is => 'rw',
282 default => 0,
283 traits => ['Bool'],
284 handles => {
285 run_complete => 'set',
286 }
287 );
288  
289 0 ▶sub run_progress {
Subroutine run_progress: not called CC=13 SCAR=52.0
Pod undocumented
290 0 my $self = shift;
291 0 my $command = shift;
292 0 my $pass = shift;
293 0 my $multipass = shift;
294 0 my ($in, $out, $err);
295 0 my $running;
296 0 my @lines;
297 0 my $old_perc = -1;
298 0 my %vals;
299  
300 my $length;
301 0 ▶ if(defined($self->output->duration)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->output->duration) { } 0 0 0
302 0 $length = $self->output->duration * 1000000;
303 } else {
304 0 foreach my $input(@{$self->inputs}) {
305 0 my $dur = $input->duration * 1000000;
306 0 ▶ if(!defined($length)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $length) 0 0 0
307 0 $length = $dur;
308 0 next;
309 }
310 0 ▶ $length = $length < $dur ? $length : $dur;
Branch: 1 branch
BranchTrueFalseTotal
$length < $dur ? : 0 0 0
311 }
312 }
313 0 shift @$command;
314 0 unshift @$command, ('ffmpeg', '-progress', '/dev/stdout');
315 0 open my $ffmpeg, "-|", @{$command};
316 0 ▶ while(<$ffmpeg>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $ffmpeg)) 0 0 0
317 0 /^(\w+)=(.*)$/;
318 0 $vals{$1} = $2;
319 0 ▶ if($1 eq 'progress') {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq "progress") 0 0 0
320 0 ▶ next if $vals{out_time_ms} eq "N/A";
Branch: 1 branch
BranchTrueFalseTotal
if $vals{"out_time_ms"} eq "N/A" 0 0 0
321 0 ▶ next if $length eq "N/A";
Branch: 1 branch
BranchTrueFalseTotal
if $length eq "N/A" 0 0 0
322 0 my $perc = int($vals{out_time_ms} / $length * 100);
323 0 ▶ if($vals{progress} eq 'end') {
Branch: 1 branch
BranchTrueFalseTotal
if ($vals{"progress"} eq "end") 0 0 0
324 0 $perc = 100;
325 }
326 0 ▶ if($multipass) {
Branch: 1 branch
BranchTrueFalseTotal
if ($multipass) 0 0 0
327 0 $perc = int($perc / 2);
328 }
329 0 ▶ if($pass == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pass == 2) 0 0 0
330 0 $perc += 50;
331 }
332 0 ▶ if($perc != $old_perc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($perc != $old_perc) 0 0 0
333 0 $old_perc = $perc;
334 0 &{$self->progress}($perc);
335 }
336 }
337 }
338 0 $self->run_complete;
339 }
340  
341 =head1 METHODS
342  
343 =head2 run
344  
345 Run the ffmpeg command.
346  
347 If this method is not run at least once, the object's destructor will
348 issue a warning.
349  
350 =cut
351  
352 10 ▶sub run {
Subroutine run: called CC=31 SCAR=60.5
Pod documented
353 10 my $self = shift;
354 10 my $pass;
355 10 my @attrs = (
356 'video_codec' => 'vcopy',
357 'video_size' => 'vcopy',
358 'video_width' => 'vcopy',
359 'video_height' => 'vcopy',
360 'video_bitrate' => 'vcopy',
361 'video_framerate' => 'vcopy',
362 'pix_fmt' => 'vcopy',
363 'audio_codec' => 'acopy',
364 'audio_bitrate' => 'acopy',
365 'audio_samplerate' => 'acopy',
366 );
367 10 my @video_attrs = ('video_codec', 'video_size', 'video_width', 'video_height', 'video_bitrate', 'video_framerate', 'pix_fmt');
368 10 my @audio_attrs = ('audio_codec', 'audio_bitrate', 'audio_samplerate');
369  
370 10 ▶ for($pass = 1; $pass <= ($self->multipass ? 2 : 1); $pass++) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pass <= ($self->multipass ? 2 : 1)) 10 0 10
$self->multipass ? : 1 9 10
371 10 my @command = ("ffmpeg", "-loglevel", "warning", "-y");
372 10 foreach my $input(@{$self->inputs}) {
373 10 ▶ if($self->multipass) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->multipass) 1 9 10
374 1 $input->pass($pass);
375 1 $self->output->pass($pass);
376 }
377 10 ▶ while(scalar(@attrs) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @attrs > 0) 46 4 50
378 46 my $attr = shift @attrs;
379 46 my $target = shift @attrs;
380 46 ▶ next unless $self->meta->get_attribute($target)->get_value($self);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->meta->get_attribute($target)->get_value($self) 40 6 46
381 6 my $oval = $self->output->meta->find_attribute_by_name($attr)->get_value($self->output);
382 6 my $ival = $input->meta->find_attribute_by_name($attr)->get_value($input);
383 0 ▶ if(defined($oval) && $ival ne $oval) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $oval and $ival ne $oval) 0 0 0
Condition0%0/3
defined $oval and $ival ne $oval
!ll&&!rl&&r
000
MC/DC0%0/2
defined $oval and $ival ne $oval
defined $oval $ival ne $oval
Truth table0%0/3
A and B
A= defined $ovalB= $ival ne $oval
ABresult
0X0
100
111
384 0 $self->meta->get_attribute($target)->set_value($self, 0);
385 }
386 }
387 4 push @command, $input->readopts($self->output);
388 }
389 4 ▶ if(!$self->vcopy() && !$self->vskip()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->vcopy or $self->vskip) 4 0 4
Condition33%1/3
$self->vcopy or $self->vskip
l!l&&r!l&&!r
004
MC/DC0%0/2
$self->vcopy or $self->vskip
$self->vcopy $self->vskip
Truth table33%1/3
A or B
A= $self->vcopyB= $self->vskip
ABresult
1X1
011
000
390 4 my $isize = $self->inputs->[0]->video_size;
391 0 my $osize = $self->output->video_size;
392 0 ▶ if(defined($isize) && defined($osize) && $isize ne $osize) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $isize and defined $osize and $isize ne $osize) 0 0 0
Condition0%0/6
defined $isize and defined $osize and $isize ne $osize
!ll&&!rl&&r
000
defined $isize and defined $osize
!ll&&!rl&&r
000
MC/DC0%0/3
defined $isize and defined $osize and $isize ne $osize
defined $isize defined $osize $isize ne $osize
Truth table0%0/4
A and B and C
A= defined $isizeB= defined $osizeC= $isize ne $osize
ABCresult
0XX0
10X0
1100
1111
393 0 push @command, ("-vf", "scale=" . $osize);
394 }
395 }
396 0 foreach my $map(@{$self->map}) {
397 0 my $in_map = $map->input;
398 0 my $index;
399 0 ▶ for(my $i=0; $i<=$#{$self->inputs}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$self->inputs;}) 0 0 0
400 0 ▶ if($in_map == ${$self->inputs}[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_map == do { $self->inputs }->[$i]) 0 0 0
401 0 $index = $i;
402 }
403 }
404 0 push @command, $map->arguments($index);
405 }
406 0 ▶ if($self->vcopy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->vcopy) 0 0 0
407 0 push @command, ('-c:v', 'copy');
408 }
409 0 ▶ if($self->acopy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->acopy) 0 0 0
410 0 push @command, ('-c:a', 'copy');
411 }
412 0 ▶ if($self->vskip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->vskip) 0 0 0
413 0 push @command, ('-vn');
414 }
415 0 ▶ if($self->askip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->askip) 0 0 0
416 0 push @command, ('-an');
417 }
418 0 ▶ if($self->dskip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dskip) 0 0 0
419 0 push @command, ('-dn');
420 }
421 0 ▶ if($self->sskip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->sskip) 0 0 0
422 0 push @command, ('-sn');
423 }
424 0 ▶ if($self->vcopy || $self->acopy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->vcopy or $self->acopy) 0 0 0
Condition0%0/3
$self->vcopy or $self->acopy
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->vcopy or $self->acopy
$self->vcopy $self->acopy
Truth table0%0/3
A or B
A= $self->vcopyB= $self->acopy
ABresult
1X1
011
000
425 0 push @command, ('-fflags', '+genpts');
426 }
427 0 ▶ if($self->vcopy || $self->acopy || ($self->output->can('has_fragment_start') && $self->output->has_fragment_start)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->vcopy or $self->acopy or $self->output->can("has_fragment_start") and $self->output->has_fragment_start) 0 0 0
Condition0%0/9
$self->vcopy or $self->acopy or $self->output->can("has_fragment_start") and $self->output->has_fragment_start
l!l&&r!l&&!r
000
$self->vcopy or $self->acopy
l!l&&r!l&&!r
000
$self->output->can("has_fragment_start") and $self->output->has_fragment_start
!ll&&!rl&&r
000
MC/DC0%0/4
$self->vcopy or $self->acopy or $self->output->can("has_fragment_start") and $self->output->has_fragment_start
$self->vcopy $self->acopy $self->output->can("has_fragment_start") $self->output->has_fragment_start
Truth table0%0/5
A or B or C and D
A= $self->vcopyB= $self->acopyC= $self->output->can("has_fragment_start")D= $self->output->has_fragment_start
ABCDresult
1XXX1
01XX1
00111
000X0
00100
428 0 push @command, ('-avoid_negative_ts', 'make_zero');
429 }
430 0 push @command, $self->output->writeopts($self);
431  
432 0 print "Running: '" . join ("' '", @command) . "'\n";
433 0 ▶ if($self->has_progress) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_progress) { } 0 0 0
434 0 $self->run_progress(\@command, $pass, $self->multipass);
435 } else {
436 0 system(@command);
437 }
438 }
439 0 foreach my $input(@{$self->inputs}) {
440 0 $input->clear_pass;
441 }
442 0 $self->output->clear_pass;
443 0 $self->run_complete;
444 }
445  
446 10 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=2 SCAR=7.2
Pod undocumented
447 10 ▶ if(!(shift->has_run)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not +(shift())->has_run) 10 0 10
448 10 confess "object destructor for Media::Convert::Pipe object entered without having seen a run!";
449 }
450 }
451  
452 13 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
453  
454 13 1;
↑ Summary blib/lib/Media/Convert/Asset.pm »