#!/usr/bin/perl -w # Rip a book (voice) on CD to low-rate MP3 files #################### set system specifics here #$dev = "/dev/hdb"; $dev = "/dev/sg0"; $tmpDir = "/tmp"; ############################################### &parseArgs(); $count = &countTracks($dev,$tmpDir); # count tracks on CD (-d &safeFileName($author)) or mkdir (&safeFileName($author)); (-d &safeFileName($author)."/".&safeFileName($book)) or mkdir (&safeFileName($author)."/".&safeFileName($book)); for($track=1;$track<=$count;$track++) { $fNam = sprintf("%s/%s/d%02dt%02d.mp3", &safeFileName($author), &safeFileName($book),$diskNo,$track); $cmd = sprintf( "cdparanoia %s -d $dev %d -",$paranoia,$track); # sox takes 60% of my CPU with paranoia disabled when downsampling to stereo. # only 40% when downsampling to mono. Seems more efficient to let sox # convert to 16kHz mono $cmd .= " | nice -19 sox -t wav -"; $cmd .= " -v $volume" if ($volume != 1); $cmd .= " -t wav -r 16000 -c 1 - | nice -19 lame -b $bitRate"; $cmd = sprintf("%s --tt \'Disk %d, Track %d\' --ta \'$author\' --tl \'$book\' --tn %d", $cmd,$diskNo,$track,$track); $cmd .= " --ty $year" if ($year > 0); $cmd .= " --tc \'$comment\'" if ($comment ne ""); $cmd .= " --tg 101"; # speech $cmd .= " - $fNam"; print `date`; print "$cmd\n"; sleep $sleep if ($sleep > 0); system($cmd); chmod 0666,$fNam; } print `date`," ******** Done.\n"; #--------------------------- Count tracks on CD sub countTracks { local ($dev,$tmpDir) = @_; local $tocFile = "$tmpDir/cdda.toc"; # write table of contents to temp file $_ = "cdparanoia -Qd $dev 2> $tocFile"; print "$_\n"; system $_; chmod 0666,$tocFile; #print `cat $tocFile`; open(TOC,$tocFile) or die "Could not open TOC\n"; local $count = 0; while() { print; $count++ if ( m/^ +[0-9]+\. +/ ); } close TOC; unlink $tocFile; print "$dev has $count tracks.\n"; return($count); } sub parseArgs { ($#ARGV > 1) or die "\nUsage: $0 author title [-y year] [-c comment] \t\t[-Z] [-v volume] [-r bitRate] diskNo\n \tUse -Z to disaable paranoia on rip (paranoia is safe but slow) \tDefault volume is 1.0 (no change). Many disks do better with some gain. \tDefault bitrate is 32 (kbps). \t\tMany people find 24 (kbps) acceptable for speech\n\n"; $author = $ARGV[0]; $book = $ARGV[1]; $diskNo = $ARGV[$#ARGV]; $year = -1; $comment = ""; $volume = 1; $bitRate = 32; $paranoia = ""; $sleep = 0; # undocumented pause before starting each track local $i; for ($i = 2; $i < $#ARGV; $i++) { local $arg = $ARGV[$i]; # i hate typing this if ($arg eq "-Z") { $paranoia = "-Z"; } elsif ($arg eq "-v") { $volume = $ARGV[++$i]; } elsif ($arg eq "-r") { $bitRate = $ARGV[++$i]; } elsif ($arg eq "-y") { $year = $ARGV[++$i]; } elsif ($arg eq "-c") { $comment = $ARGV[++$i]; } elsif ($arg eq "-s") { $sleep = $ARGV[++$i]; } else { die "\n\nUnrecognized argument $arg\n\n"; } } &echoArgs; } sub echoArgs { local $fmt = "%-24s : %s\n"; printf $fmt,"Author",$author; printf $fmt,"Book",$book; printf $fmt,"Disk",$diskNo; printf $fmt,"Year",$year if ($year > 100); printf $fmt,"Comment",$comment if ($comment); printf $fmt,"Gain",$volume; printf $fmt,"Bit Rate (kbps)",$bitRate; printf $fmt,"Paranoia",$paranoia; print "\n"; } # make substitutions in a string to create something more appropriate # for a UNIX filename sub safeFileName{ ($_) = @_; chomp; s/\s/_/g; # replace white space with underscores s/['";:\[\]\(\)\*&#`\/]/~/g; # replace odd characters with ~ return $_; } __END__