If I have a YouTube video URL, is there any way to use PHP and cURL to get the associated thumbnail from the YouTube API?
In YouTube API V3 we can also use these URLs for obtaining thumbnails... They are classified based on their quality.
https://i1.ytimg.com/vi/<insert-youtube-video-id-here>/default.jpg - default
https://i1.ytimg.com/vi/<insert-youtube-video-id-here>/mqdefault.jpg - medium
https://i1.ytimg.com/vi/<insert-youtube-video-id-here>/hqdefault.jpg - high
https://i1.ytimg.com/vi/<insert-youtube-video-id-here>/sddefault.jpg - standard
And for the maximum resolution..
https://i1.ytimg.com/vi/<insert-youtube-video-id-here>/maxresdefault.jpg
One advantage of these URLs over the URLs in the first answer is that these URLs don't get blocked by firewalls.
Save this code in empty .php file and test it.
<img src="<?php echo youtube_img_src('9bZkp7q19f0', 'high');?>" />
<?php
// Get a YOUTUBE video thumb image's source url for IMG tag "src" attribute:
// $ID = YouYube video ID (string)
// $size = string (default, medium, high or standard)
function youtube_img_src ($ID = null, $size = 'default') {
switch ($size) {
case 'medium':
$size = 'mqdefault';
break;
case 'high':
$size = 'hqdefault';
break;
case 'standard':
$size = 'sddefault';
break;
default:
$size = 'default';
break;
}
if ($ID) {
return sprintf('https://img.youtube.com/vi/%s/%s.jpg', $ID, $size);
}
return 'https://img.youtube.com/vi/ERROR/1.jpg';
}
Thanks.
I think their are a lots of answer for thumbnail, but I want to add some other URLs to get YouTube thumbnail very easily. I am just taking some text from Asaph's answer. Here are some URLs to get YouTube thumbnails:
https://ytimg.googleusercontent.com/vi/<insert-youtube-video-id-here>/default.jpg
For the high quality version of the thumbnail use a URL similar to this:
https://ytimg.googleusercontent.com/vi/<insert-youtube-video-id-here>/hqdefault.jpg
There is also a medium quality version of the thumbnail, using a URL similar to the high quality:
https://ytimg.googleusercontent.com/vi/<insert-youtube-video-id-here>/mqdefault.jpg
For the standard definition version of the thumbnail, use a URL similar to this:
https://ytimg.googleusercontent.com/vi/<insert-youtube-video-id-here>/sddefault.jpg
For the maximum resolution version of the thumbnail use a URL similar to this:
https://ytimg.googleusercontent.com/vi/<insert-youtube-video-id-here>/maxresdefault.jpg
Here's the top answer optimized for manual use. The video ID token without separators enables selecting with a double click.
Each YouTube video has four generated images. They are predictably formatted as follows:
https://img.youtube.com/vi/YOUTUBEVIDEOID/0.jpg
https://img.youtube.com/vi/YOUTUBEVIDEOID/1.jpg
https://img.youtube.com/vi/YOUTUBEVIDEOID/2.jpg
https://img.youtube.com/vi/YOUTUBEVIDEOID/3.jpg
The first one in the list is a full size image and others are thumbnail images. The default thumbnail image (ie. one of 1.jpg
, 2.jpg
, 3.jpg
) is:
https://img.youtube.com/vi/YOUTUBEVIDEOID/default.jpg
For the high quality version of the thumbnail use a URL similar to this:
https://img.youtube.com/vi/YOUTUBEVIDEOID/hqdefault.jpg
There is also a medium quality version of the thumbnail, using a URL similar to the HQ:
https://img.youtube.com/vi/YOUTUBEVIDEOID/mqdefault.jpg
For the standard definition version of the thumbnail, use a URL similar to this:
https://img.youtube.com/vi/YOUTUBEVIDEOID/sddefault.jpg
For the maximum resolution version of the thumbnail use a URL similar to this:
https://img.youtube.com/vi/YOUTUBEVIDEOID/maxresdefault.jpg
All of the above URLs are available over HTTP too. Additionally, the slightly shorter hostname i3.ytimg.com
works in place of img.youtube.com
in the example URLs above.
Alternatively, you can use the YouTube Data API (v3) to get thumbnail images.
YouTube provides us the four generated images for every video through the Data API (v3), for example,
https://i.ytimg.com/vi/V_zwalcR8DU/maxresdefault.jpg
https://i.ytimg.com/vi/V_zwalcR8DU/sddefault.jpg
https://i.ytimg.com/vi/V_zwalcR8DU/hqdefault.jpg
https://i.ytimg.com/vi/V_zwalcR8DU/mqdefault.jpg
As per this, you need to phrase your URL like this -
www.googleapis.com/youtube/v3/videos?part=snippet&id=`yourVideoId`&key=`yourApiKey`
Now change your video ID and your API key to the your respective video-id and api-key and its response will be a JSON output providing you the four links in the thumbnails of snippet variable (if all are available).
Method 1:
You can find all information for a YouTube video with a JSON page which has even "thumbnail_url", http://www.youtube.com/oembed?format=json&url={your video URL goes here}
Like final URL look + PHP test code
$data = file_get_contents("https://www.youtube.com/oembed?format=json&url=https://www.youtube.com/watch?v=_7s-6V_0nwA");
$json = json_decode($data);
var_dump($json);
object(stdClass)[1]
public 'width' => int 480
public 'version' => string '1.0' (length=3)
public 'thumbnail_width' => int 480
public 'title' => string 'how to reminder in window as display message' (length=44)
public 'provider_url' => string 'https://www.youtube.com/' (length=24)
public 'thumbnail_url' => string 'https://i.ytimg.com/vi/_7s-6V_0nwA/hqdefault.jpg' (length=48)
public 'author_name' => string 'H2 ZONE' (length=7)
public 'type' => string 'video' (length=5)
public 'author_url' => string 'https://www.youtube.com/channel/UC9M35YwDs8_PCWXd3qkiNzg' (length=56)
public 'provider_name' => string 'YouTube' (length=7)
public 'height' => int 270
public 'html' => string '<iframe width="480" height="270" src="https://www.youtube.com/embed/_7s-6V_0nwA?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>' (length=171)
public 'thumbnail_height' => int 360
For details, you can also see How to get a YouTube video thumbnail using id or https://www.youtube.com/watch?v=mXde7q59BI8 video tutorial 1
Method 2:
Using YouTube image link, https://img.youtube.com/vi/"insert-youtube-video-id-here"/default.jpg
Method 3:
Using browser source code for getting thumbnail using video URL link -go to video source code and search for thumbnailurl. Now you can use this URL into your source code:
{img src="https://img.youtube.com/vi/"insert-youtube-video-id-here"/default.jpg"}
For details you can also see How to get a YouTube video thumbnail using id or https://www.youtube.com/watch?v=9f6E8MeM6PI video tutorial 2