Monday, August 31, 2009

Setting up an Ubuntu Music Streaming Server using Firefly

If you're like me, you probably:
  • have too much music to fit on your laptop HD
  • end up storing your music on an external HD but hate carrying it around everywhere
  • want to listen to your music from your other computers
  • have access to a linux server somewhere

If you're lucky, those are the only things you have in common with me. If so, great, I'm going to show you how to serve music from a linux machine.

Now there are a few caveats before I start:
  • you'll want to have a decently fast network connection
  • it's nicer to look at your neatly-categorized music locally through iTunes cover browser interface than as a shared library
  • your library won't maintain play counts etc, and I haven't tried using ratings
  • I don't have a perfect solution for syncing an iPod/iPhone, but there are some half-baked solutions you can look into

I'm going to assume the laptop you're using is a Mac and the linux box is running Ubuntu 9.04 or greater, but most of these instructions should translate somewhat to other OS's/distros.

Oh yeah, and before you do any of this stuff, back up your music. No one wants to make a mistake and lose years of collected tracks.

Step 0: Clean up your Library

Now is a really good time to clean up your library: get rid of the old crap you don't listen to, fix your ID3 tags, add album art, replace missing songs in albums etc. I just deleted my iTunes library file and selectively added back music one album at a time when I wanted to listen to it, and have since become a bit obsessive about keeping my library organized. I lost my playlists etc, but they were all a mess anyway. You could back up this stuff if you want.

iTunes doesn't have art for a bunch of my music, so I used this widget to pull art off Amazon.

Step 1: Install Firefly

I'm assuming you have a working Ubuntu installation and you know your way around the command line.

Firefly (also known as mt-daapd) is the program that exports your library as a DAAP stream, which is Apple's protocol for iTunes media sharing. Apple have changed the protocol in recent versions of iTunes to make it incompatible with other programs, but Firefly uses a version of the protocol that works fine.

Install Firefly (mt-daapd):
sudo apt-get install mt-daapd

Now edit /etc/mt-daapd.conf to your liking. The defaults are a good start, but you'll want to change admin_pw, mp3_dir, servername and password. You can change many of these settings through the web interface once you get going. I created a new user account named "media" to store my media on the server, added myself to the "media" group, and pointed mp3_dir to /home/media/music, but do as you please. servername is whatever you want your stream to be listed as in iTunes. admin_pw is the password used to access the web configuration interface, while password is just the password people need to enter to listen to your shared library.

Now restart Firefly:
sudo /etc/init.d/mt-daapd restart

If this worked so far, you should be able to access the web configuration interface at http://servername:3689. Make sure you forward port 3689 on your router (if you're behind a NAT), and open that port on your firewall (if you have one).

Step 2: Connect to your Shared Library

Now the exciting part.

We can't connect to the stream in iTunes yet, but we can with any other program that connects to DAAP streams. I use Songbird, which I'm hoping to fully transition over to from iTunes at some stage.

Install Songbird and the Songbird Daap Client add-on. Then go to File -> New DAAP library, type in daap://servername:3689, and it should load your library in Songbird!

Wait wait, that wasn't very exciting - we haven't added any music yet! We'll get to that now.

Step 3: Move Your Music to Your Server

Your music might be neatly organized, but if you're using iTunes, all your album art is sitting in a separate database file. I used this Applescript to embed all the art back into the ID3 tags.

Now just copy your music folder across to your server, using rsync, scp, or from an external HD. (One day I'll post an article about mounting an HFS+ formatted HD in linux.)

I came across one small complication here: mp3 files have two types of ID3 tags - v1 and v2. iTunes writes tag information to the ID3v2 tag, but ignores the ID3v1 tag, which can leave bad tag data lying around to confuse other programs. I fixed this problem in bulk by stripping away the ID3v1 tags using a program called id3v2.

sudo apt-get install id3v2
id3v2 -s *.mp3
id3v2 -s *.m4a

I didn't lose any useful tag data this way. If you want to be on the safe side, you can select your mp3s in iTunes, right-click, select Convert ID3 Tags, and convert the tags to v2.3.

Step 4: Enable iTunes Sharing

Are you sure you still want to use iTunes? Okay, I still do, but bear in mind that you can only connect to your library if you're on the same subnet. There are ways around accessing libraries on different networks, but you're better off just using Songbird in this case.

iTunes doesn't just let you type in the location of a DAAP share - that would be too easy. Instead it uses Zeroconf (Bonjour) to automatically discover shared libraries on your network. What we need to do is set up the Ubuntu server to advertise Zeroconf services using Avahi. This can also be used to advertise a network drive from the server,but that's the topic of another discussion.

First install Avahi:
sudo apt-get install avahi-daemon

At the time of writing, there's a race condition between Avahi and Firefly in the current Ubuntu packages.  This sometimes prevents Firefly from loading. The bug can be fixed by applying the following patch

for 32 bit:
wget http://launchpadlibrarian.net/26549308/mt-daapd_0.9%7Er1696.dfsg-9_i386.deb
sudo dpkg -i mt-daapd_0.9%7Er1696.dfsg-9_i386.deb

or for 64 bit:
wget http://launchpadlibrarian.net/26923010/mt-daapd_0.9%7Er1696.dfsg-9ubuntu1_amd64.deb
sudo dpkg -i mt-daapd_0.9~r1696.dfsg-9ubuntu1_amd64.deb

Hopefully this bug will be fixed in Ubuntu 9.10.

Now create a file /etc/avahi/services/mt-daapd.service containing:

<?xml version="1.0" standalone='no'?>
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
<name replace-wildcards="yes">%h</name>
<service>
<type>_daap._tcp</type>
<port>3689</port>
<txt-record>txtvers=1</txt-record>
<txt-record>iTSh Version=131073</txt-record>
<txt-record>Version=196610</txt-record>
</service>
</service-group>


Restart Avahi:
sudo /etc/init.d/avahi-daemon restart

and your DAAP share should pop up in the iTunes sidebar on any of the computers in your local network.

Step 5: Listen to Some Music

Are we done? I hope so! If I've left something out, or made an error, please let me know in the comments. Bear in mind that I might not have time to help with specific problems.

James

5 comments:

  1. I'm getting ready to do this on a setup at home (a linux share accessed in itunes) and I don't know much about the protocols and other details. Though I'm running different versions of thing (and we'll see about apple tv and videos) this looks to be a great starting point. Thanks for sharing the info.

    ReplyDelete
  2. Nice work! However: when I log in to the server's admin pages, I am greeted by a strange message: "the Firefly server is not running,..." albeit followed below by: "Bonjour running" and "Firefly Media Server running". Which one should I believe?

    (PS: I am installing it remotely, so using lynx from ssh session to log in).

    ReplyDelete
  3. Exactly my situation. Followed this in a hurry before work and it works like a charm. I have rhythmbox on my laptops as clients. My big fat ubuntu fileserver in the hall which doesn't even have speakers is housing my music collection and firefly. Yay! Perfect.

    ReplyDelete
  4. Nice guide, got it up and running in a few minutes. Saves copying music or messing around with Samba, this is exactly what i wanted to setup for my home network.

    Kind Regards

    ReplyDelete
  5. How can you access the music over a web browser or android device for free?

    ReplyDelete