[Techtalk] Streaming MP3s from a webserver?

Almut Behrens almut_behrens at yahoo.com
Sat Aug 25 02:52:19 EST 2001


On Fri, Aug 24, 2001 at 12:43:08PM -0400, David Merrill wrote:
> On Fri, Aug 24, 2001 at 12:26:19PM -0400, Jennifer Davis wrote:
> > Hi!
> > 
> > I was experimenting with working with PHP and SQL before I take on the
> > project of writing a database for a friend's organisation so being the
> > geek I am, I practiced on about the only thing I cared to organise, my cd
> > collection.  Now, after all this work, I had a brilliant idea.	Because
> > the PHP generated pages are just plain html, I was wondering if there were
> > any tools out there that would allow me to have somewhere on my server a
> > way to stream an mp3 (or other format?).  I figure I would convert the
> > last of my cds to mp3 and just have the php generate links to it.  I just
> > don't know how to make it stream.
> 
> Check out www.IceCast.org. It's schweet. I keep an icecast stream
> running in FreeAmp all the time.

Although I'll definitely second what David said, it might probably
also help to get an idea of the basic principle of mp3 streaming, if
you want to mess around with it yourself at some lower level.  Also,
icecast / shoutcast particularly address the broadcasting aspect,
which (although nice) is not necessarily required if you just want a
simple web-based solution to manage your music collection at home...

The most important thing to understand is the difference between mp3
files and the playlists required for streaming (extension typically
.m3u or .pls). The playlists are short text files which tell the mp3
player what to play/download -- which it then does on its own, instead
of through the browser. This kind of kludge mainly helps to avoid that
the browser will download the whole mp3 file, before passing it on to
the player.

The browser-side configuration consists of setting up the program
that'll be called to handle the MIME-type "audio/x-mpegurl", which
identifies the playlists (sometimes you'll also see "audio/mpegurl";
doesn't seem to be standardized yet)

In Netscape 4.x, for example, you'll get the dialog box to configure
the helper applications via Edit->Preferences->Navigator->Applications
Simply create a new MIME-type association:

  Description:  Streamed mp3 
  MIME Type:    audio/x-mpegurl 
  Suffixes:     .m3u 
  Application:  xmms %s 

(substitute your favorite player for xmms, which will most probably
support playlists too)

On the server side you have to create the playlists (.m3u), and
indicate the appropriate MIME-type by sending

  Content-Type: audio/x-mpegurl

in the HTTP headers (in place of text/html, text/plain, etc.).
The playlists are text files, which in the most simple case contain
one mp3-URL per line like this

  http://myserver/path/to/mp3s/song1.mp3
  http://myserver/path/to/mp3s/song2.mp3
  ...

The browser passes this list to the mp3-player, which then fetches and
starts to play the actual music data in streaming mode, i.e. without
first downloading the whole file.

If you have static playlist files (e.g. it might make sense to cache
dynamically computed lists), give them the extension ".m3u" and add
the following line to your httpd.conf (assuming you're using Apache):

  AddType audio/x-mpegurl .m3u

This will make Apache send the correct MIME-type for files with the
extension .m3u, not requiring any dynamic PHP or CGI stuff.
(if you're always creating the lists dynamically, you don't need the
AddType, of course)

That's the basic setup.  If you wanted support for the ID3 tag info
like artist, title, genre, etc., things would get a bit more tricky,
though -- time to look at what other people have already done... :)

You mentioned PHP, and I myself am not so much of a PHP gal, so I can't
tell you any details here. Yet, a php-ophile friend of mine recently
also had plans on web-ifying his mp3 collection at home.  So I just
asked him for a list of what he had checked out.  Here's what I got,
with the remark that he liked RIMPS the most:

RIMPS
  http://rimps.sourceforge.net/
  
muwed
  http://www.teco.edu/~krebs/muwed/

MP3 Streamer
  http://www.solsys.org/linux/projects/mp3-streamer/mp3-streamer-1.12.tar.gz

Agatha
  http://agatha.sourceforge.net/

Brunhilde
  http://www.phatplaya.com/brunhilde/index.php

I suspect that these projects (all GPL, AFAIK) provide enough PHP code
snippets to get you started.
He started his search at http://www.php.net/projects.php -- this sounds
like a good place to look for more...

Some other background info is available at these links, though somewhat
mod_perl / Apache::MP3 oriented.

http://stein.cshl.org/~lstein/talks/perl_conference/cute_tricks/index.html

(see the MP3 part) -- with a concise description of the .mp3/.m3u/MIME-type
stuff on this slide:

http://stein.cshl.org/~lstein/talks/perl_conference/cute_tricks/mp3_types.html

Some time ago, there was an article "Managing Streaming Audio" in
The Perl Journal, which is a good introduction.  On their site,
http://www.tpj.com, back issues no longer seem to be available,
but google located a copy of it here:

http://library.cs.tuiasi.ro/programming/perl/tpj/issues/vol4_4/tpj0404-0005.html


Have fun hacking,

- Almut





More information about the Techtalk mailing list