ASX/M3U/PLS On-Demand Playlist Generator

Using a single php file and a simple rewrite rule, it is possible to turn any standard website hosting account in to a on-demand stream server with automatic playlist generation (to launch specific players, ect).

Place this into a file called playlist.php in the root folder:

<?php
  // Auto Playlist Generator by R. McAnally 6/2010
  // To be used with proper rewrite rules
  $expire = "Expires: " . gmdate("D, d M Y H:i:s", time()) . " GMT";
  header("Cache-Control: max-age=86400, must-revalidate");
  header($expire);
  header("Content-Disposition: filename=".basename($_SERVER['REQUEST_URI']));
?>
<?php if ( strtolower($_REQUEST['type']) == 'asx' ) {
header("Content-Type: video/x-ms-asf"); ?>
<ASX version="3.0" >
  <Repeat count="8">
  <Entry>
    <Ref href="<?php echo 'http://'.$_SERVER['SERVER_NAME'].'/'.$_REQUEST['file'] ?>" />
  </Entry>
</ASX>

<?php } elseif ( strtolower($_REQUEST['type']) == 'm3u' ) {
header("Content-Type: audio/x-mpegurl"); ?>
<?php echo 'http://'.$_SERVER['SERVER_NAME'].'/'.$_REQUEST['file'] ?>

<?php } elseif ( strtolower($_REQUEST['type']) == 'pls' ) {
header("Content-Type: audio/x-scpls"); ?>
<?php echo 'http://'.$_SERVER['SERVER_NAME'].'/'.$_REQUEST['file'] ?>


<?}
?>

Then, add the following rewrite directives into an .htaccess file in the same location:

RewriteEngine on
RewriteRule ^(.*)\.(asx|pls|m3u)$ playlist.php?file=$1&type=$2

Now simply upload some media files (.wma, .wmv, .mp3, ect).

For example, you have set this up on somedomain.com and uploaded test.mp3. To access it on-demand using a standard player, use the following URL:

http://somedomain.com/test.m3u

To access it using Windows Media Player, use the following instead:

http://somedomain.com/test.asx

Make sense? I hope so!

3 Responses to ASX/M3U/PLS On-Demand Playlist Generator

  1. chad says:

    how can i get this to work with a m3u8 playlist?

    i have the playlist.php but its not in the root, its like this
    http://mysite.com/stuff/playlists/playlist.php

    the mod rewrite is what is giving me the problem.

  2. eno says:

    Does it work also with video files ?!

  3. Miguel says:

    Wowww: nice tip mate. Thanks for this sharing