File Coverage

blib/lib/Mediainfo.pm
Criterion Covered Total %
statement 12 155 7.7
branch 0 108 0.0
condition 0 47 0.0
subroutine 4 6 66.6
pod 0 2 0.0
total 16 318 5.0


line stmt bran cond sub pod time code
1             package Mediainfo;
2 1     1   16648 use strict;
  1         2  
  1         45  
3 1     1   5 use warnings;
  1         3  
  1         31  
4 1     1   703 use IPC::Open3;
  1         3285  
  1         192  
5              
6             our $VERSION = '0.12';
7              
8             sub new
9             {
10 0     0 0   my $pkg = shift;
11 0           my $self = {@_};
12 0           bless $self, $pkg;
13              
14 0           $self->mediainfo($self->{filename});
15 0           return $self;
16             }
17              
18             sub mediainfo
19             {
20 0     0 0   my $self = shift;
21 0   0       my $file = shift || return undef;
22              
23 0           my @PATH = split /:/, $ENV{PATH};
24 0           push @PATH, "./";
25 0           push @PATH, "/bin";
26 0           push @PATH, "/sbin";
27 0           push @PATH, "/usr/bin";
28 0           push @PATH, "/usr/sbin";
29 0           push @PATH, "/usr/local/bin";
30 0           push @PATH, "/usr/local/sbin";
31 0           my $mediainfo_exec;
32              
33 0           foreach (@PATH)
34             {
35 0           my $executable = $_ . '/mediainfo';
36 0 0         if (-s $executable)
37             {
38 0           $mediainfo_exec = $executable;
39 0           last;
40             }
41             }
42              
43 0           my $filesize = -s $file;
44 0           my ($wtr, $rdr, $err, $mediainfo, $mediainfo_err);
45 1     1   5 use Symbol 'gensym';
  1         2  
  1         1516  
46 0           $err = gensym;
47 0           my $pid = open3($wtr, $rdr, $err, "$mediainfo_exec -f \"$file\"");
48 0           close $wtr;
49 0           while (<$rdr>) { $mediainfo .= $_; }
  0            
50 0           while (<$err>) { $mediainfo_err .= $_; }
  0            
51 0           waitpid($pid, 0);
52              
53 0 0         $mediainfo =~ s/\r//g if $mediainfo;
54 0           my ($genernal_info) = $mediainfo =~ /(^General\n.*?\n\n)/sm;
55 0 0         return undef unless $genernal_info;
56              
57 0           my ($video_info) = $mediainfo =~ /(^Video[\s\#\d]*\n.*?\n\n)/sm;
58 0           my ($audio_info) = $mediainfo =~ /(^Audio[\s\#\d]*\n.*?\n\n)/sm;
59              
60 0           my $container;
61             my $length;
62 0           my $bitrate;
63 0           my $title;
64 0           my $album;
65 0           my $track_name;
66 0           my $performer;
67 0           ($container) = $genernal_info =~ /Format\s*:\s*([\w\_\-\\\/\. ]+)\n/;
68 0 0         $container =~ s/\s//g if $container;
69 0           ($length) = $genernal_info =~ /Duration\s*:\s*(\d+)\.?\d*\n/;
70 0           ($bitrate) = $genernal_info =~ /Overall bit rate\s*:\s*(\d+)\n/;
71 0           ($title) = $genernal_info =~ /Title\s*:\s*(.+)\n/;
72 0           ($album) = $genernal_info =~ /Album\s*:\s*(.+)\n/;
73 0           ($track_name) = $genernal_info =~ /Track name\s*:\s*(.+)\n/;
74 0           ($performer) = $genernal_info =~ /Performer\s*:\s*(.+)\n/;
75              
76 0           my $video_codec;
77             my $video_codec_profile;
78 0           my $video_format;
79 0           my $video_format_profile;
80 0           my $video_length;
81 0           my $video_bitrate;
82 0           my $width;
83 0           my $height;
84 0           my $fps;
85 0           my $frame_count;
86 0           my $fps_mode;
87 0           my $dar;
88 0           my $rotation;
89              
90 0 0         if ($video_info)
91             {
92 0           ($video_codec) = $video_info =~ /Codec\s*:\s*([\w\_\-\\\/ ]+)\n/;
93 0           ($video_format) = $video_info =~ /Format\s*:\s*([\w\_\-\\\/ ]+)\n/;
94 0           ($video_codec_profile) =
95             $video_info =~ /Codec profile\s*:\s*([\w\_\-\\\/\@\. ]+)\n/;
96 0           ($video_format_profile) =
97             $video_info =~ /Format profile\s*:\s*([\w\_\-\\\/\@\. ]+)\n/;
98 0 0         $video_codec =~ s/\s//g if $video_codec;
99 0 0         $video_format =~ s/\s//g if $video_format;
100 0 0         $video_codec_profile =~ s/\s//g if $video_codec_profile;
101 0 0         $video_format_profile =~ s/\s//g if $video_format_profile;
102 0           ($video_length) = $video_info =~ /Duration\s*:\s*(\d+)\.?\d*\n/;
103 0           ($video_bitrate) = $video_info =~ /Bit rate\s*:\s*(\d+)\n/;
104 0           ($width) = $video_info =~ /Original width\s*:\s*(\d+)\n/;
105 0 0         ($width) = $video_info =~ /Width\s*:\s*(\d+)\n/ unless $width;
106 0           ($height) = $video_info =~ /Original height\s*:\s*(\d+)\n/;
107 0 0         ($height) = $video_info =~ /Height\s*:\s*(\d+)\n/ unless $height;
108 0           ($fps) = $video_info =~ /Frame rate\s*:\s*([\d\.]+)\n/;
109 0 0         ($fps) = $video_info =~ /frame rate\s*:\s*([\d\.]+)\s*fps\n/
110             unless $fps;
111 0           ($frame_count) = $video_info =~ /Frame count\s*:\s*(\d+)\n/;
112 0           ($fps_mode) = $video_info =~ /Frame rate mode\s*:\s*([\w\.]+)\n/i;
113 0           ($dar) = $video_info =~ /Display aspect ratio\s*:\s*([\d\.]+)\n/i;
114 0 0 0       $frame_count = int($fps * $video_length / 1000)
      0        
      0        
115             if ( $fps
116             and $video_length
117             and (!$frame_count or $frame_count <= 0));
118 0 0 0       $fps = substr($frame_count / $video_length * 1000, 0, 6)
      0        
      0        
119             if ((!$fps or $fps <= 0) and $video_length and $frame_count);
120 0 0 0       $video_length = substr($frame_count / $fps * 1000, 0, 6)
      0        
      0        
121             if ( $fps
122             and (!$video_length or $video_length <= 0)
123             and $frame_count);
124 0 0 0       $video_length = $length
      0        
125             if (!$video_length and $length and $video_info);
126 0           ($rotation) = $video_info =~ /Rotation\s*:\s*([\d\.]+)\n/i;
127 0 0         $rotation = 0 unless $rotation;
128             }
129              
130 0           my $audio_codec;
131             my $audio_format;
132 0           my $audio_length;
133 0           my $audio_bitrate;
134 0           my $audio_rate;
135 0           my $audio_language;
136 0           my $audio_channel;
137 0           my $audio_channel_original;
138              
139 0 0         if ($audio_info)
140             {
141 0           ($audio_codec) = $audio_info =~ /Codec\s*:\s*([\w\_\-\\\/ ]+)\n/;
142 0           ($audio_format) = $audio_info =~ /Format\s*:\s*([\w\_\-\\\/ ]+)\n/;
143 0 0         $audio_codec =~ s/\s//g if $audio_codec;
144 0 0         $audio_format =~ s/\s//g if $audio_format;
145 0           ($audio_length) = $audio_info =~ /Duration\s*:\s*(\d+)\.?\d*\n/;
146 0           ($audio_bitrate) = $audio_info =~ /Bit rate\s*:\s*(\d+)\n/;
147 0           ($audio_rate) = $audio_info =~ /Sampling rate\s*:\s*(\d+)\n/;
148 0 0 0       $audio_length = $video_length
      0        
      0        
149             if ( (!$audio_length or $audio_length <= 0)
150             and $video_length
151             and $audio_info);
152 0           ($audio_language) = $audio_info =~ /Language\s*:\s*(\w+)\n/;
153              
154             #add by @owen 201401222 start
155              
156 0           ($audio_channel) = $audio_info =~ /Channel\(s\)\s*:\s*(\d+)\n/;
157 0           ($audio_channel_original) =
158             $audio_info =~ /Channel\(s\)_Original\s*:\s*(\d+)\n/;
159 0           my ($channel_layout) = $audio_info =~ /ChannelLayout\s*:\s*([\w\s]+)/;
160 0           my @layout = split(/ /, $channel_layout);
161 0 0         if ($audio_channel_original)
162             {
163 0 0 0       if ( $audio_channel_original == scalar @layout
164             and $audio_channel_original != $audio_channel)
165             {
166 0           $audio_channel = $audio_channel_original;
167             }
168             }
169              
170             #add by @owen 20141222 end
171             }
172              
173 0 0         $self->{'filename'} = $file if $file;
174 0 0         $self->{'filesize'} = $filesize if $filesize;
175 0 0         $self->{'container'} = lc($container) if $container;
176 0 0         $self->{'length'} = $length if $length;
177 0 0         $self->{'bitrate'} = $bitrate if $bitrate;
178 0 0         $self->{'title'} = $title if $title;
179 0 0         $self->{'album'} = $album if $album;
180 0 0         $self->{'track_name'} = $track_name if $track_name;
181 0 0         $self->{'performer'} = $performer if $performer;
182 0 0         $self->{'video_codec'} = lc($video_codec) if $video_codec;
183 0 0         $self->{'video_format'} = lc($video_format) if $video_format;
184 0 0         $self->{'video_codec_profile'} = lc($video_codec_profile)
185             if $video_codec_profile;
186 0 0         $self->{'video_format_profile'} = lc($video_format_profile)
187             if $video_format_profile;
188 0 0         $self->{'video_length'} = $video_length if $video_length;
189 0 0         $self->{'video_bitrate'} = $video_bitrate if $video_bitrate;
190 0 0         $self->{'width'} = $width if $width;
191 0 0         $self->{'height'} = $height if $height;
192 0 0         $self->{'fps'} = $fps if $fps;
193 0 0         $self->{'fps_mode'} = lc($fps_mode) if $fps_mode;
194 0 0         $self->{'dar'} = $dar if $dar;
195 0 0         $self->{'frame_count'} = $frame_count if $frame_count;
196 0 0         $self->{'rotation'} = $rotation if $rotation;
197 0 0         $self->{'audio_codec'} = lc($audio_codec) if $audio_codec;
198 0 0         $self->{'audio_format'} = lc($audio_format) if $audio_format;
199 0 0         $self->{'audio_length'} = $audio_length if $audio_length;
200 0 0         $self->{'audio_bitrate'} = $audio_bitrate if $audio_bitrate;
201 0 0         $self->{'audio_rate'} = $audio_rate if $audio_rate;
202 0 0         $self->{'audio_language'} = $audio_language if $audio_language;
203 0 0         $self->{'audio_channel'} = $audio_channel if $audio_channel;
204 0 0         $self->{'have_video'} = ($video_info) ? 1 : 0;
205 0 0         $self->{'have_audio'} = ($audio_info) ? 1 : 0;
206             }
207              
208             1;
209              
210             __END__