blib/lib/Media/Convert/Asset.pm

filter: Statementstmt 33.2% 107 / 322 Branchbran 10.5% 15 / 142 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 41.3% 19 / 46 Podpod 0.0% 0 / 3 totaltotal 26.2% 141 / 538 CC 84 SCAR 52.1
CC
84
Cov
25%
writeopts
812.0
_probe_videobitrate
20.0
_probe_videominrate
20.0
CRAP 183.5
« blib/lib/Media/Convert/Pipe.pm ↑ Summary blib/lib/Media/Convert/Map.pm »
1 package Media::Convert::Asset;
2  
3 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 16 our $VERSION;
8  
9 16 ▶use Media::Convert;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 16 ▶use DateTime::Format::ISO8601;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 16 ▶use autodie qw/:all/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 Media::Convert::Asset - Media::Convert representation of a media asset
18  
19 =head1 SYNOPSIS
20  
21 use Media::Convert::Asset;
22 use Media::Convert::ProfileFactory;
23 use Media::Convert::Pipe;
24  
25 # convert any input file to VP9 at recommended settings for vertical resolution and frame rate
26 my $input = Media::Convert::Asset->new(url => $input_filename);
27 my $profile = Media::Convert::ProfileFactory->new("vp9", $input);
28 my $output = Media::Convert::Asset->new(url => $output_filename, reference => $profile);
29 Media::Convert::Pipe->new(inputs => [$input], output => $output)->run();
30  
31 # do that again; but this time, force vorbis audio:
32 $output = Media::Convert::Asset->new(url => $other_filename, reference => $profile);
33 $output->audio_codec("libvorbis");
34 Media::Convert::Pipe->new(inputs => [$input], output => $output)->run();
35  
36 # Merge the video stream from one file with the audio stream from another,
37 # and convert to VP9
38 use Media::Convert::Map;
39 my $input_v = Media::Convert::Asset->new(url => $input_video_filename);
40 my $input_a = Media::Convert::Asset->new(url => $input_audio_filename);
41 $profile = Media::Convert::ProfileFactory->new("vp9", $input);
42 $output = Media::Convert::Asset->new(url => $merged_filename, reference => $profile);
43 my $map_v = Media::Convert::Map->new(input => $input_v, type => "stream", choice => "video");
44 my $map_a = Media::Convert::Map->new(input => $input_a, type => "stream", choice => "audio");
45 Media::Convert::Pipe->new(inputs => [$input_a, $input_v], map => [$map_a, $map_v], output => $output)->run();
46  
47 =head1 DESCRIPTION
48  
49 The C<Media::Convert::Asset> package is used to represent media assets
50 inside C<Media::Convert>. It is a C<Moose>-based base class for much of
51 the other classes in C<Media::Convert>.
52  
53 There is one required attribute, C<url>, which represents the filename
54 of the asset.
55  
56 If the C<url> attribute points to an existing file and an attempt is
57 made to read any of the codec, framerate, bit rate, or similar
58 attributes (without explicitly writing to them first), then
59 C<Media::Convert::Asset> will call C<ffprobe> on the file in question,
60 and use that to populate the requested attributes. If it does not, or
61 C<ffprobe> is incapable of detecting the requested attribute (which may
62 be the case for things like audio or video bitrate), then the attribute
63 in question will resolve to C<undef>.
64  
65 If the C<url> attribute does not point to an existing file and an
66 attempt is made to read any of the codec, framerate, bit rate, or
67 similar attributes (without explicitly writing to them first), then they
68 will resolve to C<undef>. However, if the C<reference> attribute is
69 populated with another C<Media::Convert::Asset> object, then reading any of the
70 codec, framerate, bit rate, or similar attributes (without explicitly
71 writing to them first) will resolve to the value of the requested
72 attribute that is set or detected on the C<reference> object.
73  
74 The return value of C<Media::Convert::ProfileFactory-E<gt>create()> is
75 also a Media::Convert object, but with different implementations of
76 some of the probing methods; this allows it to choose the correct values
77 for things like bitrate and encoder speed based on properties set in the
78 input object provided to the
79 C<Media::Convert::ProfileFactory-E<gt>create()> method.
80  
81 For more information on how to use the files referred to in the
82 C<Media::Convert::Asset> object in an ffmpeg command line, please see
83 C<Media::Convert::Pipe>.
84  
85 =head1 ATTRIBUTES
86  
87 The following attributes are supported by C<Media::Convert::Asset>. All
88 attributes will be probed from ffprobe output unless noted otherwise.
89  
90 =cut
91  
92 16 ▶use JSON::MaybeXS qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
93 16 ▶use Media::Convert::CodecMap qw/detect_to_write/;
Subroutine BEGIN: called CC=1 SCAR=0.0
94  
95 16 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
96  
97 =head2 url
98  
99 The filename of the asset this object should deal with. Required at
100 construction time. Will not be probed.
101  
102 =cut
103  
104 16 has 'url' => (
105 is => 'rw',
106 required => 1,
107 );
108  
109 =head2 mtime
110  
111 The mtime of the file backing this asset. Only defined if the file
112 exists at the time the attribute is first read, and is not updated later
113 on.
114  
115 =cut
116  
117 16 has 'mtime' => (
118 is => 'ro',
119 lazy => 1,
120 builder => '_probe_mtime',
121 );
122  
123 0 ▶sub _probe_mtime {
Subroutine _probe_mtime: not called CC=3 SCAR=24.8
124 0 my $self = shift;
125 0 ▶ if($self->has_reference) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_reference) 0 0 0
126 0 return $self->reference->mtime;
127 }
128 0 my @statdata = stat($self->url);
129 0 ▶ if(scalar(@statdata) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @statdata == 0) 0 0 0
130 0 return;
131 }
132 0 return $statdata[9];
133 }
134  
135 =head2 canonical_duration
136  
137 Which value to take as the canonical duration while probing the duration
138 of a media file.
139  
140 Unfortunately, some versions of ffprobe have bugs in that they either
141 read or write corrupt values for some parts of a media file. Sometimes
142 the video stream length is correct but the container length is not,
143 sometimes it is the other way around.
144  
145 Since, given that, it is not possible to choose a source that will
146 always work as the probed value for the L<duration> attribute, this
147 needs to be configurable in some way.
148  
149 This option can take the following values:
150  
151 =over 4
152  
153 =item container
154  
155 Uses the duration value that is parsed from the container file, rather
156 than any particular stream. If the parsing of that value is not buggy,
157 this is the right thing to do, and therefore this is also the default.
158  
159 =item video
160  
161 Uses the duration value that is parsed from the first video stream.
162  
163 =item audio
164  
165 Uses the duration value that is parsed from the first audio stream.
166  
167 =back
168  
169 =cut
170  
171 16 has 'canonical_duration' => (
172 is => 'ro',
173 isa => 'Str',
174 default => 'container',
175 );
176  
177 =head2 creation_time
178  
179 The time at which this asset was created, as parsed from the C<creation_time>
180 tag, if that is set.
181  
182 has 'creation_time' => (
183 is => 'ro',
184 isa => 'Maybe[DateTime]',
185 builder => '_probe_creation_time',
186 lazy => 1,
187 );
188  
189 sub _probe_creation_time {
190 my $self = shift;
191 if($self->has_reference) {
192 return $self->reference->creation_time;
193 }
194 return DateTime::Format::ISO8601->parse_datetime($self->_get_probedata->{format}{tags}{creation_time});
195 }
196  
197 =head2 duration
198  
199 The duration of this asset. See C<canonical_duration> for details on how
200 it is probed.
201  
202 =cut
203  
204 16 has 'duration' => (
205 is => 'rw',
206 builder => '_probe_duration',
207 lazy => 1,
208 );
209  
210 1 ▶sub _probe_duration {
Subroutine _probe_duration: called CC=6 SCAR=26.2
211 1 my $self = shift;
212 1 ▶ if($self->has_reference) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_reference) 0 1 1
213 0 return $self->reference->duration;
214 }
215 1 ▶ if($self->duration_style eq "seconds") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->duration_style eq 'seconds') { } 1 0 1
216 1 ▶ if($self->canonical_duration eq 'container') {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->canonical_duration eq 'container') { } 1 0 1
elsif ($self->canonical_duration eq 'video') { } 0 0 0
elsif ($self->canonical_duration eq 'audio') { } 0 0 0
217 1 return $self->_get_probedata->{format}{duration};
218 } elsif($self->canonical_duration eq 'video') {
219 0 return $self->_get_videodata->{duration};
220 } elsif($self->canonical_duration eq 'audio') {
221 0 return $self->_get_audiodata->{duration};
222 } else {
223 # don't support this
224 ...
225 0 }
226 } else {
227 0 return $self->duration_frames;
228 }
229 }
230  
231 =head2 duration_frames
232  
233 The number of frames in this asset.
234  
235 =cut
236  
237 16 has 'duration_frames' => (
238 is => 'rw',
239 builder => '_probe_duration_frames',
240 lazy => 1,
241 );
242  
243 0 ▶sub _probe_duration_frames {
Subroutine _probe_duration_frames: not called CC=1 SCAR=6.9
244 0 return shift->_get_videodata->{duration_ts};
245 }
246  
247 =head2 duration_style
248  
249 The time unit is used for the C<duration> attribute. One of 'seconds'
250 (default) or 'frames'. Will not be probed.
251  
252 Deprecated; will be removed in a future release. Use duration_frames
253 instead of setting this to a non-default value.
254  
255 =cut
256  
257 0 ▶sub _warn_duration {
Subroutine _warn_duration: not called CC=1 SCAR=6.9
258 0 carp "setting duration_style is deprecated. Rather use duration_frames."
259 }
260  
261 16 has 'duration_style' => (
262 is => 'rw',
263 default => 'seconds',
264 trigger => \&_warn_duration,
265 );
266  
267 =head2 force_key_frames
268  
269 Can be set to an expression that can be passed to ffmpeg's C<-force_key_frames>
270 parameter. Will not be probed.
271  
272 =cut
273  
274 16 has 'force_key_frames' => (
275 is => 'rw',
276 isa => 'Maybe[Str]',
277 builder => '_probe_force_key_frames',
278 lazy => 1,
279 predicate => 'has_force_key_frames',
280 );
281  
282 0 ▶sub _probe_force_key_frames {
Subroutine _probe_force_key_frames: not called CC=2 SCAR=17.9
283 0 my $self = shift;
284 0 ▶ if($self->has_reference) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_reference) 0 0 0
285 0 return $self->reference->force_key_frames;
286 }
287 0 return;
288 }
289  
290 =head2 video_codec
291  
292 The codec in use for the video stream. Note that C<ffprobe> will
293 sometimes use a string (e.g., "vp8") that is not the best choice when
294 instructing C<ffmpeg> to transcode video to the said codec (for vp8, the
295 use of "libvpx" is recommended). C<Media::Convert::CodecMap> is used to
296 map detected codecs to output codecs and resolve this issue.
297  
298 =cut
299  
300 16 has 'video_codec' => (
301 is => 'rw',
302 builder => '_probe_videocodec',
303 lazy => 1,
304 );
305  
306 12 ▶sub _probe_videocodec {
Subroutine _probe_videocodec: called CC=2 SCAR=6.9
307 12 my $self = shift;
308 12 ▶ if($self->has_reference) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_reference) 1 11 12
309 1 return $self->reference->video_codec;
310 }
311 11 return $self->_get_videodata->{codec_name};
312 }
313  
314 =head2 audio_codec
315  
316 The codec in use for the audio stream. Note that C<ffprobe> will
317 sometimes use a string (e.g., "vorbis") that is not the best choice when
318 instructing C<ffmpeg> to transcode audio to the said codec (for vorbis,
319 the use of "libvorbis" is recommended). C<Media::Convert::CodecMap> is used to
320 map detected codecs to output codecs and resolve this issue.
321  
322 =cut
323  
324 16 has 'audio_codec' => (
325 is => 'rw',
326 builder => '_probe_audiocodec',
327 lazy => 1,
328 );
329  
330 0 ▶sub _probe_audiocodec {
Subroutine _probe_audiocodec: not called CC=2 SCAR=17.9
331 0 my $self = shift;
332 0 ▶ if($self->has_reference) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_reference) 0 0 0
333 0 return $self->reference->audio_codec;
334 }
335 0 return $self->_get_audiodata->{codec_name};
336 }
337  
338 =head2 video_size
339  
340 A string representing the resolution of the video in C<WxH> format,
341 where W is the width and H is the height.
342  
343 This attribute is special in that in contrast to all the other
344 attributes, it is not provided directly by C<ffprobe>; instead, when
345 this parameter is read, the C<video_width> and C<video_height>
346 attributes are read and combined.
347  
348 That does mean that you should not read this attribute, and based on
349 that possibly set the height and/or width attributes of a video (or vice
350 versa). Instead, you should read I<either> the C<video_width> and
351 C<video_height> attribute, I<or> this one.
352  
353 Failure to follow this rule will result in undefined behaviour.
354  
355 =cut
356  
357 16 has 'video_size' => (
358 is => 'rw',
359 builder => '_probe_videosize',
360 lazy => 1,
361 predicate => 'has_video_size',
362 );
363  
364 4 ▶sub _probe_videosize {
Subroutine _probe_videosize: called CC=4 SCAR=22.9
365 4 my $self = shift;
366 4 ▶ if($self->has_reference) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_reference) 0 4 4
367 0 return $self->reference->video_size;
368 }
369 4 my $width = $self->video_width;
370 0 my $height = $self->video_height;
371 0 ▶ return unless defined($width) && defined($height);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $width and defined $height 0 0 0
Condition0%0/3
defined $width and defined $height
!ll&&!rl&&r
000
MC/DC0%0/2
defined $width and defined $height
defined $width defined $height
Truth table0%0/3
A and B
A= defined $widthB= defined $height
ABresult
0X0
100
111
372 0 return $self->video_width . "x" . $self->video_height;
373 }
374  
375 =head2 video_width
376  
377 The width of the video, in pixels.
378  
379 =cut
380  
381 16 has 'video_width' => (
382 is => 'rw',
383 builder => '_probe_width',
384 lazy => 1,
385 );
386  
387 4 ▶sub _probe_width {
Subroutine _probe_width: called CC=3 SCAR=12.7
388 4 my $self = shift;
389 4 ▶ if($self->has_reference) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_reference) 0 4 4
390 0 return $self->reference->video_width;
391 }
392 4 ▶ if($self->has_video_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_video_size) { } 0 4 4
393 0 return (split /x/, $self->video_size)[0];
394 } else {
395 4 return $self->_get_videodata->{width};
396 }
397 }
398  
399 =head2 video_height
400  
401 The height of the video, in pixels.
402  
403 =cut
404  
405 16 has 'video_height' => (
406 is => 'rw',
407 builder => '_probe_height',
408 lazy => 1,
409 );
410  
411 0 ▶sub _probe_height {
Subroutine _probe_height: not called CC=3 SCAR=24.8
412 0 my $self = shift;
413 0 ▶ if($self->has_reference) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_reference) 0 0 0
414 0 return $self->reference->video_height;
415 }
416 0 ▶ if($self->has_video_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_video_size) { } 0 0 0
417 0 return (split /x/, $self->video_size)[1];
418 } else {
419 0 return $self->_get_videodata->{height};
420 }
421 }
422  
423 =head2 video_bitrate
424  
425 The bit rate of this video, in bits per second.
426  
427 Note that not all container formats support probing the bitrate of the
428 encoded video or audio; when read on input objects with those that do
429 not, this will resolve to C<undef>.
430  
431 =cut
432  
433 16 has 'video_bitrate' => (
434 is => 'rw',
435 builder => '_probe_videobitrate',
436 lazy => 1,
437 );
438  
439 0 ▶sub _probe_videobitrate {
Subroutine _probe_videobitrate: not called CC=4 SCAR=30.0
440 0 my $self = shift;
441 0 ▶ if($self->has_reference) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_reference) 0 0 0
442 0 return $self->reference->video_bitrate;
443 }
444 0 my $bitrate = $self->_get_videodata->{bit_rate};
445 0 ▶ if(defined($bitrate) && (!($bitrate =~ /k/))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bitrate and not $bitrate =~ /k/u) 0 0 0
Condition0%0/3
defined $bitrate and not $bitrate =~ /k/u
!ll&&!rl&&r
000
MC/DC0%0/2
defined $bitrate and not $bitrate =~ /k/u
defined $bitrate not $bitrate =~ /k/u
Truth table0%0/3
A and not(B)
A= defined $bitrateB= not $bitrate =~ /k/u
ABresult
0X0
100
111
446 0 return $bitrate / 1000;
447 }
448 0 return $bitrate;
449 }
450  
451 =head2 video_minrate
452  
453 The minimum bit rate for this video, in bits per second.
454  
455 Defaults to 0.5 * video_bitrate
456  
457 =cut
458  
459 16 has 'video_minrate' => (
460 is => 'rw',
461 builder => '_probe_videominrate',
462 lazy => 1,
463 );
464  
465 0 ▶sub _probe_videominrate {
Subroutine _probe_videominrate: not called CC=4 SCAR=30.0
466 0 my $self = shift;
467 0 my $rate;
468 0 ▶ if($self->has_reference) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_reference) 0 0 0
469 0 $rate = $self->reference->video_minrate;
470 0 ▶ if(defined($rate)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rate) 0 0 0
471 0 return $rate;
472 }
473 }
474 0 $rate = $self->video_bitrate;
475 0 ▶ if(defined($rate)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rate) 0 0 0
476 0 return $rate * 0.5;
477 }
478 0 return;
479 }
480  
481 =head2 video_maxrate
482  
483 The maximum bit rate for this video, in bits per second.
484  
485 Defaults to 1.45 * video_bitrate
486  
487 =cut
488  
489 16 has 'video_maxrate' => (
490 is => 'rw',
491 builder => '_probe_videomaxrate',
492 lazy => 1,
493 );
494  
495 =head2 video_preset
496  
497 The value for the -preset parameter. Used by some codecs, like av1 (and
498 pretty critical there).
499  
500 =cut
501  
502 16 has 'video_preset' => (
503 is => 'rw',
504 builder => '_probe_video_preset',
505 lazy => 1,
506 );
507  
508 0 ▶sub _probe_video_preset {
Subroutine _probe_video_preset: not called CC=2 SCAR=17.9
509 0 my $self = shift;
510 0 ▶ if($self->has_reference) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_reference) 0 0 0
511 0 return $self->reference->video_preset;
512 }
513 0 return;
514 }
515  
516 0 ▶sub _probe_videomaxrate {
Subroutine _probe_videomaxrate: not called CC=4 SCAR=30.0
517 0 my $self = shift;
518 0 my $rate;
519 0 ▶ if($self->has_reference) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_reference) 0 0 0
520 0 $rate = $self->reference->video_maxrate;
521 0 ▶ if(defined($rate)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rate) 0 0 0
522 0 return $rate;
523 }
524 }
525 0 $rate = $self->video_bitrate;
526 0 ▶ if(defined($rate)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rate) 0 0 0
527 0 return $rate * 1.45;
528 }
529 0 return;
530 }
531  
532 =head2 aspect_ratio
533  
534 The Display Aspect Ratio of a video. Note that with non-square pixels,
535 this is not guaranteed to be what one would expect when reading the
536 C<video_size> attribute.
537  
538 =cut
539  
540 16 has 'aspect_ratio' => (
541 is => 'rw',
542 builder => '_probe_aspect_ratio',
543 lazy => 1,
544 );
545  
546 0 ▶sub _probe_aspect_ratio {
Subroutine _probe_aspect_ratio: not called CC=2 SCAR=17.9
547 0 my $self = shift;
548 0 ▶ if($self->has_reference) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_reference) 0 0 0
549 0 return $self->reference->aspect_ratio;
550 }
551 0 return $self->_get_videodata->{display_aspect_ratio};
552 }
553  
554 =head2 audio_bitrate
555  
556 The bit rate of the audio stream on this video, in bits per second
557  
558 =cut
559  
560 16 has 'audio_bitrate' => (
561 is => 'rw',
562 builder => '_probe_audiobitrate',
563 lazy => 1,
564 );
565  
566 0 ▶sub _probe_audiobitrate {
Subroutine _probe_audiobitrate: not called CC=2 SCAR=17.9
567 0 my $self = shift;
568 0 ▶ if($self->has_reference) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_reference) 0 0 0
569 0 return $self->reference->audio_bitrate;
570 }
571 0 return $self->_get_audiodata->{bit_rate};
572 }
573  
574 =head2 audio_samplerate
575  
576 The sample rate of the audio, in samples per second
577  
578 =cut
579  
580 16 has 'audio_samplerate' => (
581 is => 'rw',
582 builder => '_probe_audiorate',
583 lazy => 1,
584 );
585  
586 0 ▶sub _probe_audiorate {
Subroutine _probe_audiorate: not called CC=2 SCAR=17.9
587 0 my $self = shift;
588 0 ▶ if($self->has_reference) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_reference) 0 0 0
589 0 return $self->reference->audio_samplerate;
590 }
591 0 return $self->_get_audiodata->{sample_rate};
592 }
593  
594 =head2 video_framerate
595  
596 The frame rate of the video, as a fraction.
597  
598 Note that in the weird US frame rate, this could be 30000/1001.
599  
600 =cut
601  
602 16 has 'video_framerate' => (
603 is => 'rw',
604 builder => '_probe_framerate',
605 lazy => 1,
606 );
607  
608 0 ▶sub _probe_framerate {
Subroutine _probe_framerate: not called CC=2 SCAR=17.9
609 0 my $self = shift;
610 0 ▶ if($self->has_reference) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_reference) 0 0 0
611 0 return $self->reference->video_framerate;
612 }
613 0 my $framerate = $self->_get_videodata->{r_frame_rate};
614 0 return $framerate;
615 }
616  
617 =head2 video_frame_length
618  
619 The length of a single video frame in seconds, as a floating point number.
620  
621 Calculated as 1 / (video_framerate numerator / video_framerate denumerator)
622  
623 Probed from the value of video_framerate. Ignored if set.
624  
625 =cut
626  
627 16 has 'video_frame_length' => (
628 is => 'rw',
629 builder => '_probe_frame_length',
630 lazy => 1,
631 );
632  
633 0 ▶sub _probe_frame_length {
Subroutine _probe_frame_length: not called CC=1 SCAR=6.9
634 0 my $self = shift;
635  
636 0 my ($numerator, $denumerator) = split /\//, $self->video_framerate;
637 0 return 1 / ($numerator / $denumerator);
638 }
639  
640 =head2 fragment_start
641  
642 If set, this instructs Media::Convert on read to only read a particular
643 part of the video from this file. Should be specified in seconds; will
644 not be probed.
645  
646 =cut
647  
648 16 has 'fragment_start' => (
649 is => 'rw',
650 predicate => 'has_fragment_start',
651 );
652  
653 =head2 quality
654  
655 The quality used for the video encoding, i.e., the value passed to the C<-crf>
656 parameter. Mostly for use by a profile. Will not be probed.
657  
658 =cut
659  
660 16 has 'quality' => (
661 is => 'rw',
662 builder => '_probe_quality',
663 lazy => 1,
664 );
665  
666 0 ▶sub _probe_quality {
Subroutine _probe_quality: not called CC=2 SCAR=17.9
667 0 my $self = shift;
668 0 ▶ if($self->has_reference) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_reference) 0 0 0
669 0 return $self->reference->quality;
670 }
671 0 return;
672 }
673  
674 =head2 metadata
675  
676 Can be used to set video metadata (as per C<ffmpeg>'s C<-metadata>
677 parameter). Functions C<add_metadata> and C<drop_metadata> can be used
678 to add or remove individual metedata values. Will not be probed.
679  
680 =cut
681  
682 16 has 'metadata' => (
683 traits => ['Hash'],
684 isa => 'HashRef[Str]',
685 is => 'ro',
686 handles => {
687 add_metadata => 'set',
688 drop_metadata => 'delete',
689 },
690 predicate => 'has_metadata',
691 );
692  
693 =head2 reference
694  
695 If set to any C<Media::Convert::Asset> object, then when any value is being
696 probed, rather than trying to run C<ffprobe> on the file pointed to by
697 our C<url> attribute, we will use the value reported by the referenced
698 object.
699  
700 Can be used in "build a file almost like this one, but with these things
701 different" kind of scenarios.
702  
703 Will not be probed (obviously).
704  
705 =cut
706  
707 16 has 'reference' => (
708 isa => 'Media::Convert::Asset',
709 is => 'ro',
710 predicate => 'has_reference',
711 );
712  
713 =head2 pix_fmt
714  
715 The pixel format (e.g., C<yuv420p> or the likes) of the video.
716  
717 =cut
718  
719 16 has 'pix_fmt' => (
720 is => 'rw',
721 builder => '_probe_pix_fmt',
722 lazy => 1,
723 );
724  
725 0 ▶sub _probe_pix_fmt {
Subroutine _probe_pix_fmt: not called CC=2 SCAR=17.9
726 0 my $self = shift;
727 0 ▶ if($self->has_reference) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_reference) 0 0 0
728 0 return $self->reference->pix_fmt;
729 }
730 0 return $self->_get_videodata->{pix_fmt};
731 }
732  
733 =head2 astream_id
734  
735 Returns the numeric ID for the first audio stream in this file. Useful for the
736 implementation of stream mappings etc; see C<Media::Convert::Map>
737  
738 =cut
739  
740 16 has 'astream_id' => (
741 is => 'rw',
742 builder => '_probe_astream_id',
743 lazy => 1,
744 );
745  
746 0 ▶sub _probe_astream_id {
Subroutine _probe_astream_id: not called CC=1 SCAR=6.9
747 0 my $self = shift;
748 0 return $self->_get_audiodata->{index};
749 }
750  
751 =head2 blackspots
752  
753 Returns an array of hashes. Each hash contains a member C<start>,
754 C<end>, and C<duration>, containing the start, end, and duration,
755 respectively, of locations in the video file that are (almost) entirely
756 black.
757  
758 Could be used by a script for automatic review.
759  
760 Note that the ffmpeg run required to detect blackness is CPU intensive
761 and may require a very long time to finish.
762  
763 =cut
764  
765 16 has blackspots => (
766 is => 'ro',
767 isa => 'ArrayRef[HashRef[Num]]',
768 builder => '_probe_blackspots',
769 lazy => 1,
770 );
771  
772 0 ▶sub _probe_blackspots {
Subroutine _probe_blackspots: not called CC=4 SCAR=30.0
773 0 my $self = shift;
774 0 my $blacks = [];
775 0 pipe my $R, my $W;
776 0 ▶ if(fork == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Media::Convert::Asset::fork() == 0) 0 0 0
777 0 open STDERR, ">", "$W";
778 0 open STDOUT, ">", "$W";
779 0 my @cmd = ("ffmpeg", "-threads", "1", "-nostats", "-i", $self->url, "-vf", "blackdetect=d=0:pix_th=.01", "-f", "null", "/dev/null");
780 0 exec @cmd;
781 0 die "exec failed";
782 }
783 0 close $W;
784 0 ▶ while(<$R>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $R)) 0 0 0
785 0 ▶ if(/blackdetect.*black_start:(?<start>[\d\.]+)\sblack_end:(?<end>[\d\.]+)\sblack_duration:(?<duration>[\d\.]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/blackdetect.*black_start:(?<start>[\d\.]+)\sblack_end:(?<end>[\d\.]+)\sblack_duration:(?<duration>[\d\.]+)/u) 0 0 0
786 0 push @$blacks, { %+ };
787 }
788 }
789 0 close($R);
790 0 return $blacks;
791 }
792  
793  
794 =head2 channel_layouts
795  
796 Returns an array of audio channel layouts, as detected by ffprobe.
797  
798 =cut
799  
800 16 has 'channel_layouts' => (
801 is => 'rw',
802 traits => ['Array'],
803 isa => 'ArrayRef[Str]',
804 builder => '_probe_channel_layouts',
805 lazy => 1,
806 );
807  
808 0 ▶sub _probe_channel_layouts {
Subroutine _probe_channel_layouts: not called CC=3 SCAR=24.8
809 0 my $self = shift;
810 0 my $rv = [];
811 0 foreach my $stream(@{$self->_get_probedata->{streams}}) {
812 0 ▶ if($stream->{codec_type} eq "audio") {
Branch: 1 branch
BranchTrueFalseTotal
if ($stream->{"codec_type"} eq "audio") 0 0 0
813 0 push @$rv, $stream->{channel_layout};
814 }
815 }
816 0 return $rv;
817 }
818  
819 =head2 astream_ids
820  
821 Returns an array with the IDs for the audio streams in this file.
822  
823 =head2 astream_count
824  
825 Returns the number of audio streams in this file.
826  
827 =cut
828  
829 16 has 'astream_ids' => (
830 is => 'rw',
831 traits => ['Array'],
832 isa => 'ArrayRef[Int]',
833 builder => '_probe_astream_ids',
834 lazy => 1,
835 handles => {
836 astream_count => "count",
837 },
838 );
839  
840 0 ▶sub _probe_astream_ids {
Subroutine _probe_astream_ids: not called CC=3 SCAR=24.8
841 0 my $self = shift;
842 0 my $rv = [];
843 0 foreach my $stream(@{$self->_get_probedata->{streams}}) {
844 0 ▶ if($stream->{codec_type} eq "audio") {
Branch: 1 branch
BranchTrueFalseTotal
if ($stream->{"codec_type"} eq "audio") 0 0 0
845 0 push @$rv, $stream->{index};
846 }
847 }
848 0 return $rv;
849 }
850  
851 =head2 audio_channel_count
852  
853 Returns the number of channels in the first audio stream
854  
855 =cut
856  
857 16 has 'audio_channel_count' => (
858 is => 'rw',
859 isa => 'Int',
860 lazy => 1,
861 builder => '_probe_audio_channel_count',
862 );
863  
864 0 ▶sub _probe_audio_channel_count {
Subroutine _probe_audio_channel_count: not called CC=2 SCAR=17.9
865 0 my $self = shift;
866 0 ▶ if($self->has_reference) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_reference) 0 0 0
867 0 return $self->reference->audio_channel_count;
868 }
869 0 return $self->_get_audiodata->{channels};
870 }
871  
872 =head2 vstream_id
873  
874 Returns the numeric ID for the first video stream in this file. Useful
875 for the implementation of stream mappings etc; see C<Media::Convert::Map>
876  
877 =cut
878  
879 16 has 'vstream_id' => (
880 is => 'rw',
881 builder => '_probe_vstream_id',
882 lazy => 1,
883 );
884  
885 0 ▶sub _probe_vstream_id {
Subroutine _probe_vstream_id: not called CC=1 SCAR=6.9
886 0 my $self = shift;
887 0 return $self->_get_videodata->{index};
888 }
889  
890 =head2 extra_params
891  
892 Add extra (output) parameters. This should be used sparingly, rather add some
893 abstraction.
894  
895 =head3 handles
896  
897 =over
898  
899 =item *
900  
901 add_param (adds an extra parameter)
902  
903 =item *
904  
905 drop_param (delete a parameter)
906  
907 =back
908  
909 =cut
910  
911 16 has 'extra_params' => (
912 traits => ['Hash'],
913 isa => 'HashRef[Str]',
914 is => 'ro',
915 handles => {
916 add_param => 'set',
917 drop_param => 'delete',
918 },
919 builder => "_probe_extra_params",
920 lazy => 1,
921 );
922  
923 0 ▶sub _probe_extra_params {
Subroutine _probe_extra_params: not called CC=2 SCAR=17.9
924 0 my $self = shift;
925 0 ▶ if($self->has_reference) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_reference) 0 0 0
926 0 return $self->reference->extra_params;
927 }
928 0 return {};
929 }
930  
931 =head2 input_params
932  
933 Add extra input parameters. This should be used sparingly, rather add
934 some abstraction.
935  
936 =head3 handles
937  
938 =over
939  
940 =item *
941  
942 add_input_param (adds an extra parameter)
943  
944 =item *
945  
946 drop_input_param (delete a parameter)
947  
948 =back
949  
950 =cut
951  
952 16 has 'input_params' => (
953 traits => ['Hash'],
954 isa => 'HashRef[Str]',
955 is => 'ro',
956 handles => {
957 add_input_param => 'set',
958 drop_input_param => 'delete',
959 },
960 builder => "_probe_input_params",
961 lazy => 1,
962 );
963  
964 4 ▶sub _probe_input_params {
Subroutine _probe_input_params: called CC=2 SCAR=7.6
965 4 my $self = shift;
966 4 ▶ if($self->has_reference) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_reference) 0 4 4
967 0 return $self->reference->input_params;
968 }
969 4 return {};
970 }
971  
972 =head2 time_offset
973  
974 Apply an input time offset to this video (only valid when used as an
975 input video in L<Media::Convert::Pipe>). Can be used to apply A/V sync
976 correction values.
977  
978 =cut
979  
980 16 has 'time_offset' => (
981 isa => 'Num',
982 is => 'ro',
983 predicate => 'has_time_offset',
984 );
985  
986 # Only to be used by the Pipe class when doing multiple passes
987 16 has 'pass' => (
988 is => 'rw',
989 predicate => 'has_pass',
990 clearer => 'clear_pass',
991 );
992  
993 ## The below exist to help autodetect sizes, and are not meant for the end user
994 16 has 'videodata' => (
995 is => 'bare',
996 reader => '_get_videodata',
997 builder => '_probe_videodata',
998 lazy => 1,
999 );
1000 16 has 'audiodata' => (
1001 is => 'bare',
1002 reader => '_get_audiodata',
1003 builder => '_probe_audiodata',
1004 lazy => 1,
1005 );
1006 16 has 'probedata' => (
1007 is => 'bare',
1008 reader => '_get_probedata',
1009 builder => '_probe',
1010 clearer => 'clear_probedata',
1011 lazy => 1,
1012 );
1013  
1014 4 ▶sub readopts {
Subroutine readopts: called CC=4 SCAR=16.1
Pod undocumented
1015 4 my $self = shift;
1016 4 my @opts = ();
1017  
1018 4 ▶ if($self->has_time_offset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_time_offset) 0 4 4
1019 0 push @opts, ("-itsoffset", $self->time_offset);
1020 }
1021  
1022 4 ▶ if(scalar(keys(%{$self->input_params})) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$self->input_params;} > 0) 0 4 4
1023 0 foreach my $param(keys %{$self->input_params}) {
1024 0 push @opts, ("-$param", $self->input_params->{$param});
1025 }
1026 }
1027  
1028 4 push @opts, ("-i", $self->url);
1029 4 return @opts;
1030 }
1031  
1032 0 ▶sub writeopts {
Subroutine writeopts: not called CC=28 SCAR=67.0
Pod undocumented
1033 0 my $self = shift;
1034 0 my $pipe = shift;
1035 0 my @opts = ();
1036  
1037 0 ▶ if(!$pipe->vcopy && !$pipe->vskip) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pipe->vcopy or $pipe->vskip) 0 0 0
Condition0%0/3
$pipe->vcopy or $pipe->vskip
l!l&&r!l&&!r
000
MC/DC0%0/2
$pipe->vcopy or $pipe->vskip
$pipe->vcopy $pipe->vskip
Truth table0%0/3
A or B
A= $pipe->vcopyB= $pipe->vskip
ABresult
1X1
011
000
1038 0 push @opts, ('-threads', '1');
1039 0 ▶ if(defined($self->video_codec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->video_codec) 0 0 0
1040 0 push @opts, ('-c:v', detect_to_write($self->video_codec));
1041 }
1042 0 ▶ if(defined($self->video_bitrate)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->video_bitrate) 0 0 0
1043 0 push @opts, ('-b:v', $self->video_bitrate . "k", '-minrate', $self->video_minrate . "k", '-maxrate', $self->video_maxrate . "k");
1044 }
1045 0 ▶ if(defined($self->video_framerate)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->video_framerate) 0 0 0
1046 0 push @opts, ('-r:v', $self->video_framerate);
1047 }
1048 0 ▶ if(defined($self->quality)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->quality) 0 0 0
1049 0 push @opts, ('-crf', $self->quality);
1050 }
1051 0 ▶ if(defined($self->speed)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->speed) 0 0 0
1052 0 push @opts, ('-speed', $self->speed);
1053 }
1054 0 ▶ if(defined($self->video_preset)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->video_preset) 0 0 0
1055 0 push @opts, ('-preset', $self->video_preset);
1056 }
1057 0 ▶ if(defined($self->force_key_frames)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->force_key_frames) 0 0 0
1058 0 push @opts, ('-force_key_frames', $self->force_key_frames);
1059 }
1060 0 ▶ if($self->has_pass) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_pass) 0 0 0
1061 0 push @opts, ('-pass', $self->pass, '-passlogfile', $self->url . '-multipass');
1062 }
1063 }
1064 0 ▶ if(!$pipe->acopy && !$pipe->askip) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pipe->acopy or $pipe->askip) 0 0 0
Condition0%0/3
$pipe->acopy or $pipe->askip
l!l&&r!l&&!r
000
MC/DC0%0/2
$pipe->acopy or $pipe->askip
$pipe->acopy $pipe->askip
Truth table0%0/3
A or B
A= $pipe->acopyB= $pipe->askip
ABresult
1X1
011
000
1065 0 ▶ if(defined($self->audio_codec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->audio_codec) 0 0 0
1066 0 push @opts, ('-c:a', detect_to_write($self->audio_codec));
1067 }
1068 0 ▶ if(defined($self->audio_bitrate)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->audio_bitrate) 0 0 0
1069 0 push @opts, ('-b:a', $self->audio_bitrate);
1070 }
1071 0 ▶ if(defined($self->audio_samplerate)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->audio_samplerate) 0 0 0
1072 0 push @opts, ('-ar', $self->audio_samplerate);
1073 }
1074 }
1075 0 ▶ if($self->has_fragment_start) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_fragment_start) 0 0 0
1076 0 push @opts, ('-ss', $self->fragment_start);
1077 }
1078 0 ▶ if(defined($self->duration)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->duration) { } 0 0 0
elsif (defined $self->duration_frames) { } 0 0 0
1079 0 ▶ if($self->duration_style eq 'seconds') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->duration_style eq 'seconds') { } 0 0 0
1080 0 push @opts, ('-t', $self->duration);
1081 } else {
1082 0 push @opts, ('-frames:v', $self->duration);
1083 }
1084 } elsif(defined($self->duration_frames)) {
1085 0 push @opts, ('-frames:v', $self->duration_frames);
1086 }
1087 0 ▶ if(defined($self->pix_fmt)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->pix_fmt) 0 0 0
1088 0 push @opts, ('-pix_fmt', $self->pix_fmt);
1089 }
1090 0 ▶ if($self->has_metadata) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_metadata) 0 0 0
1091 0 foreach my $meta(keys %{$self->metadata}) {
1092 0 push @opts, ('-metadata', $meta . '=' . $self->metadata->{$meta});
1093 }
1094 }
1095  
1096 0 ▶ if(!defined($self->duration) && $#{$pipe->inputs}>0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->duration and $#{$pipe->inputs;} > 0) 0 0 0
Condition0%0/3
not defined $self->duration and $#{$pipe->inputs;} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $self->duration and $#{$pipe->inputs;} > 0
not defined $self->duration $#{$pipe->inputs;} > 0
Truth table0%0/3
not(A) and B
A= not defined $self->durationB= $#{$pipe->inputs;} > 0
ABresult
0X0
100
111
1097 0 push @opts, '-shortest';
1098 }
1099  
1100 0 ▶ if(scalar(keys(%{$self->extra_params}))>0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$self->extra_params;} > 0) 0 0 0
1101 0 foreach my $param(keys %{$self->extra_params}) {
1102 0 push @opts, ("-$param", $self->extra_params->{$param});
1103 }
1104 }
1105  
1106 0 ▶ if(exists($ENV{SREVIEW_NONSTRICT})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{"SREVIEW_NONSTRICT"}) 0 0 0
1107 0 push @opts, ("-strict", "-2");
1108 }
1109  
1110 0 push @opts, $self->url;
1111  
1112 0 return @opts;
1113 }
1114  
1115 16 ▶sub _probe {
Subroutine _probe: called CC=4 SCAR=18.5
1116 16 my $self = shift;
1117  
1118 16 ▶ if($self->has_reference) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_reference) 0 16 16
1119 0 return $self->reference->_get_probedata;
1120 }
1121 16 ▶ if(-e $self->url) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $self->url) 11 5 16
1122 11 open my $jsonpipe, "-|:encoding(UTF-8)", "ffprobe", "-loglevel", "quiet", "-print_format", "json", "-show_format", "-show_streams", $self->url;
1123 0 my $json = "";
1124 0 ▶ while(<$jsonpipe>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $jsonpipe)) 0 0 0
1125 0 $json .= $_;
1126 }
1127 0 close $jsonpipe;
1128 0 return decode_json($json);
1129 }
1130 5 return {};
1131 }
1132  
1133 0 ▶sub _probe_audiodata {
Subroutine _probe_audiodata: not called CC=4 SCAR=30.0
1134 0 my $self = shift;
1135 0 ▶ if(!exists($self->_get_probedata->{streams})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->_get_probedata->{"streams"}) 0 0 0
1136 0 return {};
1137 }
1138 0 foreach my $stream(@{$self->_get_probedata->{streams}}) {
1139 0 ▶ if($stream->{codec_type} eq "audio") {
Branch: 1 branch
BranchTrueFalseTotal
if ($stream->{"codec_type"} eq "audio") 0 0 0
1140 0 return $stream;
1141 }
1142 }
1143 0 return {};
1144 }
1145  
1146 15 ▶sub _probe_videodata {
Subroutine _probe_videodata: called CC=4 SCAR=21.7
1147 15 my $self = shift;
1148 15 ▶ if(!exists($self->_get_probedata->{streams})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->_get_probedata->{"streams"}) 5 0 5
1149 5 return {};
1150 }
1151 0 foreach my $stream(@{$self->_get_probedata->{streams}}) {
1152 0 ▶ if($stream->{codec_type} eq "video") {
Branch: 1 branch
BranchTrueFalseTotal
if ($stream->{"codec_type"} eq "video") 0 0 0
1153 0 return $stream;
1154 }
1155 }
1156 0 return {};
1157 }
1158  
1159 0 ▶sub speed {
Subroutine speed: not called CC=2 SCAR=17.9
Pod undocumented
1160 0 my $self = shift;
1161 0 ▶ if($self->has_reference) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_reference) 0 0 0
1162 0 return $self->reference->speed;
1163 }
1164 0 return 4;
1165 }
1166  
1167 16 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
1168  
1169 16 1;
« blib/lib/Media/Convert/Pipe.pm ↑ Summary blib/lib/Media/Convert/Map.pm »