Getting a Blip.tv Thumbnail from the Embed URL

Here’s a function I wrote to get the thumbnail URL for a Blip.tv video when all you have is the embed URL. It can parse the embed URL out of a larger block of text (e.g., a blog post). It doesn’t use their API, which makes it easier to setup, but also means it might break in the future if they change things.

/**
* Gets a Blip.tv video thumbnail URL when given a embed URL
* $author Ian Dunn 
* @param string $embedURL e.g., http://blip.tv/play/hZBPgqbXeQA
* @param string $parse If equal to 'parse' then we'll try to parse the URL out of a larger block of text (like a post's $content), otherwise we'll assume it's the exact URL
* @return mixed If successful, the URL string e.g., http://a.images.blip.tv/Brandon-bgintro7369.jpg. If unsuccessful, boolean false
*/
function getBlipThumbnail($embedURL, $parse = '')
{
	$urlStart = strpos($embedURL, 'blip.tv/play/');
	if($urlStart === false)
		return false;

	// Parse out the embed URL if needed
	if($parse == 'parse')
	{
		$substrLength = strpos($embedURL, '"', $urlStart) - $urlStart;
		$embedURL = 'http://' . substr($embedURL, $urlStart, $substrLength);
	}

	// Open the redirect page
	$handler = curl_init();
	curl_setopt($handler, CURLOPT_URL, $embedURL);
	curl_setopt($handler, CURLOPT_RETURNTRANSFER, 1);
	$redirectPage = curl_exec($handler);
	curl_close($handler);

	// Parse out the ID
	$urlStart = strpos($redirectPage, '?file=');
	$substrLength = strpos($redirectPage, '&', $urlStart) - $urlStart;
	$redirectURL = substr($redirectPage, $urlStart, $substrLength);
	$id = substr($redirectURL, strrpos($redirectURL, '%2F') + 3);

	// Get video details
	$handler = curl_init();
	curl_setopt($handler, CURLOPT_URL, 'http://blip.tv/rss/'. $id);
	curl_setopt($handler, CURLOPT_RETURNTRANSFER, 1);
	$videoRSS = curl_exec($handler);
	curl_close($handler);

	// Parse out the thumbnail URL
	$urlStart = strpos($videoRSS, '');

	if($urlStart !== false)
	{
		$substrLength = strpos($videoRSS, '', $urlStart) - $urlStart;
		$thumbnailURL = substr($videoRSS, $urlStart + 21, $substrLength - 21);
	}
	else
	{
		$urlStart = strpos($videoRSS, 'media:thumbnail url="');

		if($urlStart !== false)
		{
			$substrLength = strpos($videoRSS, '"/>', $urlStart) - $urlStart;
			$thumbnailURL = substr($videoRSS, $urlStart + 21, $substrLength - 21);
		}
		else
			return false;
	}

	return $thumbnailURL;
}

2 thoughts on “Getting a Blip.tv Thumbnail from the Embed URL

  1. Hi Ian, this piece of code may need a little tweaking in the wake of the recent upgrade to Blip.tv’s URL embed structure.

    In the meantime, I’ve used a custom field and a WordPress function to grab the thumbnail image.


    function getBliptvThumb() {
    global $post;
    $blipID = get_post_meta($post->ID, 'Video', true);
    $xml = simplexml_load_file("http://blip.tv/players/episode/$blipID?skin=rss");
    $result = $xml->xpath("/rss/channel/item/media:thumbnail/@url");
    $thumbnail = (string) $result[0]['url'];
    echo '';
    }

    Store your Blip.tv video ID (the ‘AYLInREC’ bit of http://blip.tv/play/AYLInREC) in a custom field and then just call the getBliptvThumb() function somewhere in your theme.

    Hope this helps someone.

Leave a Reply to George Cancel reply

Your email address will not be published. Required fields are marked *