#!/usr/bin/perl -w # # burncd.pl - gets album name from command line # pulls songs from mp3 database # copies songs to /music/wavs/ # renames songs to 01.mp3, 02.mp3, etc. format # calls mpg123 to convert mp3 to wav # calls sox to convert wav to cdr # calls cdrecord to burn CD-R # cleans up the /music/wavs/ directory use strict; use vars qw/$DBI/; use DBI; use Getopt::Long; use File::Basename; my $startTime = `date`; my $DSN = "DBI:mysql:mp3"; my $host = "localhost"; my $user = "userid"; my $passwd = "seekrit"; my $WavDir = "/music/wavs"; my ($ALBUM, $ARTIST); GetOptions("album=s" => \$ALBUM, "artist=s"=> \$ARTIST); # connect to database my $dbh = DBI->connect($DSN, $user, $passwd); die "Cannot connect: $DBI:errstr\n" if (!($dbh)); my $drh = DBI->install_driver("mysql"); # $ALBUM is required input if (!($ALBUM)) { print "correct usage is: burncd \n"; print "where options equal:\n"; print "-album=\"album name\"\n"; print "-artist=\"artist name\"\n"; print "\n"; print "-artist is only necessary if there are two\n"; print "or more albums of the same name (i.e.,Greatest Hits)\n"; exit; } my $SQLText = "SELECT location FROM song, album WHERE fk_album_id = album.id AND album = \"$ALBUM\" ORDER BY location"; if ($ARTIST) { $SQLText = "SELECT location FROM song, album, artist WHERE fk_artist_id = artist.id AND fk_album_id = album.id AND album = \"$ALBUM\" AND performer = \"$ARTIST\" ORDER BY location"; } # this is what we're executing print "SQL: $SQLText\n"; my $sth = $dbh->prepare($SQLText); my $query = $sth->execute; my $FilesFound = "0"; my @list; # copy files from $Loc to $WavDir while (my $ref = $sth->fetchrow_hashref()) { $FilesFound = '1'; last if (!($FilesFound)); my $Loc = $ref->{'location'}; print "copying $Loc to $WavDir\n"; my @args = ("cp", "$Loc", "$WavDir"); system(@args) == 0 or die "system @args failed: $?"; my ($filename,$pathname,$suffixname) = fileparse($Loc,"\.[^.]*"); $filename = $filename . $suffixname; push @list, $filename; } if (!($FilesFound)) { print "No entries found for $ALBUM $ARTIST\n"; exit; } # set working dir to $WavDir chdir($WavDir); # rename files to 01.mp3, 02.mp3, etc. my @mp3list = process_files("mv", undef, "renaming", "\.mp3", @list); # call mpg123 to convert .mp3 to .wav my @wavlist = process_files("mpg123", "-qw", "converting", "\.wav", @mp3list); # call sox to convert each .wav to .cdr my @cdrlist = process_files("sox", undef, "converting", "\.cdr", @wavlist); # call cdrecord on *.cdr my @args = ("cdrecord -v dev=0,4,0 -audio *.cdr"); system(@args) == 0 or die "system @args failed: $?"; # clean up print "cleaning up $WavDir\n"; unlink @list; unlink @mp3list; unlink @wavlist; unlink @cdrlist; # done print "processing for $ALBUM complete\n"; my $endTime = `date`; print "start time: $startTime\n"; print "end time : $endTime\n"; exit; sub process_files { my ($function, $parm, $message, $extension, @list) = @_; my @returnlist; my @args; foreach my $file (@list) { my $Newfile = substr($file, 0, 2); $Newfile = $Newfile . $extension; print "($function) $message $file to $Newfile\n"; if ($function =~ /mpg123/) { @args = ("$function", "$parm", "$Newfile", "$file"); } else { @args = ("$function", "$file", "$Newfile"); } system(@args) == 0 or die "system @args failed: $?"; push @returnlist, $Newfile; } return(@returnlist); }