Skip to content

Instantly share code, notes, and snippets.

@patrickmoffitt
Created October 8, 2017 21:52
Show Gist options
  • Save patrickmoffitt/1b671d0645011fac93368e81dc048e8f to your computer and use it in GitHub Desktop.
Save patrickmoffitt/1b671d0645011fac93368e81dc048e8f to your computer and use it in GitHub Desktop.
Script to losslessly copy all the tracks on an audio CD to a folder named the same as the album. Requires Mac OS X, a cdrom drive and cdparanoia.
#!/bin/bash
# Script to losslessly copy all the tracks on an audio CD to a folder
# named the same as the album.
#
# System Requirements: Mac OS X, cdrom drive, cdparanoia.
#
# Patrick Moffitt October 8, 2017.
#
CDPARANOIA=$(which cdparanoia)
if [ "$CDPARANOIA" == "" ]; then
echo "cdparanoia not found. Try $ brew install cdparanoia"
exit 127
fi
DEVICE="$(drutil status | sed -En 's/(.*)(\/dev\/disk[0-9])$/\2/p')"
if [ "$DEVICE" == "" ]; then
echo "No CDROM compatable device found. Insert disk (close tray) and retry."
exit 6
fi
ALBUM="$(mount | grep disk2 | sed -En "s/^${DEVICE//\//\\/}.*(Volumes\/)(.*)( \(.*$)/\2/p")"
if [ "$ALBUM" == "" ]; then
echo "The CDROM is not mounted. Wait for the system to mount it and retry."
exit 2
fi
TRACKS="$(ls -1 "/Volumes/${ALBUM}" | sort -g)"
DIR="$(basename "$PWD")"
if [ "$DIR" != "$ALBUM" ]; then
if [ ! -d "$ALBUM" ]; then
mkdir "$ALBUM"
fi
cd "$ALBUM" || exit 2
fi
while read -r TRACK; do
TRACK_NUM=$(echo "$TRACK" | sed -En 's/^([0-9]{1,2})(.*$)/\1/p')
TRACK_NAME="$(echo "$TRACK" | sed -En 's/^([0-9]{1,2}.*)(\..*$)/\1/p')"
cdparanoia "$TRACK_NUM" "${TRACK_NAME}.wav"
done <<< "$TRACKS"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment