Scott Klarr Jr
How to grab thumbnails for youtube and google video
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];
Additional Expressions
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];
This topic has the following tags:
Last 5 Linkbacks
- Oct 07, 2008www.technospot.net



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.
kurrija Sep 22, 2009
Regarding YT, there is also a possibility to get the images from different domains (interesting if using AJAX) plus there are 3 different images availible in high-quality.
The following pattern should include all the possibilities there are where $1 represents the ID of you YT video (yes i didn't escape any regexp relevant characters in favor of legibility, but i think it's still clear)
http://i[1-4]?.ytimg.com/vi/$1/hq[1-3].jpg
The post about google vids helped me a lot though ... thanks for that one!
dave Oct 25, 2009
thanks for this very useful info. im wondering if you or any readers know where the thumbnail for videos that shows up in SERPs comes from? Currently on one of my sites, a thumbnail is showing up in SERPs but I actually have no idea where Google has taken it from. I'm looking at introducing custom thumbnails using
dave Oct 25, 2009
oops code got cut off, im looking at using custom thumbnails with the link rel image_src tag, and wondering if this will show in SERPs as the thumbnail for that page? Any help is greatly appreciated! Cheers!
Victoria Nov 30, 2009
where do i find the youtube video ID? Im trying to create thumbnails for a google site that i already have videos on... help?
Jonas Jan 21, 2010
Thanks a bunch for making this post - it gave me the answer I was looking for! Keep up the good work :)