I want to display an image thumbnail on a page, so that a user can click on it and the video will pop up in a lightbox. But how do I get a thumbnail image of the video I want?
This article shows you how you can quickly and easily use a Youtube URL to get video thumbnails.
There is a demo of this here: http://www.mccran.co.uk/examples/youtube-thumbs/
Youtube has an API you can use to pull down data on a video, including several thumbnail images, if you don't want to get your head around that they also have a URL you can use.
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
http://img.youtube.com/vi/ video id /#.jpg
use 1,2,3 for thumbs
use 0 for a large image
1http://img.youtube.com/vi/ video id /#.jpg
2use 1,2,3 for thumbs
3use 0 for a large image
You can use the URL above along with the video ID from the 'embed' code on Youtube and either 1,2,3 (replace the #) to get a thumbnail image. The thumbnails seem to be roughly from the start, middle and end based on the number.
I have put together a small CFC that accepts a video id and returns a structure of thumbnail URL's.
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
<cffunction name="get" access="public" output="false" returntype="struct" hint="I get the url paths for the you tube video sent in">
<cfargument name="videoId"
type="string"
required="true"
hint="Video id to use">
<cfset var youTubeUrl = "http://img.youtube.com/vi/">
<cfset var thumbData = structNew()>
<cfset thumbData['thumb1'] = youTubeUrl & arguments.videoId & "/1.jpg">
<cfset thumbData['thumb2'] = youTubeUrl & arguments.videoId & "/2.jpg">
<cfset thumbData['thumb3'] = youTubeUrl & arguments.videoId & "/3.jpg">
<cfset thumbData['thumbWidth'] = "120">
<cfset thumbData['thumbHeight'] = "90">
<cfset thumbData['full'] = youTubeUrl & arguments.videoId & "/0.jpg">
<cfset thumbData['fullWidth'] = "480">
<cfset thumbData['fullHeight'] = "360">
<cfreturn thumbData>
</cffunction>
1<cffunction name="get" access="public" output="false" returntype="struct" hint="I get the url paths for the you tube video sent in">
2
3<cfargument name="videoId"
4 type="string"
5 required="true"
6 hint="Video id to use">
7
8<cfset var youTubeUrl = "http://img.youtube.com/vi/">
9<cfset var thumbData = structNew()>
10
11<cfset thumbData['thumb1'] = youTubeUrl & arguments.videoId & "/1.jpg">
12<cfset thumbData['thumb2'] = youTubeUrl & arguments.videoId & "/2.jpg">
13<cfset thumbData['thumb3'] = youTubeUrl & arguments.videoId & "/3.jpg">
14
15<cfset thumbData['thumbWidth'] = "120">
16<cfset thumbData['thumbHeight'] = "90">
17
18<cfset thumbData['full'] = youTubeUrl & arguments.videoId & "/0.jpg">
19
20<cfset thumbData['fullWidth'] = "480">
21<cfset thumbData['fullHeight'] = "360">
22
23 <cfreturn thumbData>
24 </cffunction>
There is a demo of this here: http://www.mccran.co.uk/examples/youtube-thumbs/
Maybe for a next version I may add cfhttp calls to download the images and store them locally.
Its this one? http://youtubecfc.riaforge.org/
So, I'd refactor and have a size argument ('small' or 'large') passed in and then return the appropriate 120x90 or 480x360 thumbnail image.
Note: default.jpg/0.jpg gets the sd/hd default image (not hd0.jpg)
Good tips! Thanks!
I wasn't aware of the HD versions, I'll have a tinker with it and see what the difference in image quality is.