blib/lib/Media/Convert/Asset/ProfileFactory.pm

filter: Statementstmt 56.3% 53 / 94 Branchbran 16.6% 3 / 18 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 43.3% 13 / 30 Podpod 0.0% 0 / 2 totaltotal 43.4% 69 / 159 CC 13 SCAR 20.1
CC
13
Cov
46%
_probe_videosize
30.0
speed
20.0
_probe_videobitrate
6.0
CRAP 7.4
« blib/lib/Media/Convert/Normalizer/Bs1770gain.pm ↑ Summary blib/lib/Media/Convert/Asset/Profile/copy.pm »
1 =head1 NAME
2  
3 Media::Convert::Asset::ProfileFactory - Create an output profile from an input video.
4  
5 =head1 SYNOPSIS
6  
7 use Media::Convert::Asset;
8 use Media::Convert::Pipe;
9 use Media::Convert::Asset::ProfileFactory;
10  
11 package Media::Convert::Asset::Profile::myprofile;
12 use Moose;
13 extends Media::Convert::Asset::Profile::webm;
14  
15 has '+audio_samplerate' => (
16 builder => '_probe_my_audiorate',
17 );
18  
19 has '+audio_codec' => (
20 default => 'vorbis',
21 );
22  
23 sub _probe_exten {
24 return 'my.webm',
25 }
26  
27 sub _probe_my_audiorate {
28 my $self = shift;
29 return $self->reference->audio_samplerate / 2;
30 }
31  
32 no Moose;
33  
34 package main;
35  
36 my $input = Media::Convert::Asset->new(url => "foo.mp4");
37 my $profile = Media::Convert::Asset::ProfileFactory->create("myprofile", $input);
38 my $output = Media::Convert::Asset->new(url => "foo." . $profile->exten, reference => $profile);
39 Media::Convert::Pipe->new(inputs => [$input], output => $output)->run();
40  
41 # Or using an ad-hoc profile:
42 $profile = Media::Convert::Asset::ProfileFactory->create("extraprofile", $input,
43 {
44 extraprofile => {
45 parent => "webm",
46 settings => { audio_codec => "vorbis", quality => 10 },
47 }
48 });
49 # After this, using the profile happens exactly the same way.
50  
51 =head1 DESCRIPTION
52  
53 C<Media::Convert::Asset::Profile::Base> is a subclass of Media::Convert::Asset,
54 but with a number of the probing methods overridden so that they return values
55 that are not in line with the reference of the given video.
56  
57 The C<Media::Convert::Asset::ProfileFactory>'s C<create> method is a simple
58 helper to:
59  
60 =over
61  
62 =item *
63  
64 ensure that the relevant C<Media::Convert::Asset::Profile::I<profile>> module
65 has been loaded (the module for the C<parent> attribute of the ad-hoc
66 profile, if one is chosen)
67  
68 =item *
69  
70 create a L<Media::Convert::Asset> subclass of the right type, with reference
71 set to the passed input C<Media::Convert::Asset> object.
72  
73 =item *
74  
75 if an ad-hoc profile was selected, ensure that the relevant attributes
76 of the resulting object are set to the values in the C<settings> key of
77 the ad-hoc profile's settings
78  
79 =back
80  
81 =head1 CREATING NEW PROFILES
82  
83 It is possible to create a new profile by extending an existing one. The
84 C<myprofile> profile in the above example demonstrates how to do so. Any
85 property that is known by L<Media::Convert::Asset> can be overridden in the
86 manner given.
87  
88 Alternatively, one can create new profiles in the ad-hoc manner. This is
89 easier, since it only requires an extra parameter be passed to the
90 C<create> function; however, profiles created in this manner can only
91 hardcode values, and cannot vary any parameters based on the input file.
92 To create a profile that can do so, when the new profile just changes a
93 minor detail of an existing profile, extend that profile and change the
94 detail which you want to change. To create a new profile from scratch,
95 extend the C<Base> profile (see below).
96  
97 =head1 PRE-EXISTING PROFILES
98  
99 The following profiles are defined by C<Media::Convert::Asset::ProfileFactory>:
100  
101 =cut
102  
103 package Media::Convert::Asset::Profile::Base; ## no critic(MatchesPackage)
104  
105 =head2 Base
106  
107 This profile serves as a base class for the other profiles. It should
108 not be used directly.
109  
110 It adds the extension, and defaults the pixel format to yuv420p.
111  
112 =cut
113  
114 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
115  
116 5 extends 'Media::Convert::Asset';
117  
118 5 has '+reference' => (
119 required => 1,
120 );
121  
122 5 has 'exten' => (
123 lazy => 1,
124 is => 'ro',
125 builder => '_probe_exten',
126 );
127  
128 5 has '+pix_fmt' => (
129 builder => '_build_pixfmt',
130 );
131  
132 0 ▶sub _build_pixfmt {
Subroutine _build_pixfmt: not called CC=1 SCAR=6.9
133 0 return 'yuv420p';
134 }
135  
136 0 ▶sub _probe_exten {
Subroutine _probe_exten: not called CC=1 SCAR=6.9
137 ...
138 0 }
139  
140 package Media::Convert::Asset::Profile::vp9;
141  
142 =head2 vp9
143  
144 Produces a video in WebM/VP9 format, using the quality/bitrate settings
145 recommended by Google on L<https://developers.google.com/media/vp9/>,
146 and with OPUS audio. Produces files with the C<vp9.webm> extension.
147  
148 Audio settings are hardcoded to 48KHz sampling rate, 128k bits per
149 second.
150  
151 =cut
152  
153 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
154  
155 5 extends 'Media::Convert::Asset::Profile::Base';
156  
157 0 ▶sub _probe_exten {
Subroutine _probe_exten: not called CC=1 SCAR=6.9
158 0 return 'vp9.webm'
159 }
160  
161 5 my %rates_30 = (
162 240 => 150,
163 360 => 276,
164 480 => 750,
165 720 => 1024,
166 1080 => 1800,
167 1440 => 6000,
168 2160 => 12000
169 );
170  
171 5 my %rates_50 = (
172 240 => 150,
173 360 => 276,
174 480 => 750,
175 720 => 1800,
176 1080 => 3000,
177 1440 => 9000,
178 2160 => 18000
179 );
180  
181 5 my %quals = (
182 240 => 37,
183 360 => 36,
184 480 => 33,
185 720 => 32,
186 1080 => 31,
187 1440 => 24,
188 2160 => 15,
189 );
190  
191 0 ▶sub _probe_videobitrate {
Subroutine _probe_videobitrate: not called CC=2 SCAR=17.9
192 0 my $self = shift;
193 0 ▶ if(eval($self->video_framerate) > 30) { ## no critic(StringyEval)
Branch: 1 branch
BranchTrueFalseTotal
if (eval $self->video_framerate > 30) { } 0 0 0
194 0 return $rates_50{$self->video_height};
195 } else {
196 0 return $rates_30{$self->video_height};
197 }
198 }
199  
200 0 ▶sub _probe_audiorate {
Subroutine _probe_audiorate: not called CC=1 SCAR=6.9
201 0 return "48000";
202 }
203  
204 0 ▶sub _probe_audiobitrate {
Subroutine _probe_audiobitrate: not called CC=1 SCAR=6.9
205 0 return "128k";
206 }
207  
208 0 ▶sub _probe_quality {
Subroutine _probe_quality: not called CC=1 SCAR=6.9
209 0 my $self = shift;
210 0 return $quals{$self->video_height};
211 }
212  
213 0 ▶sub speed {
Subroutine speed: not called CC=4 SCAR=30.0
Pod undocumented
214 0 my $self = shift;
215 0 ▶ if($self->reference->has_pass) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->reference->has_pass) 0 0 0
216 0 ▶ if($self->reference->pass == 1 || $self->video_height < 720) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->reference->pass == 1 or $self->video_height < 720) 0 0 0
Condition0%0/3
$self->reference->pass == 1 or $self->video_height < 720
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->reference->pass == 1 or $self->video_height < 720
$self->reference->pass == 1 $self->video_height < 720
Truth table0%0/3
A or B
A= $self->reference->pass == 1B= $self->video_height < 720
ABresult
1X1
011
000
217 0 return 4;
218 }
219 0 return 2;
220 }
221 0 return 4;
222 }
223  
224 1 ▶sub _probe_videocodec {
Subroutine _probe_videocodec: called CC=1 SCAR=0.0
225 1 return "vp9";
226 }
227  
228 0 ▶sub _probe_audiocodec {
Subroutine _probe_audiocodec: not called CC=1 SCAR=6.9
229 0 return "opus";
230 }
231  
232 5 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
233  
234 package Media::Convert::Asset::Profile::vp8;
235  
236 =head2 vp8
237  
238 Produces a video in WebM/VP8 format. Since no similar recommendations
239 for VP8 exist as do for VP9, no explicit quality or bitrate settings are
240 configured in this profile. The libvpx video codec is selected, and the
241 libvorbis one for audio.
242  
243 The audio bitrate is explicitly left to ffmpeg defaults; the extension
244 is set to C<vp8.webm>
245  
246 =cut
247  
248 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
249  
250 5 extends 'Media::Convert::Asset::Profile::Base';
251  
252 0 ▶sub _probe_exten {
Subroutine _probe_exten: not called CC=1 SCAR=6.9
253 0 return 'vp8.webm';
254 }
255  
256 0 ▶sub _probe_videocodec {
Subroutine _probe_videocodec: not called CC=1 SCAR=6.9
257 0 return "vp8";
258 }
259  
260 0 ▶sub _probe_audiocodec {
Subroutine _probe_audiocodec: not called CC=1 SCAR=6.9
261 0 return "vorbis";
262 }
263  
264 0 ▶sub _probe_audiobitrate {
Subroutine _probe_audiobitrate: not called CC=1 SCAR=6.9
265 0 return;
266 }
267  
268 5 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
269  
270 package Media::Convert::Asset::Profile::webm;
271  
272 =head2 webm
273  
274 This profile subclasses from the C<vp9> profile, and only changes the
275 extension to plain C<webm> instead of C<vp9.webm>.
276  
277 Note that future versions of this class may switch to subclassing from
278 the AV1 profile rather than the VP9 profile, although this will not
279 happen before AV1 encoding can be done at reasonable speeds by
280 Media::Convert.
281  
282 =cut
283  
284 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
285  
286 5 extends 'Media::Convert::Asset::Profile::vp9';
287  
288 1 ▶sub _probe_exten {
Subroutine _probe_exten: called CC=1 SCAR=0.0
289 1 return 'webm',
290 }
291  
292 5 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
293  
294 package Media::Convert::Asset::Profile::vp8_lq;
295  
296 =head2 vp8_lq
297  
298 This profile subclasses from the C<vp8> profile. The extension is set to
299 C<lq.webm>. In addition to the changes made by the C<vp8> profile, this
300 profile also rescales the video to a fraction of the original; that is,
301 the height and width of the video are both divided by 4.
302  
303 =cut
304  
305 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
306  
307 5 extends 'Media::Convert::Asset::Profile::vp8';
308  
309 0 ▶sub _probe_exten {
Subroutine _probe_exten: not called CC=1 SCAR=6.9
310 0 return 'lq.webm',
311 }
312  
313 0 ▶sub _probe_height {
Subroutine _probe_height: not called CC=2 SCAR=17.9
314 0 my $self = shift;
315 0 ▶ return unless defined ($self->reference->video_height);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->reference->video_height 0 0 0
316 0 return int($self->reference->video_height / 4);
317 }
318  
319 0 ▶sub _probe_width {
Subroutine _probe_width: not called CC=2 SCAR=17.9
320 0 my $self = shift;
321 0 ▶ return unless defined ($self->reference->video_width);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->reference->video_width 0 0 0
322 0 return int($self->reference->video_width / 4);
323 }
324  
325 0 ▶sub _probe_videosize {
Subroutine _probe_videosize: not called CC=5 SCAR=34.0
326 0 my $self = shift;
327 0 my $width = $self->video_width;
328 0 my $height = $self->video_height;
329 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
330 0 ▶ return unless $width && $height;
Branch: 1 branch
BranchTrueFalseTotal
unless $width and $height 0 0 0
Condition0%0/3
$width and $height
!ll&&!rl&&r
000
MC/DC0%0/2
$width and $height
$width $height
Truth table0%0/3
A and B
A= $widthB= $height
ABresult
0X0
100
111
331 0 return $self->video_width . "x" . $self->video_height;
332 }
333  
334 5 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
335  
336 package Media::Convert::Asset::ProfileFactory;
337  
338 5 ▶use Module::Runtime qw/require_module/;
Subroutine BEGIN: called CC=1 SCAR=0.0
339  
340 4 ▶sub create {
Subroutine create: called CC=3 SCAR=12.4
Pod undocumented
341 4 my $class = shift;
342 4 my $profile = shift;
343 4 my $ref = shift;
344 4 my $profiles = shift;
345  
346 4 ▶ if(!defined($profiles)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $profiles) 3 1 4
347 3 $profiles = {};
348 }
349  
350 4 ▶ if(!exists($profiles->{$profile})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $profiles->{$profile}) { } 4 0 4
351 4 require_module("Media::Convert::Asset::Profile::$profile");
352 4 return "Media::Convert::Asset::Profile::$profile"->new(url => '', reference => $ref);
353 } else {
354 0 my $parent = $profiles->{$profile}{parent};
355 0 require_module("Media::Convert::Asset::Profile::$parent");
356 0 my $rv = "Media::Convert::Asset::Profile::$parent"->new(url => '', reference => $ref, %{$profiles->{$profile}{settings}});
357 0 return $rv;
358 }
359 0 die "Unknown profile $profile requested!";
360 }
361  
362 5 1;
363  
364 =head1 SEE ALSO
365  
366 L<Media::Convert::Asset>, L<Media::Convert::Profile::mp4>,
367 L<Media::Convert::Profile::mpeg2>, L<Media::Convert::Profile::copy>
« blib/lib/Media/Convert/Normalizer/Bs1770gain.pm ↑ Summary blib/lib/Media/Convert/Asset/Profile/copy.pm »