Previous Post: Irreducible complexity of bacterial flagellum debunked
Next Post: Great quotes: Senator Barry Goldwater - 1981
Oct 06, 2008 at 10:01 pm - 8 Comments
Today I modified my blog topic listing layout from just a simple link list to a more traditional blog style with a summary for each topic. It was a major improvement but I felt that it was a little too bland and decided that thumbnails of the videos and images contained within each topic would make it more appealing.
Now, I have over 300 posts and theres no way I would manually go through each one make the thumbnails. So naturally, I wrote code to do it for me :) The thumbnails would need to originate from either pictures or videos from within the blog post. It took me a little searching to figure out where to pull the video thumbnails from so I am posting the information here in case it can help others.
For youtube, all you need to do is use the following URL with the correct video ID.
http://img.youtube.com/vi/VIDEO_ID/default.jpg
Google video is a little more involved, but still rather simple. First thing you must do is load the video feed:
http://video.google.com/videofeed?docid=VIDEO_ID
This will return an XML document with details of that particular video. Included in which is the url for the thumbnail. The section of the DOM you are interested in looks like this:
<media:thumbnail url="http://video.google.com/ThumbnailServer2?app=vss&contentid=2922884fc6e928fa&offsetms=755000&itag=w320&lang=en&sigh=-LLFkDhQ-QF3xH-vQqeLKTk0Iko" width="320" height="240"/>
To access this data you can either parse out the whole XML document or simply use a regular expression, which is what I did. Here is the code I used which appears to work just fine:
$match = array();
preg_match("/media:thumbnail url=\"([^\"]\S*)\"/siU",$XML_SOURCE,$match);
echo $match[1][0];
If you are extracting the video IDs from html containing the flash embed, these two bits of code might help.
To retrieve the first google video ID from html:
$match = array();
preg_match("/http:\\/\\/video\.google\.com\\/googleplayer\\.swf\\?docid=([^\"][a-zA-Z0-9-_]+)[&\"]/siU",$HTML_SOURCE,$match);
echo $match[1];
To retrieve the first youtube video ID from html:
$match = array();
preg_match("/http:\\/\\/www\.youtube\.com\\/v\\/([^\"][a-zA-Z0-9-_]+)[&\"]/siU",$HTML_SOURCE,$match);
echo $match[1];

d3vlabs Nov 02, 2008
is there a way to get a bigger thumbnail?
d3vlabs Nov 02, 2008
to answer my own question
http://img.youtube.com/vi/VIDEO-ID/0.jpg
will get u a 320x240 thumb
Vlad Dec 30, 2008
Hi.
I'm not much of a coder but it appears to me that i have similar problem with my thumbs..I got those feeds from youtube but template for some reason displays only some of them. In cache folder where images are supposed to be there is only text file connected with video. SO i don't really get it how to implement your solution? where this code goes?can you please describe me step by step?Thanks in advance!
Arnold Jan 14, 2009
Thanks for the info! :D
I was able to use this to help me with my video directory modification that I installed on my forums. I can pull videos straight from YouTube into my site. Check it out here....
http://www.waltdisneyboards.com/videos
Dirk Jan 21, 2009
same here, I have trouble grabbing the thumbnails for youtube.
Ozzy Jan 23, 2009
Try this:
parse_str(parse_url($embedurl,PHP_URL_QUERY), $out);
print_r($out);
echo $out['v']; // return ID of video
BTW nice resource
Christopher Ross Apr 27, 2009
Hey Scott, thanks a lot for posting this! I'm working on a pretty big WordPress/YouTube project and this saved me a lot of time :)
prem ypi May 01, 2009
Youtube thumbnails are anyways easy to extract out. Real problem lied in finding google thumbnail. Thanks for giving pointer to the same.