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.

Google video is a little more involved, but still rather simple. First thing you must do is load the video feed:

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=”” 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();
echo $match[1];

To retrieve the first youtube video ID from html:

$match = array();
echo $match[1];