Red Hat
May 3, 2012
by Trustin Lee

I wrote a pretty simple shell script that rips an audio CD as a single FLAC file with embedded cuesheet for Linux. It ensures secure rip by ripping a disc three times with cdparanoia and comparing their checksums. It has been tested on Ubuntu 12.04 Precise, but it should work for any Linux system such as Fedora.

Usage

Just run the script in your shell prompt, it will retrieve the ID of the disc and generate a <DISCID>.flac

Required packages

Install the following packages first:

apt-get install cd-discid cdrdao cuetools flac cdparanoia

Install the ripper script

Put the following script into your path (e.g. /usr/local/bin). You might want to change the DEVICE variable (e.g. /dev/cdrom). If you want even more secure rip, you can set the COPIES variable to a greater value (e.g. 5).

#!/bin/bash
# /usr/local/bin/rip-audio-cd
DEVICE="/dev/sr0"
COPIES="3"

DISCID="`cd-discid $DEVICE 2>&1 | egrep -o '^[0-9a-z]{8}'`"
if [ "$?" != "0" ]; then
  echo "Failed to retrieve disc ID."
  exit 1
fi

echo -e "\033[1;32mDisc ID: $DISCID\033[0;0m"
rm -f "$DISCID".*

echo -e "\033[1;32mExtracting a cue sheet:\033[0;0m"

cdrdao read-toc --device "$DEVICE" --datafile "$DISCID.wav" "$DISCID.toc" || exit 2
cueconvert -i toc -o cue "$DISCID.toc" | grep -vP '^ISRC "' > "$DISCID.cue" || exit 2

CHECKSUM=''
I=0
for ((I=0; I < $COPIES; I++)); do

  echo
  echo -e "\033[1;32mPass $((I+1)) of $COPIES\033[0;0m:"

  if [[ $I -eq 0 ]]; then
    OUT="$DISCID.wav"
  else
    OUT="$DISCID.new.wav"
  fi
  rm -f "$OUT"

  cdparanoia -zX '1-' "$OUT"
  if [ "$?" != "0" ]; then
    rm -f "$DISCID".*
    echo "Failed to rip a disc."
    exit 3
  fi

  C="`sha1sum "$OUT" | cut -f1 -d' '`"
  if [[ "x$CHECKSUM" = 'x' ]]; then
    echo "Checksum: $C"
    CHECKSUM=$C
  else
    rm -f "$OUT"
    if [[ "$CHECKSUM" != "$C" ]]; then
      echo "Mismatching checksum: $C"
      exit 4
    else
      echo "Matching checksum: $C"
    fi
  fi
done

eject "$DEVICE" &

echo
echo -en "\033[1;32mCompressing...\033[0;0m"
flac -f -V --replay-gain --best --cuesheet="$DISCID.cue" "$DISCID.wav"
if [ "$?" != "0" ]; then
  echo "Failed to encode the ripped tracks."
  exit 5
fi

rm -f "$DISCID.wav" "$DISCID.cue" "$DISCID.toc"

echo
echo -e "\033[1;32mAll done: $DISCID.flac\033[0;0m"

Fill metadata

Unfortunately, the script does not fill the metadata such as track information and cover art. I usually rip a bunch of discs first using this script and then fill their metadata in Windows. Here’s the steps I take:

  1. Load the FLAC file into foobar2000
  2. Fill the track information.
    • Select all tracks, right-click them, and choose ‘Tagging -> Get tags from freedb’.
    • If not found from freedb, you can always fill manually by choosing ‘Properties’.
  3. Update ReplayGain information.
    • Select all tracks, right-click them, and choose ‘ReplayGain -> Scan selection as a single album’.
  4. Download album arts (front cover, back cover, disc) using Album Art Downloader XUI
  5. Attach the downloaded album arts into the FLAC file using foobar2000.
    • Select all tracks, right-click then, and then choose ‘Tagging -> Attach pictures’.