#!/bin/bash #Program to rip and encode CDs to MP3 #Copyright David Stark 2005 #This program is free software; you can redistribute it and/or modify #it under the terms of the GNU General Public License version 2 as published by #the Free Software Foundation. #This program 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 General Public License for more details: #http://www.gnu.org/copyleft/gpl.html case "$1" in -t) TYPE="track";; -d) TYPE="disc";; -h | --help) TYPE="help";; *) TYPE="bogus";; esac if [ $TYPE == "help" ] then echo "Usage: {-t|-d} " echo " -t will rip (and encode) as one file per track." echo " -d will rip (and encode) as one file per CD." echo " is the CD-ROM to be read from." echo " is the directory to write to." exit 0 fi #Check arguments if [ $TYPE == "bogus" ] || [ $# -lt 3 ] then echo "Usage: {-t|-d} " echo " -t will rip (and encode) as one file per track." echo " -d will rip (and encode) as one file per CD." echo " is the CD-ROM to be read from." echo " is the directory to write to." exit 1 fi if ! [ -b "$2" ] then echo "$2 is not a valid block device." echo "Exiting." exit 1 fi #CD drive to read from SOURCE="$2" #Directory to write to WRITEDIR="$3" #Look for CD in drive if cdparanoia -d "$SOURCE" -Q 2>&1 | grep "Unable to open disc." then echo "Couldn't find any audio in $SOURCE" echo "Exiting." exit 1 fi #Trap to kill lame and cdparanoia on Ctrl-C trap "pkill -P $$ &> /dev/null; echo \"Exiting on user interrupt.\"; exit 1" SIGINT #Try to make the rip directory if ! [ -d "$WRITEDIR" ] then if ! mkdir "$WRITEDIR" &> /dev/null then echo "Couldn't create $WRITEDIR. Exiting" exit 1 fi fi cd "$WRITEDIR" #Make sure the directory is empty #This is probably a good precaution CONTENTS=`ls -A` if ! [ x"$CONTENTS" == "x" ] then echo "$WRITEDIR is not empty. Exiting" exit 1 fi NUMTRACKS=`cdparanoia -d "$SOURCE" -Q 2>&1 | awk '{print $1}' | grep '^[[:digit:]]' | tail -n 1 | cut -d'.' -f1` #Normal rip & encode, one file per track if [ $TYPE == "track" ] then #This loop creates a FIFO for each .wav to be written to #and attaches an instance of LAME IND=1 while [ $IND -le $NUMTRACKS ] do if [ $IND -lt 10 ] then REALIND=0$IND else REALIND=$IND fi mkfifo track$REALIND.cdda.wav lame --preset standard track$REALIND.cdda.wav track$REALIND.mp3 &> /dev/null & ((IND++)) done #Rip cdparanoia -d "$SOURCE" -w -B #Catch track 0 if [ -e track00.cdda.wav ] then echo "Encoding track 0" lame --preset standard track00.cdda.wav track00.mp3 &> /dev/null fi #Mixtape style rip & encode, one file per CD elif [ $TYPE == "disc" ] then mkfifo cdda.wav lame --preset standard cdda.wav cdda.mp3 &> /dev/null & cdparanoia -d "$SOURCE" -w ".0-$NUMTRACKS" #Programming error else echo "Error in program. Exiting." exit 1 fi #relax wait #Remove the pipes echo "Cleaning up." rm -f *.wav echo "Ripping and encoding completed" #There is no more