[ale] Perl + CDDB
Byron A Jeff
byron at cc.gatech.edu
Thu Feb 10 16:33:10 EST 2005
On Thu, Feb 10, 2005 at 04:02:29PM -0500, Christopher Fowler wrote:
>
> I'm starting to write some programs to automate converting my massive
> CD collection into MP3s. I see there is a CDDB module but it does not
> seem to read the info from the CD. Anyone have this figured out? I could use
> mp3c to create a script but I want more control.
Chris,
I've done it. But I can't tell you what if any changes I made to the perl
CDDB_Get to make it work.
It's three relatively small scripts. Requires that cdparanoia, lame, and
the CDDB_Get perl module be installed. It will ID a CD, rip the wav files off
the disk one at a time and encode them.
the dumpcddb has been slightly modified by me. I think this is the first time
I've ever relased it.
I'll just post them here:
----------------- mp3dump -------------------------------
#!/bin/sh
if [ "$1" ] ; then
if [ $1 = - ] ; then
maindir=/thrasher
else
maindir=$1
fi
else
maindir=/thrasher
fi
cd $maindir
mkcddir
newdir=`head -1 /tmp/.cdlisting`
echo Created directory $maindir/$newdir
cd $newdir
if [ "$2" ] ; then
first=`echo $2 | cut -d- -f1`
last=`echo $2 | cut -d- -f2`
first=$[first+1]
last=$[last-first+2]
else
first=2
last=`wc -l MANIFEST | cut -c1-7 | tr -d ' '`
fi
for i in `tail +$first MANIFEST | head -$last` ; do
echo " Reading $i"
tracknum=`echo $i | cut -c1-2`
time cdparanoia -B $tracknum
echo " Converting $i"
mv track*.wav $i
lame $i
rm $i
done
eject
touch COMPLETED
--------------------------mkcddir-------------------------
#!/bin/sh
dumpcddb | tr 'A-Z' 'a-z' | tr -d '()\054' | tr -c 'a-z0-9.\n' '_' > /tmp/.cdlisting
newdir=`head -1 /tmp/.cdlisting`
if [ -z "$newdir" ] ; then
echo "No CDDB entry found for CD."
echo -n "Enter <artist>.<title>:"
read newdir
tracks=$(grep total /tmp/cddb/numtracks | tr -d ';' | cut -d'=' -f2)
echo $newdir > /tmp/.cdlisting
track=1
while [ $track -le $tracks ] ;do
printf "%02d_track%02d\n" $track $track >> /tmp/.cdlisting
track=$[track+1]
done
fi
mkdir $newdir
cd $newdir
cp /tmp/.cdlisting MANIFEST
echo $newdir
------------------------------- dumpcddb -------------------------
#!/usr/bin/perl -I.
#
# CDDB - Read the CDDB entry for an audio CD in your drive
#
# This module/script gets the CDDB info for an audio cd. You need
# LINUX, a cdrom drive and an active internet connection in order
# to do that.
#
# (c) 2000 Armin Obersteiner <armin at xos.net>
#
# LICENSE
#
# This library is released under the same conditions as Perl, that
# is, either of the following:
#
# a) the GNU General Public License as published by the Free
# Software Foundation; either version 1, or (at your option) any
# later version.
#
# b) the Artistic License.
#
use CDDB_get;
use Data::Dumper;
use strict vars;
use Getopt::Std;
my %option = ();
getopts("oghdt", \%option);
if($option{h}) {
print "$0: gets CDDB info of a CD\n";
print " no argument - gets CDDB info of CD in your drive\n";
print " -o offline mode - just stores CD info\n";
print " -d output in xmcd format\n";
print " -t output toc\n";
print " -g get CCDB info for stored CDs\n";
exit;
}
my %config;
my $diskid;
my $total;
my $toc;
my $savedir="/tmp/cddb";
# following variables just need to be declared if different from defaults
$config{CDDB_HOST}="freedb.freedb.org"; # set cddb host
$config{CDDB_PORT}=888; # set cddb port
$config{CD_DEVICE}="/dev/cdrom"; # set cd device
# user interaction welcome?
$config{input}=0; # 1: ask user if more than one possibility
# 0: no user interaction
if($option{o}) {
my $ids=CDDB_get::get_discids($config{CD_DEVICE});
unless(-e $savedir) {
mkdir $savedir,0755 || die "cannot create $savedir";
}
open OUT,">$savedir/numtracks" || die "cannot open outfile";
print OUT Data::Dumper->Dump($ids,["diskid","total","toc"]);
close OUT;
exit;
}
if($option{g}) {
print STDERR "retrieving stored cds ...\n";
opendir(DIR, $savedir) || die "cannot opendir $savedir";
while (defined(my $file = readdir(DIR))) {
next if($file =~ /^\./);
print "\n";
my $in=`/bin/cat $savedir/$file`;
my $exit = $? >> 8;
if($exit>0) {
die "error reading file";
}
unlink "$savedir/$file";
eval $in;
my %cd=get_cddb(\%config,[$diskid,$total,$toc]);
unless(defined $cd{title}) {
print "no cddb entry found: $savedir/$file\n";
}
if($option{d}) {
print_cover(\%cd);
} else {
print_cd(\%cd);
}
}
closedir(DIR);
exit;
}
# get it on
my %cd=CDDB_get::get_cddb(\%config);
unless(defined $cd{title}) {
die "no cddb entry found";
}
# do somthing with the results
if($option{d}) {
print_xmcd(\%cd);
} else {
print_cd(\%cd);
}
exit;
# subroutines
sub print_cd {
my $cd=shift;
print "$cd->{artist}.";
print "$cd->{title}\n";
my $n=1;
foreach my $i ( @{$cd->{track}} ) {
if($option{t}) {
my $out=sprintf "track %2d: %8d - %8d: $i\n",$n,$cd->{frames}[$n-1],$cd->{frames}[$n]-1;
print "$out";
} else {
printf "%02d_%s\n",$n,$i;
}
$n++;
}
}
sub print_xmcd {
my $cd=shift;
for(@{$cd->{raw}}) {
print "$_";
}
}
>
>
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://www.ale.org/mailman/listinfo/ale
More information about the Ale
mailing list