The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Apache::MP3 - Play streaming audio from Apache

SYNOPSIS

  AddType audio/mpeg .mp3
  AddType audio/mpegurl .pls
  AddType audio/x-shoutcast-stream .mps

  <Files ~ "\.(pls|mps)$">
    SetHandler perl-script
    PerlHandler Apache::MP3
  </Files>

DESCRIPTION

Apache::MP3 is designed to respond to requests for a playlist document, ending in the extension .pls, or a streaming MP3 document, ending in the extension .mps (the first of these extensions is standard; the second one I made up for this application). Neither of these documents exist as static files, but are generated as needed dynamically from a directory structure containing MP3 files.

The rules for the playlist construction are a bit tricky. Consider a Web root that contains a top-level directory named "samples", and that it contains four files arranged in the following manner:

 /samples/the_wheel/Imbolc.mp3
 /samples/the_wheel/Samhain.mp3
 /samples/the_wheel/Merry_Men.mp3
 /samples/the_wheel/The_Process.mp3

A request for the URL /samples/the_wheel/Merry_Men.pls will cause Apache::MP3 to look into the /samples/the_wheel directory. Notice that there is a MP3 file that uses the same basename as the requested playlist, and autogenerate a playlist containing the single URL http://your.site/samples/the_wheel/Merry_men.mps. Notice that the URL Apache::MP3 generates is a request for a .mps URL rather than for the MP3 file itself. The .mps URL will be used in a second request to generate an MP3 stream.

Apache::MP3 can also generate a playlist for an entire directory's worth of MP3 files. Just take the directory name and add a .pls extension. For example, if the browser requests the URL "/samples/the_wheel.pls", then Apache::MP3 will construct a playlist containing the four URLs /samples/the_wheel/Imbolc.mps through /samples/the_wheel/The_Process.mps.

When Apache::MP3 receives a request for a URL ending in the extension ".mps", it looks for the corresponding MP3 file, extracts its ID3 tags with MPEG::MP3Info, constructs a Shoutcast/Icecast header, and streams the file to the client.

To summarize, your links should look like this:

Download an mp3 file, no streaming:
  http://your.site/samples/the_wheel/Merry_Men.mp3
Stream an mp3 file:
  http://your.site/samples/the_wheel/Merry_Men.mps
Stream an entire directory of mp3's as a playlist:
  http://your.site/samples/the_wheel.pls

NOTE: This module requires the MP3::Info module.

MORE INFORMATION

See my article in the The Perl Journal volume 16 (www.tpj.com).

AUTHOR

Lincoln Stein <lstein@cshl.org>.

COPYRIGHT

Copyright (c) 2000 Cold Spring Harbor Laboratory. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO

MP3::Info