blib/lib/Media/Info/Ffmpeg.pm

filter: Statementstmt 44.7% 30 / 67 Branchbran 0.0% 0 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 77.7% 7 / 9 Podpod 100% 1 / 1 totaltotal 34.2% 38 / 111 CC 18 SCAR 54.7
CC
18
Cov
30%
get_media_info
342.0
__ANON__
2.0
BEGIN
1.0
CRAP 237.1
↑ Summary
1 package Media::Info::Ffmpeg;
2  
3 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4 1 our $DATE = '2020-06-02'; # DATE
5 1 our $DIST = 'Media-Info-Ffmpeg'; # DIST
6 1 our $VERSION = '0.008'; # VERSION
7  
8 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use Capture::Tiny qw(capture);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use IPC::System::Options 'system', -log=>1;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Perinci::Sub::Util qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 require Exporter;
18 1 our @ISA = qw(Exporter);
19 1 our @EXPORT_OK = qw(
20 get_media_info
21 );
22  
23 1 our %SPEC;
24  
25 $SPEC{get_media_info} = {
26 1 v => 1.1,
27 summary => 'Return information on media file/URL, using ffmpeg',
28 args => {
29 media => {
30 summary => 'Media file',
31 schema => 'str*',
32 pos => 0,
33 req => 1,
34 },
35 },
36 deps => {
37 prog => 'ffmpeg',
38 },
39 };
40 0 ▶sub get_media_info {
Subroutine get_media_info: not called CC=18 SCAR=58.3
Pod documented
41 0 require File::Which;
42  
43 0 my %args = @_;
44  
45 0 ▶ File::Which::which("ffmpeg")
Branch: 1 branch
BranchTrueFalseTotal
File::Which::which("ffmpeg") or return err(412, "Can't find ffmpeg in PATH") 0 0 0
46 or return err(412, "Can't find ffmpeg in PATH");
47 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
48  
49 # make sure user can't sneak in cmdline options to ffmpeg
50 0 ▶ $media = "./$media" if $media =~ /\A-/;
Branch: 1 branch
BranchTrueFalseTotal
if $media =~ /\A-/ 0 0 0
51  
52 0 ▶ my ($stdout, $stderr, $exit) = capture {
Subroutine __ANON__: not called CC=1 SCAR=6.9
53 0 local $ENV{LANG} = "C";
54 0 system("ffmpeg", "-i", $media); # ffprobe produces the same output
55 0 };
56  
57 0 ▶ return err(500, "ffmpeg doesn't show information")
Branch: 1 branch
BranchTrueFalseTotal
unless $stderr =~ /^Input \#0/m 0 0 0
58 unless $stderr =~ /^Input \#0/m;
59  
60 0 my $info = {};
61 0 ▶ $info->{duration} = $1*3600+$2*60+$3 if $stderr =~ /^\s*Duration: (\d+):(\d+):(\d+\.\d+)/m;
Branch: 1 branch
BranchTrueFalseTotal
if $stderr =~ /^\s*Duration: (\d+):(\d+):(\d+\.\d+)/m 0 0 0
62 0 ▶ $info->{rotate} = $1 if $stderr =~ /^\s*rotate\s*:\s*(.+)/m;
Branch: 1 branch
BranchTrueFalseTotal
if $stderr =~ /^\s*rotate\s*:\s*(.+)/m 0 0 0
63  
64 # XXX what about multiple video streams info?
65 0 ▶ if ($stderr =~ /^\s*Stream.+?: Video: (.+)/m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stderr =~ /^\s*Stream.+?: Video: (.+)/m) 0 0 0
66 0 my $video_info = $1;
67 0 $video_info =~ /^(\w+)/; $info->{video_format} = uc($1);
68 0 ▶ $video_info =~ /([1-9]\d*)x(\d+)/ and do {
Branch: 1 branch
BranchTrueFalseTotal
$video_info =~ /([1-9]\d*)x(\d+)/ and do { $info->{"video_width"} = $1; $info->{"video_height"} = $2 } 0 0 0
69 0 $info->{video_width} = $1;
70 0 $info->{video_height} = $2;
71 };
72 0 ▶ $video_info =~ /DAR ((\d+):(\d+))/ and do {
Branch: 1 branch
BranchTrueFalseTotal
$video_info =~ /DAR ((\d+):(\d+))/ and do { $info->{"video_dar"} = $1; if ($2 < $3) { $info->{"video_orientation"} = "portrait"; if ($info->{"video_width"} > $info->{"video_height"}) { ($info->{"video_width"}, $info->{"video_height"}) = ($info->{"video_height"}, $info->{"video_width"}); } ; } else { $info->{"video_orientation"} = "landscape"; } } 0 0 0
73 0 $info->{video_dar} = $1;
74 # portrait, adjust width & height to reflect this
75 0 ▶ if ($2 < $3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($2 < $3) { } 0 0 0
76 0 $info->{video_orientation} = 'portrait';
77 0 ▶ if ($info->{video_width} > $info->{video_height}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"video_width"} > $info->{"video_height"}) 0 0 0
78 ($info->{video_width}, $info->{video_height}) =
79 0 ($info->{video_height}, $info->{video_width});
80 }
81 } else {
82 0 $info->{video_orientation} = 'landscape';
83 }
84 };
85 0 ▶ $video_info =~ /SAR ((\d+):(\d+))/ and do {
Branch: 1 branch
BranchTrueFalseTotal
$video_info =~ /SAR ((\d+):(\d+))/ and do { $info->{"video_sar"} = $1 } 0 0 0
86 0 $info->{video_sar} = $1;
87 };
88  
89 0 ▶ $video_info =~ /(\d+(?:\.\d+)?) fps/ and $info->{video_fps} = $1;
Branch: 1 branch
BranchTrueFalseTotal
$video_info =~ /(\d+(?:\.\d+)?) fps/ and $info->{"video_fps"} = $1 0 0 0
90 0 ▶ $video_info =~ m!(\d+(?:\.\d+)?) kb/s! and $info->{video_bitrate} = $1*1024;
Branch: 1 branch
BranchTrueFalseTotal
$video_info =~ m[(\d+(?:\.\d+)?) kb/s] and $info->{"video_bitrate"} = $1 * 1024 0 0 0
91 }
92  
93 # XXX what about multiple audio streams info?
94 0 ▶ if ($stderr =~ /\s*Stream.+?: Audio: (.+)/m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stderr =~ /\s*Stream.+?: Audio: (.+)/m) 0 0 0
95 0 my $audio_info = $1;
96 0 $audio_info =~ /^(\w+)/; $info->{audio_format} = uc($1);
97 0 ▶ $audio_info =~ /(\d+(?:\.\d+)?) Hz/ and $info->{audio_rate} = $1;
Branch: 1 branch
BranchTrueFalseTotal
$audio_info =~ /(\d+(?:\.\d+)?) Hz/ and $info->{"audio_rate"} = $1 0 0 0
98 0 ▶ $audio_info =~ m!(\d+(?:\.\d+)?) kb/s! and $info->{audio_bitrate} = $1*1024;
Branch: 1 branch
BranchTrueFalseTotal
$audio_info =~ m[(\d+(?:\.\d+)?) kb/s] and $info->{"audio_bitrate"} = $1 * 1024 0 0 0
99 }
100  
101 0 [200, "OK", $info, {"func.raw_output"=>$stderr}];
102 }
103  
104 1 1;
105 # ABSTRACT: Return information on media file/URL, using ffmpeg
106  
107 __END__
↑ Summary