blib/lib/Media/Info/Mediainfo.pm

filter: Statementstmt 51.6% 31 / 60 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 80.0% 8 / 10 Podpod 100% 1 / 1 totaltotal 29.2% 40 / 137 CC 27 SCAR 62.4
CC
27
Cov
27%
get_media_info
683.2
__ANON__
2.0
BEGIN
1.0
CRAP 512.6
↑ Summary
1 package Media::Info::Mediainfo;
2  
3 1 our $DATE = '2019-01-28'; # DATE
4 1 our $VERSION = '0.004'; # VERSION
5  
6 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Capture::Tiny qw(capture);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use IPC::System::Options 'system', -log=>1;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Perinci::Sub::Util qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 require Exporter;
16 1 our @ISA = qw(Exporter);
17 1 our @EXPORT_OK = qw(
18 get_media_info
19 );
20  
21 1 our %SPEC;
22  
23 $SPEC{get_media_info} = {
24 1 v => 1.1,
25 summary => 'Return information on media file/URL, using the `mediainfo` program',
26 args => {
27 media => {
28 summary => 'Media file',
29 schema => 'str*',
30 pos => 0,
31 req => 1,
32 },
33 },
34 deps => {
35 prog => 'mediainfo',
36 },
37 };
38 0 ▶sub get_media_info {
Subroutine get_media_info: not called CC=27 SCAR=65.3
Pod documented
39 0 require File::Which;
40 1 ▶ no warnings 'numeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 0 my %args = @_;
43  
44 0 ▶ File::Which::which("mediainfo")
Branch: 1 branch
BranchTrueFalseTotal
File::Which::which("mediainfo") or return err(412, "Can't find mediainfo in PATH") 0 0 0
45 or return err(412, "Can't find mediainfo in PATH");
46 0 ▶ my $media = $args{media} or return err(400, "Please specify media");
Branch: 1 branch
BranchTrueFalseTotal
my $media = $args{"media"} or return err(400, "Please specify media") 0 0 0
47  
48 # make sure user can't sneak in cmdline options to ffmpeg
49 0 ▶ $media = "./$media" if $media =~ /\A-/;
Branch: 1 branch
BranchTrueFalseTotal
if $media =~ /\A-/ 0 0 0
50  
51 0 ▶ my ($stdout, $stderr, $exit) = capture {
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 0 local $ENV{LANG} = "C";
53 0 system("mediainfo", "--Language=raw", $media);
54 0 };
55  
56 0 ▶ return err(500, "Can't execute mediainfo successfully ($exit)") if $exit;
Branch: 1 branch
BranchTrueFalseTotal
if $exit 0 0 0
57  
58 0 my $info = {};
59 0 my $cur_section;
60 0 for my $line (split /^/, $stdout) {
61 0 ▶ next unless $line =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\S/ 0 0 0
62 0 chomp $line;
63 0 ▶ unless ($line =~ /:/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /:/) 0 0 0
64 0 $cur_section = $line;
65 0 next;
66 }
67 0 ▶ my ($key, $val) = $line =~ /(\S+.*?)\s*:\s*(\S.*)/ or next;
Branch: 1 branch
BranchTrueFalseTotal
() = $line =~ /(\S+.*?)\s*:\s*(\S.*)/ or next 0 0 0
68 #say "D:section=<$cur_section> key=<$key> val=<$val>";
69 0 ▶ if ($cur_section eq 'General') {
Branch: 3 branches
BranchTrueFalseTotal
if ($cur_section eq 'General') { } 0 0 0
elsif ($cur_section eq 'Video') { } 0 0 0
elsif ($cur_section =~ /^Audio/) { } 0 0 0
70 0 ▶ $info->{duration} = $1 * 3600 + $2 * 60 + $3 if $key eq 'DURATION' && $val =~ /(\d+):(\d+):(\d+\.\d+)/;
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "DURATION" and $val =~ /(\d+):(\d+):(\d+\.\d+)/ 0 0 0
Condition0%0/3
$key eq "DURATION" and $val =~ /(\d+):(\d+):(\d+\.\d+)/
!ll&&!rl&&r
000
MC/DC0%0/2
$key eq "DURATION" and $val =~ /(\d+):(\d+):(\d+\.\d+)/
$key eq "DURATION" $val =~ /(\d+):(\d+):(\d+\.\d+)/
Truth table0%0/3
A and B
A= $key eq "DURATION"B= $val =~ /(\d+):(\d+):(\d+\.\d+)/
ABresult
0X0
100
111
71 } elsif ($cur_section eq 'Video') {
72 0 ▶ $info->{video_format} = $val if $key eq 'Format';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "Format" 0 0 0
73 0 ▶ $info->{video_width} = $val+0 if $key eq 'Width/String';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "Width/String" 0 0 0
74 0 ▶ $info->{video_height} = $val+0 if $key eq 'Height/String';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "Height/String" 0 0 0
75 0 ▶ $info->{video_aspect} = $1/$2 if $key eq 'DisplayAspectRatio/String' && $val =~ /(\d+):(\d+)/;
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "DisplayAspectRatio/String" and $val =~ /(\d+):(\d+)/ 0 0 0
Condition0%0/3
$key eq "DisplayAspectRatio/String" and $val =~ /(\d+):(\d+)/
!ll&&!rl&&r
000
MC/DC0%0/2
$key eq "DisplayAspectRatio/String" and $val =~ /(\d+):(\d+)/
$key eq "DisplayAspectRatio/String" $val =~ /(\d+):(\d+)/
Truth table0%0/3
A and B
A= $key eq "DisplayAspectRatio/String"B= $val =~ /(\d+):(\d+)/
ABresult
0X0
100
111
76 0 ▶ $info->{video_fps} = $val+0 if $key eq 'FrameRate/String';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "FrameRate/String" 0 0 0
77 0 ▶ $info->{rotate} = $1 if $key eq 'Rotation/String' && $val =~ /(\d+)/;
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "Rotation/String" and $val =~ /(\d+)/ 0 0 0
Condition0%0/3
$key eq "Rotation/String" and $val =~ /(\d+)/
!ll&&!rl&&r
000
MC/DC0%0/2
$key eq "Rotation/String" and $val =~ /(\d+)/
$key eq "Rotation/String" $val =~ /(\d+)/
Truth table0%0/3
A and B
A= $key eq "Rotation/String"B= $val =~ /(\d+)/
ABresult
0X0
100
111
78 # XXX video_bitrate
79 } elsif ($cur_section =~ /^Audio/) {
80 # XXX handle multiple audio streams
81 0 ▶ $info->{audio_format} //= $val if $key eq 'Format';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "Format" 0 0 0
Condition0%0/3
$info->{"audio_format"} //= $val
l!l&&r!l&&!r
000
MC/DC0%0/2
$info->{"audio_format"} //= $val
$info->{"audio_format"} $val
Truth table0%0/3
A //= B
A= $info->{"audio_format"}B= $val
ABresult
1X1
011
000
82 0 ▶ $info->{audio_rate} //= $1*1000 if $key eq 'SamplingRate/String' && $val =~ /(\d+(?:\.\d+)?) KHz/;
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "SamplingRate/String" and $val =~ /(\d+(?:\.\d+)?) KHz/ 0 0 0
Condition0%0/5
$key eq "SamplingRate/String" and $val =~ /(\d+(?:\.\d+)?) KHz/
!ll&&!rl&&r
000
$info->{"audio_rate"} //= $1 * 1000
l!l
00
MC/DC0%0/2
$key eq "SamplingRate/String" and $val =~ /(\d+(?:\.\d+)?) KHz/
$key eq "SamplingRate/String" $val =~ /(\d+(?:\.\d+)?) KHz/
MC/DC0%0/1
$info->{"audio_rate"} //= $1 * 1000
$info->{"audio_rate"}
Truth table0%0/3
A and B
A= $key eq "SamplingRate/String"B= $val =~ /(\d+(?:\.\d+)?) KHz/
ABresult
0X0
100
111
Truth table0%0/2
A
A= $info->{"audio_rate"}
Aresult
11
00
83 # XXX audio_bitrate
84 }
85 }
86  
87 0 [200, "OK", $info, {"func.raw_output"=>$stdout}];
88 }
89  
90 1 1;
91 # ABSTRACT: Return information on media file/URL, using the `mediainfo` program
92  
93 __END__
↑ Summary