Attention: open in a new window. PDFPrint

How to rip CDs on your terminal including CDDB information

As I decided to rip all my CDs I own to MP3 files to be able to carry them along on my mobile (and may be to hear them) I used a way to rip them on my command line environment. I did not want to use any of thees graphical userinterfaces as I just wanted to convert a whole bunch of CDs. So I searched for I way I just have to put in a CD and wait until it is thrown out to put in the next one. So I started searching the web for a tool doing exactly this and I found out, that I could simply use cdda2wav to rip the CD into wave files and download the CDDB information at the same time without caring about theese fancy CD identification hashes on my own.

The command line arguments I used to read the CDDB information while ripping the CD to wave files is

 

 

 

cdda2wav -L 0 -B -D /dev/sr0

The -L 0 command enabled the CDDB queries and the -D /dev/sr0 defines the input device, wich is located at /dev/sr0 in my case. The -B option just forces cdda2wav to read all tracks from the device into wave files by using a single file for each track.

Afterwards you will find some wave files named like audio_01.wav, audio_02.wav (and so on...) and some .inf files in the directory. The .inf files contain the CDDB information of the tracks. Theese files use some kind of ".inf"-file format (what a surprise) and can be read using a simple shell script. So I have build a simple script that reads out the CDDB information and starts lame to convert the wave file into an mp3 and use id3tool to write the ID3-tags to the mp3file.

Here is the full script I wrote:

#!/bin/bash

DEVICE=
BITRATE=192

TMPPWD=$(pwd)
TMPDIR="$TMPPWD/tmp"
mkdir -p "$TMPDIR" 2>&1 > /dev/null
cd "$TMPDIR"

function printusage {
echo "Usage: $0 [OPTIONS]"
echo " OPTIONS:"
echo " -d <devicenode> cdrom device to use"
echo " -b <bitrate> changes the lame bitrate"
}

#function performs a cddb request to the
#inf file associated with the given track number
function cddb_query {
TRACK=$1
PROPERTY=$2

if [ ${#TRACK} -le 1 ]; then
TRACK="0$TRACK"
fi

trackfile="tmp/audio_${TRACK}.inf"
VALUE=$(awk "/^$PROPERTY/ {\$1=\"\"; print \$0; next;}" $trackfile)
echo "$VALUE" | sed -e "s/^ //g" -e "s/'//g" -e "s/\//-/g"
#eval "echo $VALUE"
}

while getopts d:b: o
do
case "$o" in
d)
EDEVICE="$OPTARG"
DEVICE="-D $EDEVICE"
echo "using $DEVICE for cdda2wav"
;;
b)
BITRATE="$OPTARG"
echo "using -b $BITRATE for lame"
;;
*)
printusage
exit 1
;;
esac
done

cdda2wav -L 0 -B -g $DEVICE
eject $EDEVICE

cd "$TMPPWD"

if [ $? -ne 0 ]; then
exit 1
fi

for I in $(find "tmp" -name "*.inf"); do
TRACK="$(echo $I | grep -Eoe "[0-9][0-9]")"
AFILE="${I%%.inf}.wav"

Albumperformer="$(cddb_query $TRACK Albumperformer)"
Performer="$(cddb_query $TRACK Performer)"
if [ "$Performer" != "" ]; then
Performer="$Albumperformer"
fi

Albumtitle="$(cddb_query $TRACK Albumtitle)"
Tracknumber="$(cddb_query $TRACK Tracknumber)"
Tracktitle="$(cddb_query $TRACK Tracktitle)"


MFILE="$TRACK ${Albumtitle} - ${Tracktitle}.mp3"
ODIR="$TMPPWD/$Albumperformer - $Albumtitle"
echo "Converting $AFILE to \"$MFILE\" using ID3: "
echo -e "\tAlbum: $Albumtitle"
echo -e "\tArtist: $Performer"
echo -e "\tTrack: $Tracknumber"
echo -e "\tTitle: $Tracktitle"
echo -e ""
echo -e "to \"$ODIR\""
echo -e ""

mkdir -p "$ODIR"
MFILE="$ODIR/$MFILE"

lame -b "$BITRATE" "$AFILE" "$MFILE"
id3tool -t "$Tracktitle" -a "$Albumtitle" -r "$Performer" -c "$Tracknumber" "$MFILE"

echo "Remove .wav and cdda information"
rm -v "$AFILE"
rm -v "$I"
done

echo "Remove cddb index"
rm -v audio.cddb audio.cdindex



As always: I cannot give any warrenty, that it works on your system and the script is only a simple draft. The script might not run on your system or might even damage your system.

The CD is ejected after cdda2wav finished. So running an other instance while lame is converting your stuff might speed up your conversion work as cdda2wav mostly uses io ressources and lame cpu ressources.