Start ripping when CD is inserted

To run really headless it is necessary to rip CDs whenever they are inserted and eject them afterwards. To achieve this I’ve chosen to use udev. First we create a script which prevents abcde to run multiple times and definitely ejects the cd even if abcde fails because of faulty CDs or any other stupid things which might happen during ripping…

sudo nano /usr/local/sbin/audio-cd-rip.sh

Now the few lines of code

#!/bin/bash
LOGFILE=/var/log/cdrip.log
echo "$(date)" >> $LOGFILE

(
  # Wait for lock on /var/lock/.audio-cd-rip.lock (fd 200) for two hours
        flock -x -w 7200 200 || exit 1

        abcde 2>&1 >> $LOGFILE
        rc=$?
        if [[ $rc != 0 ]] ; then
                eject
                exit $rc
        fi
) 200>/var/lock/.audio-cd-rip.lock

Why wait two hours? It would wait two hours. But a maximum of two hours. In real life this should never happen.

I saw multiple events from udev when a CD is inserted. To prevent multiple runs of the same script at the same time, I just let one instance of the script’s content (abcde, eject) run and block other requests until it’s done. If one request is waiting longer than two hours it gives up and exits.

Mark the script executable.

sudo chmod +x /usr/local/sbin/audio-cd-rip.sh

Now we create a udev rule that runs this script. A udev rule lets the udev service call our script it the conditions of the rule match.

sudo nano /etc/udev/rules.d/99-cd-audio-processing.rules

Insert one single line:

SUBSYSTEM=="block", KERNEL=="sr0", ACTION=="change", RUN+="/usr/local/sbin/audio-cd-rip.sh &"

This says, that the script shall run when a CD with at least one audio track is inserted in block device sr0 (which is usually your first CDROM/DVD/WHATEVER-Drive). The script will run in background (you see the ‘&’?). Now reload udev rules

sudo udevadm control --reload

And here our mission is completed.

Thanks for watching us.

<Cliffhanger>But, is it? Really? </Cliffhanger>
No, sadly it isn’t over. There is currently a problem on Raspbian with udev to detect manually inserted or ejected media. Don’t know why or what, but got a workaround!

We ‘touch’ our CDROM device every minute by cron. this lets udev wake up and our script runs if a media has been inserted. If ever this bug is fixed, you don’t need this ‘touch’ anymore. On other distros it should run without this trick.

sudo crontab -e

Add this line at the end

*/1 * * * * /bin/touch /dev/sr0

And here our mission is really completed. Thanks for watching us.

Advertisements

17 thoughts on “Start ripping when CD is inserted

  1. You may also want to enable polling under rasbian.

    To do so

    echo 2000 > /sys/module/block/parameters/events_dfl_poll_msecs

    In your rc.local script.

  2. hi!
    the first part of you tutorials work perfect, when i write sudo abcde, my cd rip perfect
    but the second part don’t work for me…
    I create audio-cd-rip.sh and made all you write but nothing to do…
    could you help me?
    thanks a lot
    David

  3. Hi, Thanks for your step by step tutorial. abcde runs fine, so i can rip cd’s, but the last part (the autorip script) does not work. When i insert a disc, nothing happens.
    I am running Volumio, which is custom debian installation. Does that have anything to do with it?

    Thanks,
    Nor

  4. Hi … I’m having a heck of a time getting udev to run my script in arcg linux ….. the abcde script works fine, I just can’t get the inserted cd to trigger it …
    do i need the rule in /etc/udev or usr/lib/udev . does the rule need special permissions? … here’s my rule (i’m running a usb cdrom on sr0 ) …

    SUBSYSTEM==”block”, KERNEL==”sr0″, ENV{ID_CDROM_MEDIA_CD}==”1″, RUN+=”/usr/bin/cdautoinsert”

    • Hi Tim i’m not a udev expert but for troubleshooting you can try removing the ‘ENV’ section, move the ‘KERNEL’ section to the front of the udev command. I believe you may also need some kind of test for the device to meet at the very beginning of the udev line, such as ACTION==”change” or ACTION==”add”

  5. Ok guys, have finally got the auto rip script working. Be warned though, what I have done is the lazy solution, and hardly the proper way to do things. I am using Jessie Lite on the Raspberry Pi 2:

    1. Follow steps above, ignore the crontab step.
    2. On the last line,change ‘ 200>/var/lock/.audio-cd-rip.lock’ to ‘ 200>~/.audio-cd-rip.lock’
    3. Chmod 777 the /var/log/ directory recursively.
    4. Reboot and insert a cd.

    The problems for me arose in a few places:
    1. If you are using abcde to write to a hard drive mounted with usbmount, make sure it is read-write, as the drive being read only was one problem I encountered.
    2. make sure that after a failed abcde rip, the /tmp dir, or wherever you store the ripped content temporarily is cleared, as abcde will fail if you try it again and the remnants of the previous rip are there.
    3. I haven’t figured out how to use /var/lock for this particular script. I have no idea which user udev runs as, but even when I chmod the /var/lock directory and the .audio-rip-cd.lock file, it loses those permissions the moment it is deleted, and thus the next cd inserted fails. The (bodge) solution appears to be to put the lock file in a directory that will always be writeable by whatever user executes the script, hence the ‘~’.

    Hopefully this gets you all one step closer.

    S.

  6. Hi !
    It works well but the CD insertion isn’t detected on a Pi3.
    I need to look further, I’ve done it really quickly.
    One interesting thing is abcde 2.7.2. You can install it on a Pi by donwloading the archive, extract it and a “make install” command.
    Edit the ACTIONS section in abcde.conf and put getalbumart into
    ACTIONS=cddb,read,getalbumart,encode,tag,move,clean
    And you’ll have a cover.jpg withy your audio files.

    • With the WorBlux echo, it works better. But the rip stops suddenly.
      Here’s the log
      Sun May 8 10:34:37 CEST 2016
      Sun May 8 10:34:38 CEST 2016
      Sun May 8 10:34:49 CEST 2016
      Grabbing entire CD – tracks: 01 02 03 04 05 06 07 08 09 10 11 12 13 14
      Retrieved 1 Musicbrainz match…done.
      —- Les Cowboys Fringants / Octobre —-
      1: Octobre
      2: Bye Bye Lou
      3: La La La
      4: Les Vers de Terre
      5: Pizza Galaxie
      6: Les Feuilles Mortes
      7: So So
      8: La Cave
      9: Marine Marchande
      10: Oktoberfest
      11: La Dévisse
      12: Mon Grand-Père
      13: Louis Hébert
      14: Pub Royal

      Then nothing more. But it starts…

      root@max2play:/tmp/abcde.vHVZoB3DK5cnCSjIPoAyDjnuAuk-# ls -l
      total 16328
      -rw-r–r– 1 root root 10 mai 8 10:34 asin.1
      -rw-r–r– 1 root root 10 mai 8 10:34 asin.2
      -rw-r–r– 1 root root 304 mai 8 10:34 cddbchoices
      -rw-r–r– 1 root root 41 mai 8 10:34 cddbquery
      -rw-r–r– 1 root root 905 mai 8 10:34 cddbread.1
      -rw-r–r– 1 root root 127 mai 8 10:34 discid
      -rw-r–r– 1 root root 36 mai 8 10:34 mbid.1
      -rw-r–r– 1 root root 36 mai 8 10:34 mbid.2
      -rw-r–r– 1 root root 262 mai 8 10:34 status
      -rw-r–r– 1 root root 16678956 mai 8 10:35 track01.wav

      And there’s no track 2…
      There’s something interesting in daemon.log
      May 8 10:43:28 max2play systemd-udevd[2719]: timeout ‘/usr/local/sbin/audio-cd-rip.sh &’
      May 8 10:43:29 max2play systemd-udevd[2719]: timeout: killing ‘/usr/local/sbin/audio-cd-rip.sh &’ [2721]
      May 8 10:43:29 max2play systemd-udevd[2719]: ‘/usr/local/sbin/audio-cd-rip.sh &’ [2721] terminated by signal 9 (Killed)

      I don’t know why there’s a time out.

      • Ok, the timeout is due to the fact that triggering udev events is only for very short scripts.
        One solution is to create a systemd service that will call the /usr/local/sbin/audio-cd-rip.sh
        Step 1 : Modify /etc/udev/rules.d/99-cd-audio-processing.rules
        KERNEL==”sr0″,ENV{ID_CDROM_MEDIA_DVD}==”1″, RUN+=”/bin/systemctl start autorip.service”

        Step 2 : Create /etc/systemd/system/autorip.service and put this as the content
        [Unit]
        Description=AutoRip CD on insertion

        [Service]
        Type=oneshot
        ExecStart=/usr/local/sbin/audio-cd-rip.sh

        Save it. Then sudo udevadm control –reload
        And it should do the trick.
        2 things :
        – putting autorip.service in the /etc/systemd/system isn’t a good idea as an update can delete it.
        – the logging is in dameon.log instead of cdrip.log

  7. Hi all,
    I’m using CDDB to get CD information. If more than one entry was found, abcde stops working and I have to choose the correct one. How to automatize the process? It’s enough for me to get the first choice (#1) in every case.

  8. Hey … so looks like this owrks ….but …. I cant seem to get permissions sorted out … if I ren the script with sudo powers it works. Ive chmod 777’d the script as well didn’t change. I realise this is simple … what do I need to do?

  9. All works great using the above pointers except for one small snag:

    I’m using flac as the encoder and on every cd the encoding is still going on for a minute or two after the cd has been ejected.
    If I insert another cd before the encoding has finished I get the following error and everything grinds to a halt.

    timeout ‘/bin/systemctl start autorip.service’
    timeout: killing ‘/bin/systemctl start autorip.service’ [5054]
    ‘/bin/systemctl start autorip.service’ [5054] terminated by signal 9 (Killed)

    I am a complete linux novice so wouldn’t have a clue how to get around this. Is there a way of waiting on the encode to finish before ejecting the cd perhaps?

    Thanks!

  10. I got everything working with the exception of the auto rip. I tried a number of the steps listed in the documentation as well as suggestions in the replies but nothing seems to kick off the read when a CD is inserted (I still have to connect with SSH and enter sudo abcde). I am really hoping someone has a bullet-proof method to make this work. Thanks in advance.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s