#!/usr/bin/perl -w use strict; use DBI; use File::Find; use MP3::Info; use Getopt::Std; my (%options, $DBH, $STH, $tmp, %mp3s, $artist, $album, $title); getopts("d:au:p:h", \%options); if ($options{h}) { print <<'eof'; -d dir: Root MP3 directory -u username: Username for mysql -p password: Password for mysql -a: Turn aliasing on -h: This help file eof exit; } my (@dirs) = $options{m} || ("Your mp3 dir"); my ($orig_path) = $dirs[0]; $orig_path =~ /(.+?)(\/(.+?)\/)/; my ($alias) = $2; my ($user_name) = $options{u} || 'user'; my ($password) = $options{p} || 'password'; my ($database) = 'DBI:mysql:mp3'; find(\&id3info, @dirs); sub id3info { return unless $File::Find::name =~ /^.+?\.[Mm][Pp]3$/; $tmp = get_mp3tag($File::Find::name); return unless defined $$tmp{'ARTIST'} && defined $$tmp{'TITLE'}; $$tmp{'ALBUM'} = "Other" unless defined $$tmp{'ALBUM'}; $$tmp{'TRACKNUM'} = 0 unless defined $$tmp{'TRACKNUM'}; unless (exists $mp3s{$$tmp{'ARTIST'}}{$$tmp{'ALBUM'}}{$$tmp{'TITLE'}}) { %{$mp3s{$$tmp{'ARTIST'}}{$$tmp{'ALBUM'}}{$$tmp{'TITLE'}}} = ( 'PATH' => $File::Find::name, 'TRACK' => $$tmp{'TRACKNUM'}, 'TIME' => $$tmp{'TIME'} ); } else { if ($mp3s{$$tmp{'ARTIST'}}{$$tmp{'ALBUM'}}{$$tmp{'TITLE'}}{'TIME'} < $$tmp{'TIME'}) { %{$mp3s{$$tmp{'ARTIST'}}{$$tmp{'ALBUM'}}{$$tmp{'TITLE'}}} = ( 'PATH' => $File::Find::name, 'TRACK' => $$tmp{'TRACK'}, 'TIME' => $$tmp{'TIME'} ); } } if ($options{a}) { $mp3s{$$tmp{'ARTIST'}}{$$tmp{'ALBUM'}}{$$tmp{'TITLE'}}{'PATH'} =~ s/^$orig_path(.+)$/$alias$1/; } } $DBH = DBI->connect ($database, $user_name, $password, { RaiseError => 1 }); $STH = $DBH->prepare (qq{ INSERT mp3s (artist,album,title,track,path) VALUES(?,?,?,?,?)}); foreach $artist (keys %mp3s) { foreach $album (keys %{ $mp3s{$artist} }) { foreach $title (keys %{ $mp3s{$artist}{$album} }) { $STH->execute ($artist, $album, $title, $mp3s{$artist}{$album}{$title}{'TRACK'}, $mp3s{$artist}{$album}{$title}{'PATH'}); } } } $STH->finish(); $DBH->disconnect ();