Skip to content

Instantly share code, notes, and snippets.

@micalexander
Created October 14, 2014 21:53
Show Gist options
  • Save micalexander/ed6bbd4d2433d7fcbf43 to your computer and use it in GitHub Desktop.
Save micalexander/ed6bbd4d2433d7fcbf43 to your computer and use it in GitHub Desktop.
jquery:snippet:Get youtube or vimeo thumbnail url
function get_video_thumb(url, callback){
var id = get_video_id(url);
if (id['type'] == 'y') {
return processYouTube(id);
} else if (id['type'] == 'v') {
$.ajax({
url: 'http://vimeo.com/api/v2/video/' + id['id'] + '.json',
dataType: 'jsonp',
success: function(data) {
callback({type:'v', id:id['id'], url:data[0].thumbnail_large});
}
});
}
function processYouTube(id) {
if (!id) {
throw new Error('Unsupported YouTube URL');
}
callback({type:'y',id:id['id'],url:'http://i2.ytimg.com/vi/'+id['id'] + '/hqdefault.jpg'});
}
}
function get_video_id(url) {
var id;
var a;
if (url.indexOf('youtube.com') > -1)
{
if (url.indexOf('v=') > -1) {
id = url.split('v=')[1].split('&')[0];
}
else if (url.indexOf('embed') > -1){
id = url.split('embed/')[1].split('?')[0];
};
return processYouTube(id);
}
else if (url.indexOf('youtu.be') > -1)
{
id = url.split('/')[1];
return processYouTube(id);
}
else if (url.indexOf('vimeo.com') > -1)
{
if (url.match(/https?:\/\/(www\.)?vimeo.com\/(\d+)($|\/)/))
{
id = url.split('/')[3];
}
else if (url.match(/^vimeo.com\/channels\/[\d\w]+#[0-9]+/))
{
id = url.split('#')[1];
}
else if (url.match(/vimeo.com\/groups\/[\d\w]+\/videos\/[0-9]+/))
{
id = url.split('/')[4];
}
else if (url.match(/player.vimeo.com\/video\/[0-9]+/))
{
id = url.split('/')[2];
}
else {
throw new Error('Unsupported Vimeo URL');
}
}
else
{
throw new Error('Unrecognised URL');
}
a = {type:'v',id:id};
return a;
function processYouTube(id) {
if (!id) {
throw new Error('Unsupported YouTube URL');
}
a = {type:'y',id:id};
return(a); // default.jpg OR hqdefault.jpg
}
}
function callback(video)
{
$('.text-slideshow').prepend('<img src="' + video.url + '">');
console.log(video);
}
get_video_thumb('[http://vimeo-or-vimeo-url]', callback);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment