<?php
/*****************************************************************
* Plugin: YouTube
* Description: Replaces YouTube player with FlowPlayer which loads
* videos through the proxy.
* Author: jsmith
******************************************************************/
// CONFIG
$_YTconfig['timeout'] = 600; // Max time to spend downloading video (seconds)
$_YTconfig['player_url'] = GLYPE_URL . '/plugins/flowplayer.swf'; // URL to flash player
$_YTconfig['remove_js'] = false; // Remove original scripts from YouTube?
$_YTconfig['no_queue'] = true; // Allows other transfers while video downloads
// END OF CONFIG
// Pass config to Glype
$toSet[CURLOPT_TIMEOUT] = $_YTconfig['timeout'];
$options['stripJS'] = $_YTconfig['remove_js'];
$CONFIG['queue_transfers'] = !$_YTconfig['no_queue'];
// Pre-parsing applied BEFORE main proxy parser
function preParse($input, $type)
{
if ($type == 'html') {
// Look for video URL
if (preg_match('#"fmt_url_map":\s*"([^"]+)"#', $input, $urlMap)) {
// Extract URLs - guess these are quality indicators and corresponding URLs? Not sure though.
$urls = explode(',', $urlMap[1]);
foreach ($urls as $url) {
// Split by quality|url
$bits = explode('|', $url);
// Ignore if failed to find both parts
if (!isset($bits[1])) {
continue;
}
// Save values
$quality[$bits[0]] = $bits[1];
}
// Pick lowest quality and use that
if (isset($quality)) {
ksort($quality);
define('VIDEO_URL', rawurldecode(current($quality)));
}
}
}
return $input;
}
// Post-parsing applied AFTER main proxy parser.
function postParse($input, $type)
{
// Should we replace the video player? If HTML page and we have a video URL, yes.
if ($type == 'html' && defined('VIDEO_URL')) {
// Fetch config
global $_YTconfig;
// Prepare proxyed URL
$flvUrl = rawurlencode(proxifyURL(VIDEO_URL));
// Create HTML for own player
$playerHtml = <<<HTML
<object id="player" width="620" height="380" data="{$_YTconfig['player_url']}"
type="application/x-shockwave-flash">
<param name="movie" value="{$_YTconfig['player_url']}" />
<param name="allowfullscreen" value="true" />
<param name="flashvars"
value='id=player&config={"clip":"{$flvUrl}"}' />
</object>
HTML;
$playerHtml .= '<a href="' . proxifyURL(VIDEO_URL) . '">Video</a>';
// Replace YouTube player with our own player
$input = preg_replace('#<div id="watch-player".*?</div>#s', "<div>$playerHtml</div>", $input, 1);
}
return $input;
} |