Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AlexOcculate/97078c77534342efb1ada3105619d33e to your computer and use it in GitHub Desktop.
Save AlexOcculate/97078c77534342efb1ada3105619d33e to your computer and use it in GitHub Desktop.
Get Youtube Video Thumbnail with JavaScript.
var Youtube = (function () {
'use strict';
var video, results;
var getThumb = function (url, size) {
if (url === null) {
return '';
}
size = (size === null) ? 'big' : size;
results = url.match('[\\?&]v=([^&#]*)');
video = (results === null) ? url : results[1];
if (size === 'small') {
return 'http://img.youtube.com/vi/' + video + '/2.jpg';
}
return 'http://img.youtube.com/vi/' + video + '/0.jpg';
};
return {
thumb: getThumb
};
}());
//Example of usage:
var thumb = Youtube.thumb('http://www.youtube.com/watch?v=F4rBAf1wbq4', 'small');
console.log(thumb);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment