source: mp3-find/trunk/lib/MP3/Find/Util.pm @ 29

Last change on this file since 29 was 29, checked in by peter, 18 years ago
  • factored out 'get_mp3_metadata' function from Filesystem.pm to Util.pm
  • added 'update' function to DB.pm that combines 'update_db' and 'update_file' (for updating just specific files); idea courtesy of Matt Dietrich
  • modified mp3db to let you mix and match files and directories on the command line; now also uses the 'update' function in DB.pm
File size: 1.8 KB
Line 
1package MP3::Find::Util;
2
3use strict;
4use warnings;
5
6use base qw(Exporter);
7use vars qw(@EXPORT_OK);
8
9@EXPORT_OK = qw(build_query get_mp3_metadata);
10
11use Carp;
12use MP3::Info;
13
14eval { require MP3::Tag };
15my $CAN_USE_ID3V2 = $@ ? 0 : 1;
16
17sub build_query {
18    my @args = @_;
19   
20    # first find all the directories
21    my @dirs;
22    while (local $_ = shift @args) {
23        if (/^-/) {
24            # whoops, there's the beginning of the query
25            unshift @args, $_;
26            last;
27        } else {
28            push @dirs, $_;
29        }
30    }
31   
32    # now build the query hash
33    my %query;
34    my $field;
35    while (local $_ = shift @args) {
36        if (/^--?(.*)/) {
37            $field = uc $1;
38        } else {
39            $field ? push @{ $query{$field} }, $_ : die "Need a field name before value '$_'\n";
40        }
41    }
42   
43    return (\@dirs, \%query);
44}
45
46sub get_mp3_metadata {
47    my $args = shift;
48
49    my $filename = $args->{filename} or croak "get_mp3_metadata needs a 'filename' argument";
50   
51    my $mp3 = {
52        FILENAME => $filename,
53        %{ get_mp3tag($filename)  || {} },
54        %{ get_mp3info($filename) || {} },
55    };
56   
57    if ($CAN_USE_ID3V2 and $args->{use_id3v2}) {
58        # add ID3v2 tag info, if present
59        my $mp3_tags = MP3::Tag->new($filename);
60        unless (defined $mp3_tags) {
61            warn "Can't get MP3::Tag object for $filename\n";
62        } else {
63            $mp3_tags->get_tags;
64            if (my $id3v2 = $mp3_tags->{ID3v2}) {
65                for my $frame_id (keys %{ $id3v2->get_frame_ids }) {
66                    my ($info) = $id3v2->get_frame($frame_id);
67                    if (ref $info eq 'HASH') {
68                        # use the "Text" value as the value for this frame, if present
69                        $mp3->{$frame_id} = $info->{Text} if exists $info->{Text};
70                    } else {
71                        $mp3->{$frame_id} = $info;
72                    }
73                }
74            }
75        }
76    }
77
78    return $mp3;
79}
80
81# module return
821;
Note: See TracBrowser for help on using the repository browser.