Youtube Live & JW Player – Custom Playlist

Youtube recently allowed regular users to create live events using Youtube Live. It seems like a great service if you are looking for an affordable (Youtube is currently free) streaming service for church or whatever else you want to do.

I couldn’t find anything that would fit my needs so I decided to try Youtube Live and create a custom playlist for JW Player. The playlist displays an upcoming event (the next one in sequence) at the top and recorded videos in a playlist under that.

Here is the JW Player config:


<div id="player">Player</div>

<script src="http://jwpsrv.com/library/YOUR_JW_KEY.js"></script>
<script>
    jwplayer("player").setup({
        primary: "flash",
        aspectratio: "16:9",
        height: 354,
        width: 860,
        listbar: {
           position: "right",
           size: 230,
           layout: "extended"
        },
        playlist: "playlist.php"
    });
</script>

And the playlist that is being generated in playlist.php:


<?php
$youtube_user_id = "" // Can be found in your youtube account settings;
$youtube_playlist_id = "" // View the playlist in the browser and get the ID (starts with PL);

header('Content-type: application/xml');
date_default_timezone_set('America/New_York');
echo "<rss version=\"2.0\" xmlns:jwplayer=\"http://rss.jwpcdn.com/\">";

$live_event_ids = array();

function sort_by_date( $a, $b ) {
    return $a["date"] - $b["date"];
}

function upcoming_live_event() {
  global $live_event_ids;

  $feed = "http://gdata.youtube.com/feeds/api/users/" . $GLOBALS['youtube_user_id'] . "/live/events?v=2";
  $feed = file_get_contents($feed);
  $feed = str_replace('<yt:when', '<ytwhen', $feed);

  $rss = simplexml_load_string($feed);

  $live_events = array();

  foreach ($rss->entry as $item) {
    preg_match("/live\/videos\/(.*)\?v/", $item->content['src'], $matches);
    $start_time = $item->ytwhen['start'];
    $end_time = $item->ytwhen['end'];
  
    if (strtotime($end_time) > time()) {
      $live_events[] = array(
        'id' => $matches[1],
        'title' => $item->title,
        'date' => strtotime($start_time)
      );
      $live_event_ids[] = $matches[1];
    }
  }

  usort($live_events, "sort_by_date");
  
  return $live_events[0];
}

function recorded_videos() {
  global $live_event_ids;
  
  $feed = "http://gdata.youtube.com/feeds/api/playlists/" . $GLOBALS['youtube_playlist_id'] . "?alt=rss";
  $feed = file_get_contents($feed);

  $rss = simplexml_load_string($feed);
  
  $videos = array();
  
  foreach ($rss->channel->item as $item) {
    $link = str_replace('&feature=youtube_gdata', '', $item->link);
    preg_match("/\?v\=(.*)$/", $link, $matched);

    if (in_array($matched[1], $live_event_ids)) {
      continue;
    }
    
    $videos[]= array(
      'id' => $matched[1],
      'title' => $item->title,
      'date' => strtotime($item->pubDate),
      'description' => $item->description,
      'date' => ''
    );
  }
  
  usort($videos, "sort_by_date");
  
  return array_reverse($videos);
}

echo "<channel>";

$live_event = upcoming_live_event();

if (isset($live_event)) {
  echo "<item>";
  echo "<title>Live Video</title>";
  echo "<description><![CDATA[" . date("l, M jS", $live_event['date']) . "
" . date("g A", $live_event['date']) . " EST]]></description>"; echo "<jwplayer:image>YOUR_IMAGE.JPG</jwplayer:image>"; echo "<jwplayer:source type=\"youtube\" file=\"http://youtube.com/watch?v=" . $live_event['id'] . "\"/>"; echo "</item>"; } foreach (recorded_videos() as $video) { echo "<item>"; echo "<title>" . $video['title'] . "</title>"; echo "<description>" . $video['description'] . "</description>"; echo "<jwplayer:source type=\"youtube\" file=\"http://youtube.com/watch?v=" . $video['id'] . "\"/>"; echo "</item>"; } echo "</channel>"; echo "</rss>"; ?>

Hope this helps someone.

Leave a Reply