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.

<?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 <info@getid3.org>
 * 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"?>
<rss
  version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:media="http://search.yahoo.com/mrss/">
 <channel>
  <title> My Podcast </title>
  <description> My Description. </description>
  <copyright> Copyright 200x by me </copyright>
  <itunes:author> My Name </itunes:author>
  <itunes:owner>
    <itunes:name> Owner Name </itunes:name>
    <itunes:email> user@example.com </itunes:email>
  </itunes:owner>
  <itunes:category text="Religion &amp; Spirituality">
    <itunes:category text="Christianity"/>
  </itunes:category>
  <itunes:explicit>clean</itunes:explicit>
  <link>http://my.site/</link>
  <itunes:image href="http://my.site/my-logo.gif"/>
  <pubDate> <?php echo $now; ?> </pubDate>
  <ttl> 480 </ttl>
  <language>en-us</language>
<?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); ?>
  <item>
    <title> <?php echo (!empty($ThisFileInfo['comments_html']['title']) ? implode(' ', $ThisFileInfo['comments_html']['title']) : $ThisFileInfo['filename'] ); ?> </title>
    <itunes:author> <? echo (!empty($ThisFileInfo['comments_html']['artist']) ? implode(' ', $ThisFileInfo['comments_html']['artist']) : 'unknown'); ?> </itunes:author>
    <dc:creator> <?php echo (!empty($ThisFileInfo['comments_html']['artist']) ? implode(' ', $ThisFileInfo['comments_html']['artist']) : 'unknown'); ?> </dc:creator>
    <description> <?php echo (!empty($ThisFileInfo['comments_html']['comments']) ? implode(' ', $ThisFileInfo['comments_html']['comments']) : ''); ?> </description>
    <pubDate> <?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)); ?> </pubDate>
    <enclosure url="http://my.site/path/to/<?php echo $ThisFileInfo['filename']; ?>" length="<?php echo $ThisFileInfo['filesize']; ?>" type="audio/mpeg" />
    <guid isPermaLink="true"> http://my.site/path/to/<?php echo $file; ?> </guid>
  </item>
<?php     }
}?>
 </channel>
</rss>
Topics: