use DBI;
use Digest::MD5;
-use Audio::TagLib;
+use Music::Tag;
+use MP3::Info;
use File::Find;
use Encode;
+$MP3::Info::try_harder = 1;
+
my $uthome = "$ENV{HOME}/.utunes";
my $dbh = DBI->connect("dbi:SQLite:dbname=$uthome/tunes.db","","")
or die "Could not open utunes DB";
+
$dbh->{unicode} = 1;
+
my $rv = $dbh->do("SELECT count(*) FROM tunes LIMIT 1");
if (!$rv) {
print "Creating database schema...\n";
track integer,
year integer,
length integer,
- bitrate integer,
- samplerate integer,
- channels integer
+ bitrate integer
)});
}
return 1;
}
- my $f = Audio::TagLib::FileRef->new($file);
- return unless $f->file();
- return unless $f->audioProperties();
+ my $f = Music::Tag->new($file);
+ next unless @{$f->{_plugins}};
+ $f->get_tag();
my (%strings, %numeric);
- if ($f->tag()) {
- %strings = (
- title => $f->tag()->title(),
- artist => $f->tag()->artist(),
- album => $f->tag()->album(),
- comment => $f->tag()->comment(),
- genre => $f->tag()->genre(),
- );
-
- %numeric = (
- year => $f->tag()->year(),
- track => $f->tag()->track(),
- );
- }
+ %strings = (
+ title => $f->title(),
+ artist => $f->artist(),
+ album => $f->album(),
+ comment => $f->comment(),
+ genre => $f->genre(),
+ );
- %numeric = (%numeric,
- length => $f->audioProperties()->length(),
- bitrate => $f->audioProperties()->bitrate(),
- samplerate => $f->audioProperties()->sampleRate(),
- channels => $f->audioProperties()->channels(),
+ %numeric = (
+ year => $f->year(),
+ track => $f->track(),
+ length => $f->secs(),
+ bitrate => $f->bitrate(),
);
+ $f->close();
+
my (@keys, @values);
push @keys, 'hash';
push @values, qq/"$hash"/;
push @values, qq/"$file"/;
foreach my $k (keys %strings) {
if (defined($strings{$k})) {
- my $str = $strings{$k}->stripWhiteSpace()->toCString(1);
+ my $str = $strings{$k};
next unless $str;
push @keys, $k;
$str =~ s/"/""/g;
}
print "\tlength\t\t", join(':',@time),"\n";
print "\tbitrate\t\t", $r->{'bitrate'}, "kbit\n";
- print "\tsamplerate\t", $r->{'samplerate'} / 1000, "kHz\n";
- print "\tchannels\t", $r->{'channels'}, "\n";
print "\tsize\t\t", -s $file, "\n";
}
}
$dbh->commit;
} elsif ($ARGV[0] eq 'clean') {
- my $sth = $dbh->prepare(qq{SELECT hash,filename FROM tunes});
+ my $sth = $dbh->prepare('SELECT hash, filename FROM tunes');
$sth->execute;
my $r;
while ($r = $sth->fetchrow_hashref()) {
! (-e $r->{filename} || -e decode('utf8', $r->{filename}))) {
print "Purging $r->{filename}\n";
my $hash = $r->{hash};
- $dbh->do(qq{DELETE FROM tunes WHERE hash="$hash"});
+ $dbh->do('DELETE FROM tunes WHERE hash=?',
+ undef, $hash);
}
}
} elsif ($ARGV[0] eq 'info') {
exit 1;
}
- my $insert = $dbh->prepare("INSERT INTO tunes (hash,filename,artist,title,album,genre,comment,track,year,length,bitrate,samplerate,channels) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)");
- my $update = $dbh->prepare("UPDATE tunes SET filename=?, artist=?, title=?, album=?, genre=?, comment=?, track=?, year=?, length=?, bitrate=?, samplerate=?, channels=? WHERE hash=?");
+ my $insert = $dbh->prepare("INSERT INTO tunes (hash,filename,artist,title,album,genre,comment,track,year,length,bitrate) VALUES (?,?,?,?,?,?,?,?,?,?,?)");
+ my $update = $dbh->prepare("UPDATE tunes SET filename=?, artist=?, title=?, album=?, genre=?, comment=?, track=?, year=?, length=?, bitrate=? WHERE hash=?");
my $import_dbh = DBI->connect("dbi:SQLite:dbname=$db","","");
- my $import_sth = $import_dbh->prepare("select hash,filename,artist,title,album,genre,comment,track,year,length,bitrate,samplerate,channels from tunes");
+ my $import_sth = $import_dbh->prepare("select hash,filename,artist,title,album,genre,comment,track,year,length,bitrate from tunes");
$import_sth->execute;
$dbh->begin_work;
while (my @r = $import_sth->fetchrow_array) {