note to self....

http://blogs.earthside.org/note_to_self/

Monday, October 31, 2005

m3u.php - create playlist from audio file directory

<?php
////////////////////////////////////////////////////////////////
//
// m3u.php - create a playlist from a directory listing
//
// This program collects a list of MP3 files in a directory on
// the web server and creates an .m3u playlist suitable for
// passing to to e.g. XMMS from a URL within a webpage or
// typed into a browser location bar.
//
// The program expects to be run from within the same
// directory as the MP3 files that it will list - that means
// that in the *nix world we typically just place this program
// file in the same directory as the music files.
//
// This program was tested using Apache webserver under SuSE
// 9.2, Firefox 1.07 web browser, and Xmms 1.2.10.
//
// Not sure of the Apache version - it is completely stock
// (but with updates installed).
//
// The client-side tests were run in a KDE desktop environment
// under both SuSE 9.1 and SuSE 9.2 - again, these are stock
// environments with nothing special done to them except
// routine updates.
//
// Correctly behavior for this script means that the web
// browser prompts for "Save this file or open using XMMS" and
// when XMMS is selected the playlist actually loads and plays
// as expected.
//
// This behaviour does not occur using existing playlist
// transfer code found on the web. Specifically, code that
// uses 'Content-Type: audio/x-mpegurl' triggers loading to
// XMMS but does not actually play.
//
// For other content types tried in the test environment
// described above: The 'audio/mpegurl' type does play
// correctly. The audio/mpeg and audio/x-mpeg types don't
// work, either (alhthough both of those do seem to work for
// single (individual) MP3 files.
//
// This program could be easily adapted to handle other media
// types.
//
// Credits:
// The HTTP header code is modified from an example found on
// the web. The Content-Type string is changed from the
// example.
//
// Revision History:
// 2005-10-30: pdwilso@gmail.com - initial version
//
////////////////////////////////////////////////////////////////
//
// Get input params...
//
// We're not doing anything here, yet...
//
$m3ufn=$_GET['file'];
// Modify this to allow users to supply a playlist filename or
// to provide a more desciptive filename for the playlist.
if (!($m3ufn === 'pl.m3u')) {
$m3ufn='pl.m3u';
}
//
// Get the environment we're running in, and create a base URL
// to use in constructing the invidividual song pointers.
//
$host=$_SERVER['SERVER_NAME'];
$DOCBASE = $_SERVER['DOCUMENT_ROOT'];
$len = strlen($DOCBASE);
$url='';
// Need to change this if you're not under *nix - $str gets the
// name of the current working directory.
$str = rtrim(`pwd`);
// The dir seperator here is *nix specific ('/')
$pelem = explode('/',substr($str,$len));
foreach ($pelem as $s) {
$url .= '/'.urlencode("$s");
}
//
// Construct a list of files...
//
// The list is an array of filenames for files in this (the
// current working directory) directory whose extensions match
// the type(s) of files we're looking for.
//
// Right now we're just handling MP3 - note that this is case
// sensitive - were just handling lower case 'mp3'
//
$dh = opendir(".");
while (($f=readdir($dh)) != false) {
// Change this regex to include e.g. .ogg files in the
// listing that will go to the player - if you change this
// to include e.g. video files, be sure to handle the
// Content-Type header, below, as well...
if (preg_match('/mp3$/',$f)) {
$files[] = urlencode($f);
}
}
closedir($dh);
sort($files);
//
// Create the output content...
//
// Not sure if the DOS line terminators are significant
// here, but it seems to work, so we leave it in.
//
$text="#EXTM3U:\r\n";
foreach ($files as $f) {
$u = "http://$host$url/$f";
$text.= "$u\r\n";
}
// Send it...
header("Pragma: no-cache"); // HTTP/1.0
header("Cache-Control: private"); // HTTP/1.1
header("Content-Type: audio/mpegurl", TRUE);
header("Content-Length: ".strlen($text)."", TRUE);
header("Content-Disposition: attachment; filename=\"".$m3ufn."\"", TRUE);
print $text;
?>





<< Home

Archives

2004/09   2005/03   2005/04   2005/05   2005/06   2005/07   2005/08   2005/09   2005/10   2005/11   2006/01   2006/02   2006/04   2006/05   2006/06   2008/01  

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]