blib/lib/Media/Info.pm

filter: Statementstmt 30.9% 22 / 71 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 62.5% 5 / 8 Podpod 100% 1 / 1 totaltotal 21.3% 28 / 131 CC 22 SCAR 54.2
CC
22
Cov
19%
get_media_info
341.4
_type_from_name
20.0
__ANON__
2.0
CRAP 226.6
↑ Summary
1 package Media::Info;
2  
3 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
10 1 our $DATE = '2024-12-21'; # DATE
11 1 our $DIST = 'Media-Info'; # DIST
12 1 our $VERSION = '0.134'; # VERSION
13  
14 1 our @EXPORT_OK = qw(
15 get_media_info
16 );
17  
18 1 our %SPEC;
19  
20 0 ▶sub _type_from_name {
Subroutine _type_from_name: not called CC=4 SCAR=30.0
21 0 require Filename::Type::Audio;
22 0 require Filename::Type::Video;
23 0 require Filename::Type::Image;
24 0 my $name = shift;
25  
26 0 ▶ Filename::Type::Video::check_video_filename(filename => $name) ? "video" :
Branch: 3 branches
BranchTrueFalseTotal
Filename::Type::Video::check_video_filename('filename', $name) ? : 0 0 0
Filename::Type::Audio::check_audio_filename('filename', $name) ? : 0 0 0
Filename::Type::Image::check_image_filename('filename', $name) ? : 0 0 0
27 Filename::Type::Audio::check_audio_filename(filename => $name) ? "audio" :
28 Filename::Type::Image::check_image_filename(filename => $name) ? "image" : "unknown";
29 }
30  
31 $SPEC{get_media_info} = {
32 v => 1.1,
33 summary => 'Return information on media file/URL',
34 args => {
35 media => {
36 summary => 'Media file/URL',
37 description => <<'MARKDOWN',
38  
39 Note that not every backend can retrieve URL. At the time of this writing, only
40 the Mplayer backend can.
41  
42 Many fields will depend on the backend used. Common fields returned include:
43  
44 * `backend`: the `Media::Info::*` backend module used, e.g. `Ffmpeg`.
45 * `type_from_name`: either `image`, `audio`, `video`, or `unknown`. This
46 is determined from filename (extension).
47  
48 MARKDOWN
49 schema => 'str*',
50 pos => 0,
51 req => 1,
52 },
53 backend => {
54 summary => "Choose specific backend",
55 schema => ['str*', match => '\A\w+\z'],
56 0 ▶ completion => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
57 0 require Complete::Module;
58  
59 0 my %args = @_;
60 Complete::Module::complete_module(
61 word => $args{word},
62 0 ns_prefix => 'Media::Info',
63 );
64 },
65 },
66 },
67 1 };
68 0 ▶sub get_media_info {
Subroutine get_media_info: not called CC=19 SCAR=58.3
Pod documented
69 1 ▶ no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict
Subroutine BEGIN: called CC=1 SCAR=0.0
70  
71 0 my %args = @_;
72  
73 0 my @backends;
74 0 ▶ if ($args{backend}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'backend'}) { } 0 0 0
75 0 @backends = ($args{backend});
76 } else {
77 0 @backends = qw(Ffmpeg Mplayer Mediainfo);
78 }
79  
80 # try the first backend that succeeds
81 0 for my $backend (@backends) {
82 0 my $mod;
83 my $mod_pm;
84 0 ▶ if ($backend =~ /\A\w+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($backend =~ /\A\w+\z/) { } 0 0 0
85 0 $mod = "Media::Info::$backend";
86 0 $mod_pm = "Media/Info/$backend.pm";
87 } else {
88 0 return [400, "Invalid backend '$backend'"];
89 }
90 0 ▶ next unless eval { require $mod_pm; 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { require $mod_pm; 1 } } 0 0 0
91 0 my $func = \&{"$mod\::get_media_info"};
92 0 my $res = $func->(media => $args{media});
93 0 ▶ if ($res->[0] == 200) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->[0] == 200) 0 0 0
94 # add some common fields
95  
96 # backend
97 0 $res->[3]{'func.backend'} = $backend;
98 0 $res->[2]{backend} = $backend;
99 0 $res->[2]{type_from_name} = _type_from_name($args{media});
100  
101 # mtime, ctime, filesize
102 0 my @st = stat $args{media};
103 0 $res->[2]{mtime} = $st[9];
104 0 $res->[2]{ctime} = $st[10];
105 0 $res->[2]{filesize} = $st[7];
106  
107 # video_longest_side, video_shortest_side, video_orientation (if not set by backend)
108 0 ▶ if ($res->[2]{video_height} && $res->[2]{video_width}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->[2]{"video_height"} and $res->[2]{"video_width"}) 0 0 0
Condition0%0/3
$res->[2]{"video_height"} and $res->[2]{"video_width"}
!ll&&!rl&&r
000
MC/DC0%0/2
$res->[2]{"video_height"} and $res->[2]{"video_width"}
$res->[2]{"video_height"} $res->[2]{"video_width"}
Truth table0%0/3
A and B
A= $res->[2]{"video_height"}B= $res->[2]{"video_width"}
ABresult
0X0
100
111
109 0 ▶ if ($res->[2]{video_height} > $res->[2]{video_width}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->[2]{'video_height'} > $res->[2]{'video_width'}) { } 0 0 0
110 0 $res->[2]{video_longest_side} = $res->[2]{video_height};
111 0 $res->[2]{video_shortest_side} = $res->[2]{video_width};
112 0 ▶ unless ($res->[2]{video_orientation}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->[2]{"video_orientation"}) 0 0 0
113 0 ▶ my $rotate = $res->[2]{rotate} // '';
Condition0%0/2
$res->[2]{"rotate"} // ""
l!l
00
MC/DC0%0/1
$res->[2]{"rotate"} // ""
$res->[2]{"rotate"}
Truth table0%0/2
A
A= $res->[2]{"rotate"}
Aresult
11
00
114 0 ▶ $res->[2]{video_orientation} = $rotate eq '90' || $rotate eq '270' ? 'landscape' : 'portrait';
Branch: 1 branch
BranchTrueFalseTotal
$rotate eq '90' || $rotate eq '270' ? : 0 0 0
Condition0%0/3
$rotate eq 90 or $rotate eq 270
l!l&&r!l&&!r
000
MC/DC0%0/2
$rotate eq 90 or $rotate eq 270
$rotate eq 90 $rotate eq 270
Truth table0%0/3
A or B
A= $rotate eq 90B= $rotate eq 270
ABresult
1X1
011
000
115 }
116 } else {
117 0 $res->[2]{video_longest_side} = $res->[2]{video_width};
118 0 $res->[2]{video_shortest_side} = $res->[2]{video_height};
119 0 ▶ unless ($res->[2]{video_orientation}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->[2]{"video_orientation"}) 0 0 0
120 0 ▶ my $rotate = $res->[2]{rotate} // '';
Condition0%0/2
$res->[2]{"rotate"} // ""
l!l
00
MC/DC0%0/1
$res->[2]{"rotate"} // ""
$res->[2]{"rotate"}
Truth table0%0/2
A
A= $res->[2]{"rotate"}
Aresult
11
00
121 0 ▶ $res->[2]{video_orientation} = $rotate eq '90' || $rotate eq '270' ? 'portrait' : 'landscape';
Branch: 1 branch
BranchTrueFalseTotal
$rotate eq '90' || $rotate eq '270' ? : 0 0 0
Condition0%0/3
$rotate eq 90 or $rotate eq 270
l!l&&r!l&&!r
000
MC/DC0%0/2
$rotate eq 90 or $rotate eq 270
$rotate eq 90 $rotate eq 270
Truth table0%0/3
A or B
A= $rotate eq 90B= $rotate eq 270
ABresult
1X1
011
000
122 }
123 }
124 }
125 }
126 0 ▶ return $res unless $res->[0] == 412;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 412 0 0 0
127 }
128  
129 0 ▶ if ($args{backend}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'backend'}) { } 0 0 0
130 0 return [412, "Backend '$args{backend}' not available, please install it first"];
131 } else {
132 0 return [412, "No Media::Info::* backends available, please install one"];
133 }
134 }
135  
136 1 1;
137 # ABSTRACT: Return information on media file/URL
138  
139 __END__
↑ Summary