#!/usr/bin/perl # Copyright (C) 2007 Eric B Shobe ############################################################################### #This library is free software; you can redistribute it and/or #modify it under the terms of the GNU Lesser General Public #License as published by the Free Software Foundation; either #version 2.1 of the License, or (at your option) any later version. # #This library is distributed in the hope that it will be useful, #but WITHOUT ANY WARRANTY; without even the implied warranty of #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #Lesser General Public License for more details. # #You should have received a copy of the GNU Lesser General Public #License along with this library; if not, write to the Free Software #Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ################################################################################ use Getopt::Long; GetOptions ( 'lame|l=s' => \$lame, 'cdda2wav|c=s' => \$cdda, 'temp|t=s' => \$dir, 'device|d=s'=> \$device1, 'vbr' => \$variable, 'bitrate|b=s' => \$bvariable, 'help|h' => \$help, 'version|v' => \$version ); if($help){ print "sunGrab: A tool to convert your cd's to mp3's\n"; print " Usage: sunGrab [OPTIONS]\n"; print "\n"; print " Command line OPTIONS:\n"; print " -l, --lame path to lame codec\n"; print " -c, --cdda2 wavpath to cdda2wav binary\n"; print " -t, --temp directory to store mp3's\n"; print " -vbr choose this to use Variable encoding (I prefer)\n"; print " -b, --bitrate bitrate (options are 96 112 128 160 192 224 256 320)\n"; print " -d, --device device to rip from (use cdda2wav --scanbus to find (1,0,0)\n"; print " -h, --help display this help and exit\n"; print " -v, --version display this version and exit\n"; print "\n"; print " Examples:\n"; print " sunGrab -l=/opt/csw/bin/lame -c=/opt/csw/bin/cdda2wav -t=/tmp -d=1,0,0 -bitrate=320\n"; print " sunGrab -l=/opt/csw/bin/lame -c=/opt/csw/bin/cdda2wav -t=/tmp -d=1,0,0 -vbr\n"; print " sunGrab -- NOTE*- any option can be left blank, and it will prompt you and autodetect in most cases\n"; exit 0; } if($version){ print "sunGrab: Version 1.0.0\n"; exit 0; } $encode; $pathCdda = pathInteractiveCdda(); $pathLame = pathInteractiveLame(); $pathStore = getPath(); if(!$device1){ while(cdInteractive() ne "yes"){}; } else{ $device = $device1; } while(getEncoding() ne "yes"){} chdir $pathStore; print "Starting CD-to-WAV conversion\n"; print `$pathCdda -B -O wav -L 1 -D $device`; print "Finished CD-to-WAV conversion\n"; #### START BURNING, THE MAIN GUTS############ opendir (DH, "."); my @songList = grep /.*\.inf/ , readdir DH; closedir DH; #print @subdir; $artist1; $title1; foreach $song(@songList){ open FILE, "<$pathStore/$song"; while () { if (/^Albumperformer=\s+'(.+)'\s*$/) { $artist = $1; $artist1 = $1; } elsif (/^Albumtitle=\s+'(.+)'\s*$/) { $title = $1; $title1 = $1; } elsif (/^Tracktitle=\s+'(.+)'\s*$/) { $track = $1; print " $track"; } elsif (/^Tracknumber=\s+'?(\d+)'?\s*$/) { $number = $1; print " $number\n"; } } close FILE; if ($artist eq "" && $artist1 eq "") { print "Unable to determine artist name\n"; print "Enter the name of the ARTIST if you wish: "; $artist1 = ; chop($artist1); $artist = "unknown"; } if ($title eq "" && $title1 eq "") { print "Unable to determine album title\n"; print "Enter the name of the ALBUM if you wish: "; $title1 = ; chop($title1); $title = "unknown"; } if ($track eq "") { print "Unable to determine track title for track $number\n"; print "Enter the TRACK TITLE if you wish: "; $track = ; chop ($track); if($track eq ""){ $track = "unknown"; } } if ($number eq "") { print "Unable to determine track number\n"; print "Enter the TRACK NUMBER if you wish: "; $number = ; chop ($number); if($number eq ""){ $number = 0; } } mkdir "$pathStore/$artist1" if (!-e "$pwd/$artist1"); chdir "$pathStore/$artist1"; mkdir "$pathStore/$artist1/$title1" if (!-e "$pathStore/$artist/$title1"); chdir "$pathStore/$artist1/$title1"; $pwd = "$pathStore/$artist1/$title1"; $rip = "$number-$track.mp3"; $rip =~ s/\//|/g; if ($tracknumOPT) { $rip = "$number-$rip"; } @wavs = split("\\.", $song); $wav = "$wavs[0].wav"; print "Writing \"$rip\"\n"; print "$pathLame $encode -h --ta \"$artist1\" --tl \"$title1\" --tt \"$track\" --tn $number $pathStore/$wav \"$pwd/$rip\""; print `$pathLame $encode -h --ta "$artist1" --tl "$title1" --tt "$track" --tn $number $pathStore/$wav "$pwd/$rip"`; $track = ""; $number = ""; `rm $pathStore/$wav`; `rm $pathStore/$song`; } print "Congratulations, Everything converted...enjoy $pwd\n"; `rm $pathStore/audio.cddb`; `rm $pathStore/audio.cdindex`; `eject cdrom`; sub cdInteractive{ print "Choose your cd player to rip from\n"; print `$pathCdda --scanbus`; print "Enter the device path of the cd-player(i.e. 1,0,0): "; $dev = ; chop ($dev); print "Testing device path.......\n"; print `$pathCdda -G -D $dev -J -L 0`; print "Does this look correct? (yes or no or quit): "; $reply = ; chop($reply); if($reply eq "quit" || $reply eq "q"){ exit 0; } if ($reply eq "yes" || $reply eq "y"){ $device = $dev; return "yes"; } } sub pathInteractiveCdda{ if (-e $cdda){ print "Found cdda2wav at $cdda\n"; return $cdda; } print "Attempting to find path cdda2wav\n"; if (-e "/usr/sbin/cdda2wav"){ print "Found cdda2wav at /usr/sbin/cdda2wav: is this correct? (yes or no): "; $reply = ; chop($reply); if ($reply eq "yes" || $reply eq "y"){ return "/usr/sbin/cdda2wav"; } } if (-e "/usr/bin/cdda2wav"){ print "Found cdda2wav at /usr/bin/cdda2wav: is this correct? (yes or no): "; $reply = ; chop($reply); if ($reply eq "yes" || $reply eq "y"){ return "/usr/bin/cdda2wav"; } } if (-e "/opt/csw/bin/cdda2wav"){ print "Found cdda2wav at /opt/csw/bin/cdda2wav: is this correct? (yes or no): "; $reply = ; chop($reply); if ($reply eq "yes" || $reply eq "y"){ return "/opt/csw/bin/cdda2wav"; } } print "Can not find cdda2wav: enter the path at the command line w/ switch -cdda2wav or -c"; exit 0; } sub pathInteractiveLame{ if(-e $lame){ print "Found lame at $lame\n"; return $lame; } print "Attempting to find path Lame\n"; if (-e "/usr/sbin/lame"){ print "Found lame at /usr/sbin/lame is this correct? (yes or no): "; $reply = ; chop($reply); if ($reply eq "yes" || $reply eq "y"){ return "/usr/sbin/lame"; } } if (-e "/usr/bin/lame"){ print "Found lame at /usr/bin/lame: is this correct? (yes or no): "; $reply = ; chop($reply); if ($reply eq "yes" || $reply eq "y"){ return "/usr/bin/lame"; } } if (-e "/opt/csw/bin/lame"){ print "Found lame at /opt/csw/bin/lame: is this correct? (yes or no): "; $reply = ; chop($reply); if ($reply eq "yes" || $reply eq "y"){ return "/opt/csw/bin/lame"; } } print "Can not find lame: enter the path at the command line w/ switch -lame or -l"; exit 0; } sub getPath(){ if($dir){ print "Path to store mp3's is $dir (Artist and Albulm name will be created for you): \n"; $path = $dir; } else{ print "Enter root path to store mp3's (Artist and Albulm name will be created for you): "; $path = ; chop($path); } if (!-e $path) { print "$path does not exist, do you want to create it: (yes or no): "; $reply = ; chop($reply); if ($reply eq "yes" || $reply eq "y"){ mkdir $path || die "Unable to create $path\n"; if(-e $path){ print "Created $path\n"; } } } return $path; } sub getEncoding{ if($variable){ print "Variable bitrate will be used\n"; $encode = "-V 3"; return yes; } if ($bvariable =~ /96|112|128|160|192|224|256|320/){ print "Constant bitrate will be used at $bvariable kbps\n"; $encode = "-b $bvariable"; return "yes"; } else{ if($bvariable){ print "No valid bitrate was entered\n"; exit 0; } } print "Specify a (v) Variable Bit Rate or (b) for constant bitrate or press enter to use default (b - 128kps): "; $encoding = ; chop($encoding); if($encoding eq "v" || $encoding eq "b" || $encoding eq "V" || $encoding eq "B"){ if($encoding eq "v" || $encoding eq "V"){ print "Variable encoding will be used, good choice\n"; $encode = "-V 3"; return "yes"; } if($encoding eq "b" || $encoding eq "B"){ print "Constant Bitrate was chosen, what bitrate do you wish\n"; print "Enter one of the following 96 112 128 160 192 224 256 320 kbps: "; $encoding = ; chop($encoding); if ($encoding =~ /96|112|128|160|192|224|256|320/){ print "You entered a valid choice"; $encode = "-b $encoding"; return "yes"; } else{ print "No valid bitrate was entered\n"; return "no"; } } } else{ return "no"; } }