File Coverage

blib/lib/Mediainfo.pm
Criterion Covered Total %
statement 12 145 8.2
branch 0 102 0.0
condition 0 44 0.0
subroutine 4 6 66.6
pod 0 2 0.0
total 16 299 5.3


line stmt bran cond sub pod time code
1             package Mediainfo;
2 1     1   22533 use strict;
  1         3  
  1         49  
3 1     1   6 use warnings;
  1         3  
  1         33  
4 1     1   1101 use IPC::Open3;
  1         12734  
  1         408  
5              
6             our $VERSION = '0.11';
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   13 use Symbol 'gensym'; $err = gensym;
  1         2  
  1         2007  
  0            
46 0           my $pid = open3($wtr, $rdr, $err, "$mediainfo_exec -f \"$file\"");
47 0           close $wtr;
48 0           while(<$rdr>) {$mediainfo .= $_;}
  0            
49 0           while(<$err>) {$mediainfo_err .= $_;}
  0            
50 0           waitpid( $pid, 0);
51              
52 0 0         $mediainfo =~ s/\r//g if $mediainfo;
53 0           my ($genernal_info) = $mediainfo =~ /(^General\n.*?\n\n)/sm;
54 0 0         return undef unless $genernal_info;
55              
56 0           my ($video_info) = $mediainfo =~ /(^Video[\s\#\d]*\n.*?\n\n)/sm;
57 0           my ($audio_info) = $mediainfo =~ /(^Audio[\s\#\d]*\n.*?\n\n)/sm;
58              
59 0           my $container;
60             my $length;
61 0           my $bitrate;
62 0           my $title;
63 0           my $album;
64 0           my $track_name;
65 0           my $performer;
66 0           ($container) = $genernal_info =~ /Format\s*:\s*([\w\_\-\\\/\. ]+)\n/;
67 0 0         $container =~ s/\s//g if $container;
68 0           ($length) = $genernal_info =~ /Duration\s*:\s*(\d+)\.?\d*\n/;
69 0           ($bitrate) = $genernal_info =~ /Overall bit rate\s*:\s*(\d+)\n/;
70 0           ($title) = $genernal_info =~ /Title\s*:\s*(.+)\n/;
71 0           ($album) = $genernal_info =~ /Album\s*:\s*(.+)\n/;
72 0           ($track_name) = $genernal_info =~ /Track name\s*:\s*(.+)\n/;
73 0           ($performer) = $genernal_info =~ /Performer\s*:\s*(.+)\n/;
74              
75 0           my $video_codec;
76             my $video_codec_profile;
77 0           my $video_format;
78 0           my $video_format_profile;
79 0           my $video_length;
80 0           my $video_bitrate;
81 0           my $width;
82 0           my $height;
83 0           my $fps;
84 0           my $frame_count;
85 0           my $fps_mode;
86 0           my $dar;
87 0           my $rotation;
88 0 0         if($video_info)
89             {
90 0           ($video_codec) = $video_info =~ /Codec\s*:\s*([\w\_\-\\\/ ]+)\n/;
91 0           ($video_format) = $video_info =~ /Format\s*:\s*([\w\_\-\\\/ ]+)\n/;
92 0           ($video_codec_profile) = $video_info =~ /Codec profile\s*:\s*([\w\_\-\\\/\@\. ]+)\n/;
93 0           ($video_format_profile) = $video_info =~ /Format profile\s*:\s*([\w\_\-\\\/\@\. ]+)\n/;
94 0 0         $video_codec =~ s/\s//g if $video_codec;
95 0 0         $video_format =~ s/\s//g if $video_format;
96 0 0         $video_codec_profile =~ s/\s//g if $video_codec_profile;
97 0 0         $video_format_profile =~ s/\s//g if $video_format_profile;
98 0           ($video_length) = $video_info =~ /Duration\s*:\s*(\d+)\.?\d*\n/;
99 0           ($video_bitrate) = $video_info =~ /Bit rate\s*:\s*(\d+)\n/;
100 0           ($width) = $video_info =~ /Original width\s*:\s*(\d+)\n/;
101 0 0         ($width) = $video_info =~ /Width\s*:\s*(\d+)\n/ unless $width;
102 0           ($height) = $video_info =~ /Original height\s*:\s*(\d+)\n/;
103 0 0         ($height) = $video_info =~ /Height\s*:\s*(\d+)\n/ unless $height;
104 0           ($fps) = $video_info =~ /Frame rate\s*:\s*([\d\.]+)\n/;
105 0 0         ($fps) = $video_info =~ /frame rate\s*:\s*([\d\.]+)\s*fps\n/ unless $fps;
106 0           ($frame_count) = $video_info =~ /Frame count\s*:\s*(\d+)\n/;
107 0           ($fps_mode) = $video_info =~ /Frame rate mode\s*:\s*([\w\.]+)\n/i;
108 0           ($dar) = $video_info =~ /Display aspect ratio\s*:\s*([\d\.]+)\n/i;
109 0 0 0       $frame_count = int($fps * $video_length / 1000) if ($fps and $video_length and (!$frame_count or $frame_count <= 0) );
      0        
      0        
110 0 0 0       $fps = substr($frame_count / $video_length * 1000, 0, 6) if ( (!$fps or $fps <= 0) and $video_length and $frame_count);
      0        
      0        
111 0 0 0       $video_length = substr($frame_count / $fps * 1000, 0, 6) if ($fps and (!$video_length or $video_length <= 0) and $frame_count);
      0        
      0        
112 0 0 0       $video_length = $length if (!$video_length and $length and $video_info);
      0        
113 0           ($rotation) = $video_info =~ /Rotation\s*:\s*([\d\.]+)\n/i;
114 0 0         $rotation = 0 unless $rotation;
115             }
116              
117 0           my $audio_codec;
118             my $audio_format;
119 0           my $audio_length;
120 0           my $audio_bitrate;
121 0           my $audio_rate;
122 0           my $audio_language;
123 0 0         if($audio_info)
124             {
125 0           ($audio_codec) = $audio_info =~ /Codec\s*:\s*([\w\_\-\\\/ ]+)\n/;
126 0           ($audio_format) = $audio_info =~ /Format\s*:\s*([\w\_\-\\\/ ]+)\n/;
127 0 0         $audio_codec =~ s/\s//g if $audio_codec;
128 0 0         $audio_format =~ s/\s//g if $audio_format;
129 0           ($audio_length) = $audio_info =~ /Duration\s*:\s*(\d+)\.?\d*\n/;
130 0           ($audio_bitrate) = $audio_info =~ /Bit rate\s*:\s*(\d+)\n/;
131 0           ($audio_rate) = $audio_info =~ /Sampling rate\s*:\s*(\d+)\n/;
132 0 0 0       $audio_length = $video_length if ( (!$audio_length or $audio_length <= 0) and $video_length and $audio_info);
      0        
      0        
133 0           ($audio_language) = $audio_info =~ /Language\s*:\s*(\w+)\n/;
134             }
135              
136 0 0         $self->{'filename'} = $file if $file;
137 0 0         $self->{'filesize'} = $filesize if $filesize;
138 0 0         $self->{'container'} = lc($container) if $container;
139 0 0         $self->{'length'} = $length if $length;
140 0 0         $self->{'bitrate'} = $bitrate if $bitrate;
141 0 0         $self->{'title'} = $title if $title;
142 0 0         $self->{'album'} = $album if $album;
143 0 0         $self->{'track_name'} = $track_name if $track_name;
144 0 0         $self->{'performer'} = $performer if $performer;
145 0 0         $self->{'video_codec'} = lc($video_codec) if $video_codec;
146 0 0         $self->{'video_format'} = lc($video_format) if $video_format;
147 0 0         $self->{'video_codec_profile'} = lc($video_codec_profile) if $video_codec_profile;
148 0 0         $self->{'video_format_profile'} = lc($video_format_profile) if $video_format_profile;
149 0 0         $self->{'video_length'} = $video_length if $video_length;
150 0 0         $self->{'video_bitrate'} = $video_bitrate if $video_bitrate;
151 0 0         $self->{'width'} = $width if $width;
152 0 0         $self->{'height'} = $height if $height;
153 0 0         $self->{'fps'} = $fps if $fps;
154 0 0         $self->{'fps_mode'} = lc($fps_mode) if $fps_mode;
155 0 0         $self->{'dar'} = $dar if $dar;
156 0 0         $self->{'frame_count'} = $frame_count if $frame_count;
157 0 0         $self->{'rotation'} = $rotation if $rotation;
158 0 0         $self->{'audio_codec'} = lc($audio_codec) if $audio_codec;
159 0 0         $self->{'audio_format'} = lc($audio_format) if $audio_format;
160 0 0         $self->{'audio_length'} = $audio_length if $audio_length;
161 0 0         $self->{'audio_bitrate'} = $audio_bitrate if $audio_bitrate;
162 0 0         $self->{'audio_rate'} = $audio_rate if $audio_rate;
163 0 0         $self->{'audio_language'} = $audio_language if $audio_language;
164 0 0         $self->{'have_video'} = ($video_info) ? 1 : 0;
165 0 0         $self->{'have_audio'} = ($audio_info) ? 1 : 0;
166             }
167              
168             1;
169              
170             __END__