Podcasting

Events happening in the community are now at Drupal community events on www.drupal.org.
gate-gdo's picture

Hi Folks,

I'm not a drupal user; however, I have made use of some of the code that you have here, and I thought that i would post some code here that you could use in creating a podcasting/rss module for your stations. I run the computers at our campus and community station in Abbotsford, we do'nt have an FM Frequency at the moment; however, it's in the works.

I have it implemented at http://civl.ca/supercali/index.php?o=3; you can see there that any show has a podcast link where people can subscribe to it with itunes or winamp. I definitely think that podcasting is the way of the future audio media.

The module works with darkice and icecast; i currently have it running on a BSD system. You will need to write some way of inserting show name information into the id3tag; i believe that drupal has a way of doing this.

The program does the following
1) Creates mp3files from darkice stream and puts them into a nice directory structure (thank you previous mattrock & drewish) every hour (mp3cron.php)

2) Publishes id3 tags from information retrieved from my calendar program (insert drupal code here) (mp3cron.php)

3) Scans each file after it has been copied into the nice directory structure for mp3 information (makerss)

4) Checks if the rss file exists in /usr/rss, if it does not exist the file is created with the information written into the id3 tag

5) A third php script runs which creates an index.html in the /usr/rss directory so that the published files can be viewed by some webprogrammer.

notes:
/usr/rss/ & /usr/broadcastlog/ is made available over http with icecast using the webroot command and an appropriate symlink.

/usr/local/etc/darkice.cfg - as posted before under webarchiving
/usr/local/etc/icecast.xml - make sure the following is in there: <paths><webroot/usr/local/share/icecast/web</paths>

mp3cron.php runs every hour on the hour:

<?php

$localtime=localtime(time(),true);
$year =  $localtime[tm_year]+1900;
$mon  =  $localtime[tm_mon];
$mday =  $localtime[tm_mday];
$min  =  $localtime[tm_min];
$hour =  $localtime[tm_hour]-1;
if ($mon < 10) {
  $mon = 0 . ($mon+1);
}
if ($mday < 10) {
  $mday = 0 . $mday;
}
if ($min < 10) {
  $min = 0 . $min;
}

fwrite(STDOUT, "mon: $mon, mday: $mday, min: $min, hour: $hour\n");

$log_dir = "/usr/broadcastlog2/$year/$mon/$mday";
$log_file = "$hour.mp3";

$pid = cat /var/run/darkice.pid;

fwrite(STDOUT, "Logging folder: $log_dir\n");
fwrite(STDOUT, "Saving Last Hour to $log_file\n");
fwrite(STDOUT, "Darkice PID: $pid\n");

mkdir -p $log_dir;

$command="echo \"$log_dir/$log_file\" > /tmp/darkice.file-0.$pid";
exec($command);
exec("kill -s USR1 $pid");
sleep (5);

chmod("$log_dir/$log_file",0644);

$wday = array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
$today = $wday[$localtime[tm_wday]];
$months = array("January","February","March","April","May","June","July","August","September","October","November","December");
$thismonth = $months[$localtime[tm_mon]];
$showdate = "$today, $thismonth $mday, $year";
fwrite(STDOUT,$showdate);

$showname    = /usr/bin/fetch -q -o - http://civl.ca/inc/lastplaying.php;
$dj          = /usr/bin/fetch -q -o - http://civl.ca/inc/currentdj.php;
$description = /usr/bin/fetch -q -o - http://civl.ca/inc/currentdescription.php;
if ($showname == "") $showname = "CIVL Radio Fill";
if ($dj       == "") $dj = "CIVL Radio Fill";
if ($description == "") $description = "BUY BUTTONS AND T-SHIRTS FROM CIVL";

print "\nshowname: $showname\ndj: $dj\ndescription: $description\n";

$command="/usr/local/bin/id3v2 --artist \"$dj\" --album \"$showname\" --song \"$showdate\" --year \"$year\" --comment \"$description\" $log_dir/$log_file";
exec($command);

$delcommand="/bin/rm $log_dir/$hour.mp3";
print $delcommand;

if ($hour<8 || $hour>19){print"in if";exec($command);}

makerss.phps runs every hour at :05

<?php
#This file is run every hour at *:05.  It runs 5 minutes after so that we know mp3cron is done working on teh script. 
#First read the date information so we know which file to check on
$localtime=localtime(time(),true);
$year = $localtime[tm_year]+1900;
$mon  = $localtime[tm_mon];
$mday = $localtime[tm_mday];
$hour = $localtime[tm_hour]-1;
if (
$mon < 10){
 
$mon = 0 . ($mon+1);
}
if (
$mday < 10) {
 
$mday = 0 . $mday;
}

#Second read the information from the mp3 file which was just created.
$log_dir = "/usr/broadcastlog/$year/$mon/$mday";
$log_file = "$hour.mp3";

fwrite(STDOUT, "\nReading file $log_dir/$log_file\n");

#thanks for this code ben
$data = shell_exec("/usr/local/bin/id3v2 -l $log_dir/$log_file");

if((
$v2offset = strpos($data, 'id3v2')) === false){
  echo
"($log_file) has no ID3V2 tag.\n";
}
$v2soffset = strpos($data, "\n", $v2offset);
$v2data = trim(substr($data, $v2soffset+1));

$include = array(TPE1,TALB,TIT2,TYER,COMM);
$darray = split("\n",$v2data);

for(
$i=0;$i<sizeof($darray);$i++){
  if(
strlen($darray[$i]) >=4) {
   
$code = trim(substr($darray[$i], 0, 4));
    if(
in_array($code,$include)) {
     
$fc = strpos($darray[$i], ':')+2;
     
$fcdata = substr($darray[$i], $fc);
     
$fcdata = trim($fcdata);
      if (
$fcdata != '') {
        if(
$code =='TIT2') {
         
$song = $fcdata;
        } elseif(
$code == 'TPE1') {
         
$artist = $fcdata;
        } elseif(
$code == 'TALB') {
         
$album = $fcdata;
        } elseif(
$code == 'TYER') {
         
$year_id = $fcdata;
        } elseif(
$code == 'COMM') {
         
$comm = $fcdata;
        }
      }
    }
  }
}
#comment fix
$comm = substr($comm,strpos($comm, ':')+2);

print(
"\nThe following information was extracted from $log_dir/$log_file\n");
print_r($id3);

#id3 is the array which contains the informaiton on the song. 

#Third check to see if $id3['album'].xml exists in /usr/rss, if it does not exist then create it with the information from the file.

$rss_dir = "/usr/rss";
$rss_file = $album;
$rss_file = "$rss_file.xml";
$length = filesize("$log_dir/$log_file");
fwrite(STDOUT, "\nWriting RSS information to $rss_dir/$rss_file\n");
print
"testing hour var: \'$hour\'\n";
if (
$hour == 0){
 
$hourshow="12am";
}
elseif (
$hour==12){
 
$hourshow="12pm";
}
elseif (
$hour<12){
 
$hourshow=$hour . "am";
}else{
 
$hourshow=$hour-12 . "pm";
}

$data = "    <item>\n" .
       
"      <title>$song, $hourshow</title>\n" .
       
"      <description>Show Recorded $song</description>\n" .
       
"      <enclosure url=\"http://live.civl.ca:8085/podcast/$year/$mon/$mday/$hour.mp3\" length=\"$length\" type=\"audio/mpeg\"/>\n" .
       
"      <guid>http://live.civl.ca:8085/podcast/$year/$mon/$mday/$hour.mp3</guid>\n" .
#        "      <pubDate>$song</pubDate>\n" .
       
"      <link>http://civl.ca/</link>\n" .
       
"      <category>Music</category>\n" .
       
"      <itunes:author>$artist</itunes:author>\n" .
       
"      <itunes:subtitle>$song, $hourshow</itunes:subtitle>\n" .
       
"      <itunes:summary>\"CIVL Radio Abbotsford UCFV Doug Chilliwack Mission Fraser Valley Campus Community\"</itunes:summary>\n" .
       
"      <itunes:keywords>\"CIVL Radio Abbotsford UCFV Doug Chilliwack Missoin Fraser Valley Campus Community\"</itunes:keywords>\n" .
       
"    </item>\n";


if(
file_exists($rss_dir . "/" .$rss_file)){
  print(
"File exists");
 
addRSSItem($rss_dir,$rss_file,44,$data);
}
else {
 
createRss($rss_dir,$rss_file,$artist,$album,$comm);
 
addRssItem($rss_dir,$rss_file,44,$data);
}





function
createRss($rss_dir,$rss_file,$artist,$album,$comm){
 
$data = "<?xml version=\"1.0\" encoding=\"utf-8\"?>
\n" .
          "<!--\n" .
          "If you\'re seeing this, you\'ve clicked on the link\n" .
          "for a CIVL Radio RSS feed and Podcast.\n" .
          "This file is not meant to be read by a web\n" .
          "browser directly.  Instead you\'re meant to copy\n" .
          "the URL for the file, which is:\n\n" .

          "http://live.civl.ca:8085/rss/$rss_file\n\n" .

          "and paste it into your RSS reader or podcast program.\n" .
          "Please also tune in on CIVL Radio's live feed at:\n\n" .
          "http://civl.ca/listen\n" .
          "Thanks CBCRadio 3 for the formatting help\n" .
          "-->\n\n" .

          "<rss xmlns:itunes=\"http://www.itunes.com/dtds/podcast-1.0.dtd\" version=\"2.0\">\n" .
          "  <channel>\n" .
          "    <title>$album</title>\n" .
          "    <link>http://civl.ca</link>\n" .
          "    <language>en-ca</language>\n" .
          "    <copyright>UCFV Campus and Community Radio Society $mon $year</copyright>\n" .
          "    <description>$comm</description>\n" .
          "    <category>Music</category>\n"  .
          "    <itunes:subtitle>\"On CIVL Radio\"</itunes:subtitle>\n" .
          "    <itunes:author>$artist</itunes:author>\n" .
          "    <itunes:summary>$comm</itunes:summary>\n" .
          "    <itunes:owner>\n" .
          "    <itunes:name>$artist</itunes:name>\n" .
#         "      <itunes:email>NOT IMPLEMENTED</itunes:email>\n" .
          "    </itunes:owner>\n" .
          "    <itunes:category text=\"Music\">\n" .
          "      <itunes:category text=\"College Radio\"/>\n" .
          "    </itunes:category>\n\n\n\n\n\n\n\n\n\n\n" .
          "  </channel>\n" .
          "</rss>";
  print("writing rss header information to $rss_dir/$rss_file");
  fwrite(fopen("$rss_dir/$rss_file",'w'),$data);

}


function addRSSItem($rss_dir, $rssFile, $firstItem, $item) {
  // Backup File
  if(!copy("$rss_dir/$rssFile", "$rss_dir/backup.rss")) die(fwrite(STDOUT,'Backup Failed!\n'));
  
  // Store file contents in array
  $arrFile = file("$rss_dir/$rssFile");
 
  // Open file for output
  if(($fh = fopen($rss_dir . "/" . $rssFile,'w')) === FALSE){
    die('Failed to open file for writing!');
  }
 
  // Set Counters
  $currentLine = 0;
  $cntFile = count($arrFile);
 
  // Write contents, inserting $item as first item
  while( $currentLine <= $cntFile){
    if ($currentLine == $firstItem) fwrite($fh, $item);
    fwrite($fh, $arrFile[$currentLine]);
    $currentLine++;
  }

  // Delete Backup
  unlink ("$rss_dir/backup.rss");

}

And Makehtml runs every hour at :07

<?php

$dir
="/usr/rss";
$htmlfile="index.html";
$url="http://live.civl.ca:8085/rss";
$data="<html>\n<head>\n<title>Index of CIVL Radio Podcasts</title>\n</head>\n<body>\n"; #data to be written to the file

if (is_dir($dir)) {
  if (
$dh = opendir($dir)) {
    while ((
$file = readdir($dh)) !== false) {
      if (
strstr($file,"xml")){
       
$data=$data . "<a href=\"$url/$file\">$file</a><br>\n";
      }
    }
   
closedir($dh);
  }
}

$data = $data . "</body>\n</html>";
fwrite(fopen("$dir/$htmlfile",'w'),$data);
?>
 

Ha, well, if you want a linked version of the code so that it's coloured properly, you can view it for a short time (until I move to calgary), at http://future.civl.ca/~gate/scripts/

I'm not sure if this is going to be of much use to you guys since you're running the drupal engine and not a standalone system, but perhaps someone wants to take the code and port it over to running on drupal.

Doug McLean
President,
CIVL Radio

Radio

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week