File Coverage

blib/lib/Mediainfo.pm
Criterion Covered Total %
statement 12 157 7.6
branch 0 110 0.0
condition 0 47 0.0
subroutine 4 6 66.6
pod 0 2 0.0
total 16 322 4.9


line stmt bran cond sub pod time code
1             package Mediainfo;
2 1     1   72941 use strict;
  1         3  
  1         30  
3 1     1   6 use warnings;
  1         2  
  1         26  
4 1     1   541 use IPC::Open3;
  1         4138  
  1         234  
5              
6             our $VERSION = '0.13';
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   8 use Symbol 'gensym';
  1         2  
  1         1927  
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 0           my $audio_channel_layout;
139              
140 0 0         if ($audio_info)
141             {
142 0           ($audio_codec) = $audio_info =~ /Codec\s*:\s*([\w\_\-\\\/ ]+)\n/;
143 0           ($audio_format) = $audio_info =~ /Format\s*:\s*([\w\_\-\\\/ ]+)\n/;
144 0 0         $audio_codec =~ s/\s//g if $audio_codec;
145 0 0         $audio_format =~ s/\s//g if $audio_format;
146 0           ($audio_length) = $audio_info =~ /Duration\s*:\s*(\d+)\.?\d*\n/;
147 0           ($audio_bitrate) = $audio_info =~ /Bit rate\s*:\s*(\d+)\n/;
148 0           ($audio_rate) = $audio_info =~ /Sampling rate\s*:\s*(\d+)\n/;
149 0 0 0       $audio_length = $video_length
      0        
      0        
150             if ( (!$audio_length or $audio_length <= 0)
151             and $video_length
152             and $audio_info);
153 0           ($audio_language) = $audio_info =~ /Language\s*:\s*(\w+)\n/;
154              
155             #add by @owen 201401222 start
156              
157 0           ($audio_channel) = $audio_info =~ /Channel\(s\)\s*:\s*(\d+)\n/;
158 0           ($audio_channel_original) =
159             $audio_info =~ /Channel\(s\)_Original\s*:\s*(\d+)\n/;
160 0           ($audio_channel_layout) = $audio_info =~ /ChannelLayout\s*:\s*([\w\s]+)/;
161 0           my @layout;
162 0 0         @layout = split(/ /, $audio_channel_layout) if $audio_channel_layout;
163 0 0         if ($audio_channel_original)
164             {
165 0 0 0       if ( $audio_channel_original == scalar @layout
166             and $audio_channel_original != $audio_channel)
167             {
168 0           $audio_channel = $audio_channel_original;
169             }
170             }
171              
172             #add by @owen 20141222 end
173             }
174              
175 0 0         $self->{'filename'} = $file if $file;
176 0 0         $self->{'filesize'} = $filesize if $filesize;
177 0 0         $self->{'container'} = lc($container) if $container;
178 0 0         $self->{'length'} = $length if $length;
179 0 0         $self->{'bitrate'} = $bitrate if $bitrate;
180 0 0         $self->{'title'} = $title if $title;
181 0 0         $self->{'album'} = $album if $album;
182 0 0         $self->{'track_name'} = $track_name if $track_name;
183 0 0         $self->{'performer'} = $performer if $performer;
184 0 0         $self->{'video_codec'} = lc($video_codec) if $video_codec;
185 0 0         $self->{'video_format'} = lc($video_format) if $video_format;
186 0 0         $self->{'video_codec_profile'} = lc($video_codec_profile)
187             if $video_codec_profile;
188 0 0         $self->{'video_format_profile'} = lc($video_format_profile)
189             if $video_format_profile;
190 0 0         $self->{'video_length'} = $video_length if $video_length;
191 0 0         $self->{'video_bitrate'} = $video_bitrate if $video_bitrate;
192 0 0         $self->{'width'} = $width if $width;
193 0 0         $self->{'height'} = $height if $height;
194 0 0         $self->{'fps'} = $fps if $fps;
195 0 0         $self->{'fps_mode'} = lc($fps_mode) if $fps_mode;
196 0 0         $self->{'dar'} = $dar if $dar;
197 0 0         $self->{'frame_count'} = $frame_count if $frame_count;
198 0 0         $self->{'rotation'} = $rotation if $rotation;
199 0 0         $self->{'audio_codec'} = lc($audio_codec) if $audio_codec;
200 0 0         $self->{'audio_format'} = lc($audio_format) if $audio_format;
201 0 0         $self->{'audio_length'} = $audio_length if $audio_length;
202 0 0         $self->{'audio_bitrate'} = $audio_bitrate if $audio_bitrate;
203 0 0         $self->{'audio_rate'} = $audio_rate if $audio_rate;
204 0 0         $self->{'audio_language'} = $audio_language if $audio_language;
205 0 0         $self->{'audio_channel'} = $audio_channel if $audio_channel;
206 0 0         $self->{'have_video'} = ($video_info) ? 1 : 0;
207 0 0         $self->{'have_audio'} = ($audio_info) ? 1 : 0;
208             }
209              
210             1;
211              
212             __END__