chacadwa.com

Technical blog and writings by Micah Webner.

Simple Podcast Feeds with PHP and the getID3 Library

I may need to come back and write a better introduction to this, but I thought I'd share my relatively simple podcast template.

There are a number of really good ways to deliver podcasts, but a very simple way is to write a simple directory index using php and the getID3 library.

This template is based on the simple demo script, but generates an RSS feed instead of an HTML page. It reads all of the publishing information - title, author, and description - for each item directly from the ID3 tags in the mp3 files themselves. This script can be made to run faster by enabling mysql caching.

To make this work, you'll need to download and install the getID3 library on your web server, and fix all of the paths to point to the correct stuff.

I've used this script on both Linux/Apache and Windows/IIS and it works fine on either platform. Since most Windows hosting packages now support PHP, this could be a very simple way to add a podcast feed to your existing static web site.

[code lang=php]
<?php
/**
* @file
* podcast.php by Micah Webner
*
* Based on /demo/demo.simple.php - part of getID3()
* Sample script for scanning a single directory and
* displaying a few pieces of information for each file.
*
* getID3() by James Heinrich
* available at http://getid3.sourceforge.net
* or http://www.getid3.org
*/

header('Content-Type: text/xml; charset=UTF-8');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0');
header('Pragma: no-cache');
header('Expires: Sun, 19 Nov 1978 05:00:00 GMT');

$now=date('r');
?>
<?xml version="1.0" encoding="utf-8"?>

My Podcast
My Description.
Copyright 200x by me
My Name

Owner Name
user@example.com

clean
http://my.site/

<?php echo $now; ?>
480
en-us
<?php
// include getID3() library (can be in a different directory if full path is specified)
require_once('/path/to/getid3/getid3/getid3.php');
// Initialize getID3 engine
$getID3 = new getID3;

getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'extension.cache.mysql.php', __FILE__, true);

// database not configured, sorry...
// $getID3 = new getID3_cached_mysql('localhost', 'dbname', 'login', 'password');

$DirectoryToScan = '.'; // change to whatever directory you want to scan
$dir = opendir($DirectoryToScan);
while (($file = readdir($dir)) !== false) {
if (strpos($file,".mp3")) $files[] = $file;
}
closedir($dir);

rsort($files,SORT_STRING);

foreach ($files as $file) {
$FullFileName = realpath($DirectoryToScan.'/'.$file);
if (is_file($FullFileName)) {
set_time_limit(30);
$ThisFileInfo = $getID3->analyze($FullFileName);
getid3_lib::CopyTagsToComments($ThisFileInfo); ?>

<?php echo (!empty($ThisFileInfo['comments_html']['title']) ? implode(' ', $ThisFileInfo['comments_html']['title']) : $ThisFileInfo['filename'] ); ?>
<? echo (!empty($ThisFileInfo['comments_html']['artist']) ? implode(' ', $ThisFileInfo['comments_html']['artist']) : 'unknown'); ?>
<?php echo (!empty($ThisFileInfo['comments_html']['artist']) ? implode(' ', $ThisFileInfo['comments_html']['artist']) : 'unknown'); ?>
<?php echo (!empty($ThisFileInfo['comments_html']['comments']) ? implode(' ', $ThisFileInfo['comments_html']['comments']) : ''); ?>
<?php echo date("D, j M Y H:i:s -0400", mktime(0,0,0,substr($file,2,2),substr($file,4,2),substr($file,0,2),-1)); ?>
" length="<?php echo $ThisFileInfo['filesize']; ?>" type="audio/mpeg" />
http://my.site/path/to/<?php echo $file; ?>

<?php }
}?>

[/code]

Topics: