Thursday, June 4, 2009

Auto rip audio CDs in Ubuntu Server

I got my headless Linux server set up to auto rip music CDs and add them to my music library. Pop a CD in the tray and it automatically rips and tags the tracks to FLAC files and ejects the CD... and queues up a task to transcode to mp3 and then rescan my Squeezecenter library later. Here's how:

* Detection of the audio CD and kicking stuff off is done by ivman.
* Ripping and tagging is done by abcde.
* Transcoding is done by flac2mp3.
* Refreshing the library is done using the Squeezecenter CLI.

It took me "a while" to get it working end to end. The tough part was getting ivman configured. But that's the essential part for automation.

Ivman

To install
$ sudo apt-get install ivman
which installs a bunch of dependencies (but no X). Then I enabled an action to start a script when ivman detects an audio CD. This is what I added to /etc/ivman/IvmConfigActions.xml:
<ivm:Match name="hal.volume.disc.type" value="cd_rom">
<ivm:Match name="hal.volume.disc.has_audio" value="true">
<ivm:Match name="hal.volume.disc.has_data" value="false">
<ivm:Option name="exec" value="/home/user_x/bin/rip_cd '$hal.block.device$'"/>
</ivm:Match>
</ivm:Match>
</ivm:Match>
The rip_cd script just invokes abcde and directs its output to a log file.

The problem that broke my heart for ages was that ivman didn't have permission to use the cdrom drive. I got this error in the log file from abcde:
cd-discid: /dev/cdrom: open: Permission denied
[ERROR] abcde: CD could not be read. Perhaps there's no CD in the drive?
No such problem running abcde directly from the command line as the same user ivman ran as, since that user is a member of the group "cdrom".

How I eventually got it working consistently was to configure ivman to run explicitly as the group "cdrom".

Ivman requires you to specify both a user and a group for the daemon to run as - in /etc/ivman/IvmConfigBase.xml). Just because the user you specify is a member of a group, e.g. cdrom, doesn't mean that ivman will run as a member of that group.

In my setup, ivman is now solely configured from /etc/ivman and runs as "user_x" and group "cdrom". And abcde is solely configured from /etc/abcde.conf.

It can help to run ivman in debug mode:
$ sudo /etc/init.d/ivman stop
$ sudo ivman -d --nofork

ABCDE

To install
$ sudo apt-get install abcde
This also installs cd-discid and probably cdparanoia (which I had installed already). Getting abcde configured is easy, especially thanks to a great guide on Andrew's Corner.

I included a post-read hook in /etc/abcde.conf to enqueue a transcoding script when the CD is ripped.
post_read ()
{
atq -q x | while read line; do job=`echo $line | sed -e 's/\([0123456789]*\).*/\1/'`; atrm $job; done
at -q x 01:00AM -f /home/user_x/bin/flac2mp3
}
It would probably be better just to run the transcoding script as a cron job rather than "on demand". But this way works too.

The flac2mp3 script invokes Robin Bowes' flac2mp3, directing its output to a log file, and then invokes the Squeezecenter CLI to rescan the music library.

Of course, abcde would happily take care of ripping to FLAC and mp3 at the same time. But I prefer to rip to FLAC and transcode from there. That way, if I need to change tags, I just update the FLACs and run flac2mp3 to update the tags in my mp3s.

Squeezecenter CLI

Squeezecenter has a telnet-based command line interface. One of the things it supports is executing a rescan of the library. So I included the scriptlet below in my transcoding script, after transcoding completes:
echo `date` "requesting squeezecenter rescan..." >> $log
(
sleep 1;
/bin/echo -e "rescan\r";
sleep 1;
/bin/echo -e "exit\r";
sleep 1;
) | telnet localhost 9090 >> $log 2>&1
echo `date` "telnet exit status: " $? >> $log
Directing the output to a file was useful here (again). It helped me diagnose a problem when the "wrong" version of echo got used when the script was run from the at task, causing the telnet to fail. The fix was to use /bin/echo explicitly.

Of course, I could just let Squeezecenter schedule a regular rescan of the music library instead of doing it on demand. But I wanted to give the CLI a try.

Anyway, that's about it.

Just a note on hardware. I'm still using the fit-PC slim as my server. It doesn't have a CD drive. I connect the internal DVD burner from my old PC through a cheapo IDE-USB adapter cable from ebay.

10 comments:

  1. Probably about the most useful article on the entire internets. Thanks a ton!

    ReplyDelete
  2. You're welcome. I'm glad you found it useful.

    ReplyDelete
  3. I completely agree. I was MINUTES from setting something up very, very similar on my headless server. 100% win.

    ReplyDelete
  4. erhm could you meaby mail me or text out more specific what you did on the users run as part ? seems rather confusing

    ReplyDelete
  5. Hi Kenny. Do you mean how I configured ivman to run as a specific user and group - which is done in /etc/ivman/IvmConfigBase.xml?

    ReplyDelete
  6. Squeezecenter is a great product, but ripping CD's is a pain, and not something my non-technical girlfriend can do.

    I was searching for info on tools to do this, and see you beat me to it.

    Dean

    ReplyDelete
  7. Howdy, just came across this, and I was wanting to do the same. I'll be installing something similar to this on my headless server soon.

    Also, I would like to add a few more things. Namely, I want to add a check to see if it's a DVD and start a rip to iso and then a in the background mencoder process, or rip to an iso if it's neither.

    Would anyone know what the proper ivman hook would be for ANY disc?

    ReplyDelete
  8. that squeezecenter cli command just gave me an idea for mpd
    thanks

    ReplyDelete
  9. Hi Sudocode, useful article, thanks.

    I wondered if you had any idea on how to update these instructions to work on the latest versions of Ubuntu.

    It seems ivman is no longer available from aptitude.

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete