| 1 | package Audio::DB::Parse::ReadFiles; | ||
| 2 | |||
| 3 | # $Id: ReadFiles.pm,v 1.2 2005/02/27 16:56:25 todd Exp $ | ||
| 4 | # Parse tag information directly from MP3 files | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use vars qw/@ISA/; | ||
| 8 | |||
| 9 | use MP3::Info; | ||
| 10 | use File::Basename 'dirname','basename'; # Lincoln's recursive approach | ||
| 11 | use File::Find; # For recursing into directories via my method... | ||
| 12 | use LWP::MediaTypes; # NOT YET IN USE | ||
| 13 | |||
| 14 | @ISA = qw/Audio::DB::Build/; | ||
| 15 | |||
| 16 | # Ripped from Apache::MP3 | ||
| 17 | # But not yet being used | ||
| 18 | # Some audio file types... | ||
| 19 | |||
| 20 | # .. and some translations just to be nice | ||
| 21 | my %modes = ( | ||
| 22 | 0 => 'stereo', | ||
| 23 | 1 => 'joint stereo', | ||
| 24 | 2 => 'dual stereo', | ||
| 25 | 3 => 'mono' # Early beatles, ya know | ||
| 26 | ); | ||
| 27 | my @suffix = qw(.ogg .OGG .wav .WAV .mp3 .MP3 .mpeg .MPEG); | ||
| 28 | my %supported_types = ( | ||
| 29 | # type condition handler method | ||
| 30 | 'audio/mpeg' => eval "use MP3::Info; 1;" && 'read_mpeg', | ||
| 31 | 'application/x-ogg' => eval "use Ogg::Vorbis; 1;" && 'read_vorbis', | ||
| 32 | 'audio/x-wav' => eval "use Audio::Wav; 1;" && 'read_wav', | ||
| 33 | ); | ||
| 34 | |||
| 35 | |||
| 36 | |||
| 37 | # sub routines for directly reading tag information from files | ||
| 38 | # This should just return full path of all mp3s | ||
| 39 | # option for processing or not... | ||
| 40 | my $cur_obj; | ||
| 41 | sub _process_directories { | ||
| 42 | my ($name,$self,$top_dirs) = @_; | ||
| 43 | $cur_obj = $self; | ||
| 44 | foreach (@$top_dirs) { | ||
| 45 | if (defined $self->{shout}) { print STDERR "Finding MP3-containing subdirectories within $_...\n"; }; | ||
| 46 | finddepth(\&_store,$_); | ||
| 47 | } | ||
| 48 | |||
| 49 | my @dirs = keys %{$self->{recursed_dirs}}; | ||
| 50 | if (defined $self->{shout}) { print STDERR "Processing " . scalar @dirs . " directories...\n"; }; | ||
| 51 | |||
| 52 | foreach my $path (@dirs) { | ||
| 53 | $self->{counters}->{dirs_processed}++; | ||
| 54 | opendir(DIR,$path); | ||
| 55 | while (my $file = readdir(DIR)) { | ||
| 56 | my $full_path = $path . $file; | ||
| 57 | |||
| 58 | # Files to ignore for now | ||
| 59 | # This should be handled by the recursive directory listing | ||
| 60 | # THis should be done within the directory option | ||
| 61 | next unless ($file =~ /.*\.mp3/); | ||
| 62 | |||
| 63 | # LATER, TRY TO BE MORE SPECIFC - COULD EVEN HANDLE IMAGES FOR BUILDING | ||
| 64 | # HTML PAGES JPG GIF HTML TXT, ETC | ||
| 65 | next if ($file =~ /^\./); | ||
| 66 | next if ($file =~ /.*\.txt/); | ||
| 67 | |||
| 68 | # Parse the data directly from the file | ||
| 69 | my $tags = {}; | ||
| 70 | my $info = {}; | ||
| 71 | ($tags,$info) = $self->_get_tags($full_path,$file); | ||
| 72 | |||
| 73 | # TODO | ||
| 74 | # No reasonable way of dealing with songs lacking tags yet | ||
| 75 | if ($tags->{artist} eq '\N' || $tags->{album} eq '\N') { | ||
| 76 | push (@{$self->{couldnt_read}},$full_path); | ||
| 77 | next; | ||
| 78 | } | ||
| 79 | |||
| 80 | $self->cache_song($tags); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | return @dirs; | ||
| 84 | } | ||
| 85 | |||
| 86 | |||
| 87 | # I can't pass any extra parameters to _store | ||
| 88 | # through the finddepth subroutine. | ||
| 89 | # Ugliness! Have to define the $cur_obj parameter. | ||
| 90 | sub _store { | ||
| 91 | my $pwd = `pwd`; | ||
| 92 | chomp $pwd; | ||
| 93 | $pwd .= '/'; | ||
| 94 | if (-d $pwd) { | ||
| 95 | $cur_obj->{recursed_dirs}->{$pwd}++; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | # Fetch tags and file_info for the current file | ||
| 100 | # and load into into a temporary hash ref | ||
| 101 | sub _get_tags { | ||
| 102 | my ($self,$fullpath,$file) = @_; | ||
| 103 | my $flag; | ||
| 104 | my $tag = get_mp3tag($fullpath) || $flag++; | ||
| 105 | if ($flag) { | ||
| 106 | push (@{$self->{couldnt_read}},$fullpath); | ||
| 107 | $tag = undef; | ||
| 108 | return $tag; | ||
| 109 | } | ||
| 110 | |||
| 111 | return unless my $info = get_mp3info($fullpath); | ||
| 112 | |||
| 113 | my ($title,$artist,$album,$year,$comment,$genre,$track) = | ||
| 114 | @{$tag}{qw(TITLE ARTIST ALBUM YEAR COMMENT GENRE TRACKNUM)} if $tag; | ||
| 115 | my $duration = sprintf "%d:%2.2d", $info->{MM}, $info->{SS}; | ||
| 116 | my $seconds = ($info->{MM} * 60) + $info->{SS}; | ||
| 117 | |||
| 118 | # Need to build up the temporary hash with | ||
| 119 | # renamed hash keys | ||
| 120 | |||
| 121 | # THIS PLACEHOLDER SHOULD BE DSN SPECIFIC - | ||
| 122 | # SHOULD INITIALIZE PLACEHOLDER THERE | ||
| 123 | my $ph = '\N'; | ||
| 124 | |||
| 125 | my ($track,$tot_tracks) = _parse_tracks($track); | ||
| 126 | my $data = { | ||
| 127 | title => $title || $ph, | ||
| 128 | artist => $artist || $ph, | ||
| 129 | duration => $duration || $ph, | ||
| 130 | genre => $genre || $ph, | ||
| 131 | album => $album || $ph, | ||
| 132 | comment => $comment || $ph, | ||
| 133 | year => $year || $ph, | ||
| 134 | min => $info->{MM} || $ph, | ||
| 135 | sec => $info->{SS} || $ph, | ||
| 136 | seconds => $seconds || $ph, | ||
| 137 | lyrics => $tag->{LYRICS} || $ph, | ||
| 138 | track => $track || $ph, | ||
| 139 | total_tracks => $tot_tracks || $ph, | ||
| 140 | bitrate => $info->{BITRATE} || $ph, | ||
| 141 | samplerate => $info->{FREQUENCY} || $ph, | ||
| 142 | filename => $file, | ||
| 143 | filepath => $fullpath, | ||
| 144 | filesize => $info->{SIZE} || $ph, | ||
| 145 | tagtypes => $tag->{TAGVERSION} || $ph, | ||
| 146 | fileformat => $info->{LAYER} || $ph, | ||
| 147 | channels => $modes{$info->{MODE}} || $ph, | ||
| 148 | vbr => $info->{VBR} || $ph, | ||
| 149 | rating => $tag->{RATING} || $ph, | ||
| 150 | playcount => $tag->{PLAYCOUNT} || $ph, | ||
| 151 | }; | ||
| 152 | |||
| 153 | # if ($DEBUG) { | ||
| 154 | # foreach (keys %$data) { | ||
| 155 | # print $_,"\t",$data->{$_},"\n"; | ||
| 156 | # } | ||
| 157 | # } | ||
| 158 | return ($data,$info); | ||
| 159 | } | ||
| 160 | |||
| 161 | |||
| 162 | sub _parse_tracks { | ||
| 163 | my $tracks = shift; | ||
| 164 | $tracks =~ /(.*)\/(.*)/; | ||
| 165 | my $track = $1; | ||
| 166 | my $tot_tracks = $2; | ||
| 167 | return ($track,$tot_tracks); | ||
| 168 | } | ||
| 169 | |||
| 170 | |||
| 171 | =pod | ||
| 172 | |||
| 173 | =head1 Audio::DB::Parse::ReadFiles.pm | ||
| 174 | |||
| 175 | Glean information on music files directly from their ID3 tags. | ||
| 176 | |||
| 177 | =head1 DESCRIPTION | ||
| 178 | |||
| 179 | Audio::DB::Parse::ReadFiles.pm is used internally by Audio::DB. It's | ||
| 180 | internal, private methods will be called when trying to create or update | ||
| 181 | a music database using the 'dirs' option: | ||
| 182 | |||
| 183 | $mp3->load_database(-dirs =>['/path/to/MP3s/'], | ||
| 184 | -tmp =>'/tmp/'); | ||
| 185 | |||
| 186 | All methods of Audio::DB::Parse::ReadFiles are private (for now). You | ||
| 187 | will never need to interact with Audio::DB::Parse::ReadFiles objects | ||
| 188 | directly. | ||
| 189 | |||
| 190 | =head1 REQUIRES | ||
| 191 | |||
| 192 | B<MP3::Info> for reading ID3 tags, B<LWP::MediaTypes> for distinguising | ||
| 193 | types of readable files; | ||
| 194 | |||
| 195 | =head1 EXPORTS | ||
| 196 | |||
| 197 | No methods are exported. | ||
| 198 | |||
| 199 | =head1 METHODS | ||
| 200 | |||
| 201 | No public methods available. | ||
| 202 | |||
| 203 | =head1 PRIVATE METHODS | ||
| 204 | |||
| 205 | =head2 _process_directories | ||
| 206 | |||
| 207 | Title : _process_directories | ||
| 208 | Usage : $mp3->_process_directories; | ||
| 209 | Function: Fetches all mp3s at top level directories. | ||
| 210 | Returns : Nothing | ||
| 211 | Args : none | ||
| 212 | Status : Private | ||
| 213 | |||
| 214 | Fetches all the mp3s within the top level directories provided | ||
| 215 | during calls to the new() method. Farms off a file at a time | ||
| 216 | to cache_song for processing. | ||
| 217 | |||
| 218 | =head2 _parse_tracks | ||
| 219 | |||
| 220 | Title : _parse_tracks | ||
| 221 | Usage : _parse_tracks($artist); | ||
| 222 | Function: Splits the track tag into track and total tracks. | ||
| 223 | Returns : track, total tracks | ||
| 224 | Args : raw track field from ID3 tag | ||
| 225 | Status : Private | ||
| 226 | |||
| 227 | =head1 AUTHOR | ||
| 228 | |||
| 229 | Copyright 2002-2004, Todd W. Harris <harris@cshl.org>. | ||
| 230 | |||
| 231 | This module is distributed under the same terms as Perl itself. Feel | ||
| 232 | free to use, modify and redistribute it as long as you retain the | ||
| 233 | correct attribution. | ||
| 234 | |||
| 235 | =head1 ACKNOWLEDGEMENTS | ||
| 236 | |||
| 237 | Chris Nandor <pudge@pudge.net> wrote MP3::Info, the module responsible for | ||
| 238 | reading MP3 tags. Without, this module would be a best-selling pulp | ||
| 239 | romance novel behind the gum at the grocery store checkout. Chris has | ||
| 240 | been really helpful with issues that arose with various MP3 tags from | ||
| 241 | different taggers. Kudos, dude! | ||
| 242 | |||
| 243 | Lincoln (Dr. Leichtenstein) Stein <lstein@cshl.org> wrote much of the original | ||
| 244 | adaptor code as part of the l<Bio::DB::GFF> module. Much of that code is | ||
| 245 | incorporated here, albeit in a pared-down form. The code for reading ID3 tags | ||
| 246 | from files only with appropriate MIME-types is borrowed from his <Apache::MP3> | ||
| 247 | module. This was a much more elegant than my lame solution of checking for .mp3! | ||
| 248 | Lincoln tolerates having me in his lab, too, even though I use a Mac. | ||
| 249 | |||
| 250 | =head1 SEE ALSO | ||
| 251 | |||
| 252 | L<Audio::DB>,L<MP3::Info> | ||
| 253 | |||
| 254 | =cut | ||
| 255 | |||
| 256 | |||
| 257 | 1; |