For instance I have a blog post that has the following iframe
.
This function work with all types of youtube links & domains such as
https://www.youtube.com/watch?v=WZKW2Hq2fks
https://youtu.be/WZKW2Hq2fks
https://www.youtube.com/embed/WZKW2Hq2fks
https://youtu.be/WZKW2Hq2fks?t=66
And you can get the quality you want.
To use it:
// quality options: low, medium, high, max | default is max.
var thumbnail = get_youtube_thumbnail('https://youtu.be/WZKW2Hq2fks', 'max');
console.log(thumbnail);
function get_youtube_thumbnail(url, quality){
if(url){
var video_id, thumbnail, result;
if(result = url.match(/youtube\.com.*(\?v=|\/embed\/)(.{11})/))
{
video_id = result.pop();
}
else if(result = url.match(/youtu.be\/(.{11})/))
{
video_id = result.pop();
}
if(video_id){
if(typeof quality == "undefined"){
quality = 'high';
}
var quality_key = 'maxresdefault'; // Max quality
if(quality == 'low'){
quality_key = 'sddefault';
}else if(quality == 'medium'){
quality_key = 'mqdefault';
} else if (quality == 'high') {
quality_key = 'hqdefault';
}
var thumbnail = "http://img.youtube.com/vi/"+video_id+"/"+quality_key+".jpg";
return thumbnail;
}
}
return false;
}