How to get a YouTube video screenshot image (thumbnail) URL using JavaScript

YouTube is a great video service by Google. As with any other product Google makes it easy to use its content on your own site. You can embed a YouTube video on your own site or query YouTube API (if you have a developer key). But what if you don't want to embed a flash video player and you only need an image of the video. The following function returns a YouTube video's thumbnail URL. Nothing more, nothing less.

Update: Now available as jQuery YouTube plugin – jYouTube.

The javascript function:

function getScreen( url, size )
  if(url === null){ return ""; }

  size = (size === null) ? "big" : size;
  var vid;
  var results;

  results = url.match("[\\?&]v=([^&#]*)");

  vid = ( results === null ) ? url : results[1];

  if(size == "small"){
    return ""+vid+"/2.jpg";
  }else {
    return ""+vid+"/0.jpg";

You can pass a YouTube video URL or video id and the function will return a path to the video image. The second function argument is optional. You can specify the size of returned image. It can be big (320x240) or small (128x96), defaults to big.

Here is an example usage:

imgUrl_big   = getScreen("uVLQhRiEXZs"); 
imgUrl_small = getScreen("uVLQhRiEXZs", 'small');
See the plugin in action on jQuery YouTube demo page.