File Coverage

blib/lib/Movie/Info.pm
Criterion Covered Total %
statement 12 30 40.0
branch 2 8 25.0
condition 1 5 20.0
subroutine 3 4 75.0
pod 2 2 100.0
total 20 49 40.8


line stmt bran cond sub pod time code
1             package Movie::Info;
2              
3 2     2   21494 use strict;
  2         4  
  2         76  
4 2     2   1638 use File::Which qw(where);
  2         2138  
  2         871  
5              
6             our $VERSION = "0.2";
7              
8              
9             =head1 NAME
10              
11             Movie::Info - get meta data from various format movie files
12              
13             =head1 SYNOPSIS
14              
15             my $mi = Movie::Info->new || die "Couldn't find an mplayer to use\n";
16              
17             foreach my $file (@ARGV) {
18             my %info = $mi->info($file) || warn "Couldn't read info from $file\n" && next;
19             print "$file (WxH) - $info{width}x$info{height}\n";
20              
21             }
22              
23             =head1 DESCRIPTION
24              
25             C is a thin layer around B's C<--identify> command
26             line flag. As such it can only give you as much information as Mplayer
27             is able to give you which is down to the quality and number of codecs
28             you have installed.
29              
30             MPlayer is available from http://www.mplayerhq.hu/
31              
32             This module is largely based on the C script shipped with
33             MPlayer.
34              
35             =cut
36              
37             =head1 METHODS
38              
39             =head2 new [path to mplayer]
40              
41             Returns a new C instance or undef if it can't find an
42             mplayer binary.
43              
44             To find a binary it looks in three places - firstly if you've passed in
45             a path to look at it checks there, secondly at the environment variable
46             C<$MOVIE_INFO_MPLAYER_PATH> and then finally it searches your C<$PATH>
47             like the standard C command in Unix.
48              
49             =cut
50              
51              
52             sub new {
53 1     1 1 10 my $class = shift;
54 1         1 my $mplayer;
55              
56 1         7 my @where = where('mplayer');
57 1         219 for my $cand ( ( shift, $ENV{MOVIE_INFO_MPLAYER_PATH}, @where ) ) {
58 2 50 33     7 next unless defined $cand && -x $cand;
59 0         0 $mplayer=$cand;
60 0         0 last;
61             }
62              
63 1 50       6 return undef unless defined $mplayer;
64              
65              
66 0           BLESS:
67             return bless { _mplayer_binary => $mplayer }, $class;
68             }
69              
70              
71             =head2 info
72              
73             Returns a hash representing all the meta data we can garner about file.
74              
75             Returns undef if it can't read the file.
76              
77             =cut
78              
79             sub info {
80 0     0 1   my $self = shift;
81 0   0       my $file = shift || return undef;
82 0           my %info;
83              
84 0           my $mplayer = $self->{_mplayer_binary};
85              
86 0 0         open(MPLAYER, "$mplayer -vo null -ao null -frames 0 -identify \"$file\" 2>/dev/null|") || die "Couldn't read from $mplayer: $!\n";
87 0           while () {
88 0 0         next unless s/^ID_//;
89 0           s/^VIDEO_//;
90 0           chomp;
91 0           s/(^\s*|\s*$)//g;
92 0           s/([`\\!$"])/\\$1/g;
93 0           my ($key, $value) = split /=/, $_, 2;
94 0           $info{lc($key)} = $value;
95            
96             }
97             #$info{filename} = $file;
98              
99 0           close MPLAYER;
100              
101              
102 0           return %info;
103             }
104              
105              
106             =head1 AUTHOR
107              
108             Simon Wistow
109              
110             =head1 COPYRIGHT
111              
112             Copyright 2005, Simon Wistow
113              
114             Released under the same terms as Perl itself.
115              
116             =cut
117              
118             1;