Change udb to use Music::Tag instead of obsolete Audio::TagLib master
authorChip Black <bytex64@bytex64.net>
Mon, 14 Dec 2009 01:03:30 +0000 (19:03 -0600)
committerChip Black <bytex64@bytex64.net>
Mon, 14 Dec 2009 01:03:30 +0000 (19:03 -0600)
udb.pl

diff --git a/udb.pl b/udb.pl
index 3f0fbbd..6271997 100755 (executable)
--- a/udb.pl
+++ b/udb.pl
@@ -3,15 +3,20 @@ use strict;
 
 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";
@@ -26,9 +31,7 @@ if (!$rv) {
                track integer,
                year integer,
                length integer,
-               bitrate integer,
-               samplerate integer,
-               channels integer
+               bitrate integer
        )});
 }
 
@@ -104,33 +107,28 @@ sub db_add {
                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"/;
@@ -139,7 +137,7 @@ sub db_add {
        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;
@@ -189,8 +187,6 @@ sub db_info {
        }
        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";
 }
 
@@ -301,7 +297,7 @@ if ($ARGV[0] eq 'add') {
        }
        $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()) {
@@ -309,7 +305,8 @@ if ($ARGV[0] eq 'add') {
                    ! (-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') {
@@ -347,10 +344,10 @@ if ($ARGV[0] eq 'add') {
                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) {