Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a YouTube thumbnail from a YouTube iframe?

For instance I have a blog post that has the following iframe.

<iframe width="420" height="315" src="//www.youtube.com/embed/1sIWez9HAbA" frameborder="0" allowfullscreen></iframe>

How can I extract the thumbnail from this iframe?

like image 393
Hamza Dhamiya Avatar asked Sep 08 '13 08:09

Hamza Dhamiya


People also ask

How do I get the embed code for a YouTube thumbnail?

Just go to the following URL: http://i1.ytimg.com/vi/<insert-YouTube -video-id-here>/[Thumnail-Size]. jpg. Now, you will need to fetch the videoId of either from the video Url or from the embed code of YouTube video.


2 Answers

YouTube thumbnails

YouTube thumbnails can be found in this standard path:

http://img.youtube.com/vi/[video-id]/[thumbnail-number].jpg
  • [video-id] is the YouTube video ID, e.g. 1sIWez9HAbA.
  • [thumbnail-number] is the number of the thumbnail of the 4 each video usually has, e.g. 0.

Getting the thumbnail from an iframe

So, based on the iframe's src attribute you may construct the URL of the thumbnail directly.

For example, using jQuery:

var iframe           = $('iframe:first');
var iframe_src       = iframe.attr('src');
var youtube_video_id = iframe_src.match(/youtube\.com.*(\?v=|\/embed\/)(.{11})/).pop();

if (youtube_video_id.length == 11) {
    var video_thumbnail = $('<img src="//img.youtube.com/vi/'+youtube_video_id+'/0.jpg">');
    $(body).append(video_thumbnail);
}

Note this example checks the URL of the iframe for a valid YouTube video ID and assumes it to be 11 characters long, which is the de facto standard.

See jsFiddle Demo

like image 132
Boaz Avatar answered Oct 18 '22 02:10

Boaz


Get Youtube thumbnail images using below urls

Low quality

 https://img.youtube.com/vi/[video-id]/sddefault.jpg

medium quality

 https://img.youtube.com/vi/[video-id]/mqdefault.jpg

High quality

http://img.youtube.com/vi/[video-id]/hqdefault.jpg

maximum resolution

http://img.youtube.com/vi/[video-id]/maxresdefault.jpg

example: https://img.youtube.com/vi/OYr-KPboPDw/hqdefault.jpg

like image 43
Gopala raja naika Avatar answered Oct 18 '22 00:10

Gopala raja naika