Friday, May 15, 2009

jYouTube - jQuery YouTube Thumbnail plugin. Gets any YouTube video’s image/thumbnail

Writing short scripts and jQuery tips on Fridays became some kind of habit. So this Friday I will give you a small jQuery utility function that will give you a way to get any YouTube video’s still image or in other words any YouTube video’s thumbnail. All you need to know is that YouTube video’s URL address or at least video’s YouTube ID.

Couple of months ago I wrote a javascript function that does just that, gets YouTube video’s thumbnail. Today, I am rewriting that code as a jQuery utility function and releasing it as jQuery plugin called jYouTube.

Download jQuery Youtube plugin - jYouTube

It takes any YouTube video URL or video ID as first parameter and thumbnail size (big or small) as the second parameter. Utility returns that video’s screenshot URL.

Here is how you use jYouTube plugin in your own code:

// Returns big screenshot by video id
$.jYoutube('rSUfWXcNAOw');

// Returns small thumbnail by YouTube video URL
$.jYoutube('http://www.youtube.com/watch?v=rSUfWXcNAOw', 'small');
See the plugin in action on jQuery YouTube demo page.

So here is the final jQuery YouTube plugin code for those who are interested:

$.extend({
  jYoutube: function( 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 "http://img.youtube.com/vi/"+vid+"/2.jpg";
    }else {
      return "http://img.youtube.com/vi/"+vid+"/0.jpg";
    }
  }
});
You might also be interested in jQuery Twitter plugin.
Gets any Twitter user information and tweets (does not require API key).

13 comments:

  1. very good and helpful

    thx alot its realy help me

    ReplyDelete
  2. i love you man! it works like a charm!

    ReplyDelete
  3. hi! this example was great. however, I cannot generate youtube video thumbnail when the second param of the url has feature.

    http://www.youtube.com/watch?v=t8BhPOlQ5A4&feature=related

    ReplyDelete
  4. why is this a jquery plugin when it doesn't require a selector.

    ReplyDelete
  5. Hi,

    Im not sure how to actually write this on my page.

    What is the full markup when the link is provided in the page, not through the form.

    Thanks

    Rob

    ReplyDelete
  6. Hi! How can I use it on my blog? I would like to insert a script to every pages of the www.salsatune.hu witch create a thumbnail from the embedded YouTube video by automatically.

    ReplyDelete
  7. Thanks. It's what i am looking for. I had a script but not still work for IE9

    ReplyDelete
  8. Hello, will the youtube video play on my blog if I use this code? Thanks in advance...

    Joaquin

    ReplyDelete
  9. Hi, I'd love to try out your plugin, but the jquery plugin repo has had massive problems with killing spam, so they also accidentally deleted all the plugins. More info here: http://blog.jquery.com/2011/12/08/what-is-happening-to-the-jquery-plugins-site/

    Maybe you could make your plugin available directly on this page?

    ReplyDelete
  10. Its really easy and helpful. Thanks a lot.

    ReplyDelete
  11. This is nifty! Thanks for sharing.

    ReplyDelete