Config and scripts for automatically ripping audio CDs.
These scripts were assembled on a Raspberry Pi Zero W in 2017.
Once installed, they're designed to recognize when a CD is inserted and subsequently rip the audio CD. After ripping the CD, the scripts copy the files to Dropbox and trigger a Zapier webhook. You can easily edit the scripts to run whatever you want when the ripping is complete.
I will happily accept pull requests for changes and improvements and appreciate any input and suggestions. I'm not planning to build this beyond my personal needs but I hope it will be a useful resource for other people with similar goals.
-
/etc/udev/rules.d/99-cd-audio-processing.rules
This script tells udev what to do when a disc is inserted into the CD drive (sr0
here). This rule tells udev to run a systemd service.Call
udevadm control --reload
to force udev to load this rule for the drive without a reboot.We have to use a service instead of running the script directly because udev kills long running scripts. And we have to call
/bin/systemctl
instead of usingSYSTEMD_WANTS
because change events don't do theSYSTEMD_WANTS
tasks. -
/etc/systemd/system/rip-audio-cd@service
This just wraps the actual CD ripping script in a systemd service. -
/usr/local/sbin/rip-audio-cd.sh
This script handles ripping and ejecting the CD, and prevents the script from running multiple times if there are multiple events. (I don't know if the locking is necessary with the udev setup. It's something I found in tutorials and kept.)The real work is being done by abcde, a command line ripping program. The script redirects the log and errors to
/var/log/cdrip.log
. If abcde throws an error, this script makes sure to call eject. -
/etc/abcde.conf
This is the configuration file for abcde. You can pass in flags but it's better (and standard) to set things up here. My configuration is set to rip files and encode them as both flac and mp3. It then puts the encoded files in/srv/ripped-music/flac
and/srv/ripped-music/mp3
.The
post_encode()
function sets up the rest of the work. It logs the artist and album name in/srv/ripped-music/last-rip.log
for use by the remaining scripts, and then it runs the scripts. -
/usr/local/bin/upload-to-dropbox.sh
This script uses the Dropbox REST API andcurl
to add the files to Dropbox. (You may need toapt-get install curl
.)You'll need a Dropbox bearer token for authentication. I created a Dropbox API app for myself (which only has access to its own folder in Dropbox), and then copied the bearer token from the API website. Just replace the
**DROPBOX_BEARER_TOKEN**
in the script with your actual token. -
/usr/local/bin/add-to-airtable.sh
This script would be more aptly namedtrigger-zapier.sh
. It posts the artist and album as a JSON object to a Zapier webhook. The webhook is setup to add a row to an AirTable table. (Another Zapier trigger sends me an email when that new row is added.)You can create a new webhook trigger on the Zapier website. Just replace the
**ZAPIER_WEBHOOK**
in the script with the url for the webhook.
-
You'll need to install abcde and eject. You can use
sudo apt-get -y install abcde eject
. If you're encoding to flac and mp3 you'll also want to install lame and flac. -
If you're testing this stuff while logged in (i.e., if you're running abcde or eject from the command line, you may need to add yourself to the
cdrom
group. eject didn't work for me withoutsudo
otherwise.useradd -G cdrom $USERNAME
-
You might need to create the
/srv/ripped-music
directory where the files are saved, and you'll want to give permissions to thecdrom
group so that you can write the files if you call abcde manually. -
At this point, just copy the files to their respective places in the filesystem. That should be it.
Here's how I diagnose:
-
Check the log file
/var/log/cdrip.log
-
Check the abcde
status
anderrors
files.abcde creates a temp folder in the root directory,
/abcde.???
(where???
is just to say that whatever follows the.
is random). Usually that directory will contain astatus
file and sometimes anerrors
file with details.Sometimes cdparanoia, which abcde uses to rip the audio, throws errors when reading, which shuts down the whole script. I've had this happen on perfect, just opened CDs. I'm not sure how to fix this. Sometimes you can just put the CD back in and abcde will pick up where it left off. Some CDs just seem likethey can't move forward.
The annoying part of these errors is that the rest of the scripts still run, so it still gets uploaded to AirTable and the subset of songs that have completed are uploaded to Dropbox. As a result, I end up looking at the upload folder and manually checking to make sure the songs are there. It'd be nice to have an automated way to detect errors and flag them.
Sometimes the scripts don't run on insertion. Here's what I do:
eject
the CD and put it back in.- Check the status of the systemd service with
systemctl status [email protected]
- Restart the service with
sudo systemctl restart [email protected]
Some CDs haven't been found by the CDDB lookups. Not sure what to do with this yet. The script just rips them as Unknown Artist and Unknown Album and Track 1-N.
In theory abcde supports getting and embedding the album art, but I haven't got it working yet.
This depends a bit on what's in the CDDB database. If both discs have the same album name, the files will get ripped to the same directory but both discs' tracks will be numbered starting at 1. Right now I'll probably fix it manually but an automated solution would be lovely.
I put files a) where they worked and b) where they made sense based on what I could find on the web. I think in general all of these directories are appropriate, but I wouldn't be surprised if there are better places to put them.
- For ripping: https://somewideopenspace.wordpress.com/yet-another-headless-cd-ripper-and-mpd-on-raspberry-pi-installation-guide-yahcdramorpig/rip-music-from-cd/
- For abcde configuration: http://www.andrews-corner.org/linux/abcde/index.html
- For running when the disc is inserted: https://somewideopenspace.wordpress.com/yet-another-headless-cd-ripper-and-mpd-on-raspberry-pi-installation-guide-yahcdramorpig/start-ripping-when-cd-is-inserted/
- For the
post_encode()
stuff: https://lists.einval.com/pipermail/abcde-users/2015-June/000161.html - For systemd and udev: https://forums.opensuse.org/showthread.php/485261-Script-run-from-udev-rule-gets-killed-shortly-after-start
- For understanding why
SYSTEMD_WANTS
wasn't what I needed: https://superuser.com/questions/862533/how-to-start-a-systemd-service-when-a-dvd-is-inserted/862575#862575 and https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=151450&p=993856