How do I get a YouTube video thumbnail from the YouTube API?
Solution 1
Each YouTube video has four generated images. They are predictably formatted as follows:
https://img.youtube.com/vi/<insert-youtube-video-id-here>/0.jpg
https://img.youtube.com/vi/<insert-youtube-video-id-here>/1.jpg
https://img.youtube.com/vi/<insert-youtube-video-id-here>/2.jpg
https://img.youtube.com/vi/<insert-youtube-video-id-here>/3.jpg
The first one in the list is a full size image and others are thumbnail images. The default thumbnail image (i.e., one of 1.jpg
, 2.jpg
, 3.jpg
) is:
https://img.youtube.com/vi/<insert-youtube-video-id-here>/default.jpg
For the high quality version of the thumbnail use a URL similar to this:
https://img.youtube.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 HQ:
https://img.youtube.com/vi/<insert-youtube-video-id-here>/mqdefault.jpg
For the standard definition version of the thumbnail, use a URL similar to this:
https://img.youtube.com/vi/<insert-youtube-video-id-here>/sddefault.jpg
For the maximum resolution version of the thumbnail use a URL similar to this:
https://img.youtube.com/vi/<insert-youtube-video-id-here>/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.
Solution 2
You can use YouTube Data API to retrieve video thumbnails, caption, description, rating, statistics and more. API version 3 requires a key*. Obtain the key and create a videos: list request:
https://www.googleapis.com/youtube/v3/videos?key=YOUR_API_KEY&part=snippet&id=VIDEO_ID
Example PHP Code
$data = file_get_contents("https://www.googleapis.com/youtube/v3/videos?key=YOUR_API_KEY&part=snippet&id=T0Jqdjbed40");
$json = json_decode($data);
var_dump($json->items[0]->snippet->thumbnails);
Output
object(stdClass)#5 (5) {
["default"]=>
object(stdClass)#6 (3) {
["url"]=>
string(46) "https://i.ytimg.com/vi/T0Jqdjbed40/default.jpg"
["width"]=>
int(120)
["height"]=>
int(90)
}
["medium"]=>
object(stdClass)#7 (3) {
["url"]=>
string(48) "https://i.ytimg.com/vi/T0Jqdjbed40/mqdefault.jpg"
["width"]=>
int(320)
["height"]=>
int(180)
}
["high"]=>
object(stdClass)#8 (3) {
["url"]=>
string(48) "https://i.ytimg.com/vi/T0Jqdjbed40/hqdefault.jpg"
["width"]=>
int(480)
["height"]=>
int(360)
}
["standard"]=>
object(stdClass)#9 (3) {
["url"]=>
string(48) "https://i.ytimg.com/vi/T0Jqdjbed40/sddefault.jpg"
["width"]=>
int(640)
["height"]=>
int(480)
}
["maxres"]=>
object(stdClass)#10 (3) {
["url"]=>
string(52) "https://i.ytimg.com/vi/T0Jqdjbed40/maxresdefault.jpg"
["width"]=>
int(1280)
["height"]=>
int(720)
}
}
* Not only that you need a key, you might be asked for billing information depending on the number of API requests you plan to make. However, few thousand requests per day are free.
Solution 3
What Asaph said is right. However, not every YouTube video contains all nine thumbnails. Also, the thumbnails' image sizes depends on the video (the numbers below are based on one). There are some thumbnails guaranteed to exist:
Width | Height | URL
------|--------|----
120 | 90 | https://i.ytimg.com/vi/<VIDEO ID>/1.jpg
120 | 90 | https://i.ytimg.com/vi/<VIDEO ID>/2.jpg
120 | 90 | https://i.ytimg.com/vi/<VIDEO ID>/3.jpg
120 | 90 | https://i.ytimg.com/vi/<VIDEO ID>/default.jpg
320 | 180 | https://i.ytimg.com/vi/<VIDEO ID>/mq1.jpg
320 | 180 | https://i.ytimg.com/vi/<VIDEO ID>/mq2.jpg
320 | 180 | https://i.ytimg.com/vi/<VIDEO ID>/mq3.jpg
320 | 180 | https://i.ytimg.com/vi/<VIDEO ID>/mqdefault.jpg
480 | 360 | https://i.ytimg.com/vi/<VIDEO ID>/0.jpg
480 | 360 | https://i.ytimg.com/vi/<VIDEO ID>/hq1.jpg
480 | 360 | https://i.ytimg.com/vi/<VIDEO ID>/hq2.jpg
480 | 360 | https://i.ytimg.com/vi/<VIDEO ID>/hq3.jpg
480 | 360 | https://i.ytimg.com/vi/<VIDEO ID>/hqdefault.jpg
Additionally, the some other thumbnails may or may not exist. Their presence is probably based on whether the video is high-quality.
Width | Height | URL
------|--------|----
640 | 480 | https://i.ytimg.com/vi/<VIDEO ID>/sd1.jpg
640 | 480 | https://i.ytimg.com/vi/<VIDEO ID>/sd2.jpg
640 | 480 | https://i.ytimg.com/vi/<VIDEO ID>/sd3.jpg
640 | 480 | https://i.ytimg.com/vi/<VIDEO ID>/sddefault.jpg
1280 | 720 | https://i.ytimg.com/vi/<VIDEO ID>/hq720.jpg
1920 | 1080 | https://i.ytimg.com/vi/<VIDEO ID>/maxresdefault.jpg
You can find JavaScript and PHP scripts to retrieve thumbnails and other YouTube information in:
- How to get YouTube Video Info with PHP
- Retrieve YouTube Video Details using JavaScript - JSON & API v2
You can also use the YouTube Video Information Generator tool to get all the information about a YouTube video by submitting a URL or video id.
Solution 4
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.
Solution 5
If you want the biggest image from YouTube for a specific video ID, then the URL should be something like this:
http://i3.ytimg.com/vi/SomeVideoIDHere/0.jpg
Using the API, you can pick up the default thumbnail image. Simple code should be something like this:
//Grab the default thumbnail image
$attrs = $media->group->thumbnail[1]->attributes();
$thumbnail = $attrs['url'];
$thumbnail = substr($thumbnail, 0, -5);
$thumb1 = $thumbnail."default.jpg";
// Grab the third thumbnail image
$thumb2 = $thumbnail."2.jpg";
// Grab the fourth thumbnail image.
$thumb3 = $thumbnail."3.jpg";
// Using simple cURL to save it your server.
// You can extend the cURL below if you want it as fancy, just like
// the rest of the folks here.
$ch = curl_init ("$thumb1");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata = curl_exec($ch);
curl_close($ch);
// Using fwrite to save the above
$fp = fopen("SomeLocationInReferenceToYourScript/AnyNameYouWant.jpg", 'w');
// Write the file
fwrite($fp, $rawdata);
// And then close it.
fclose($fp);
CodeOverload
Updated on July 11, 2022Comments
-
CodeOverload almost 2 years
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?
-
squarecandy about 6 yearsHere are some PHP functions I created to get the best thumbnail available from the URL method w/o pinging the API, based off of the many good answers on this page: gist.github.com/squarecandy/30ec9f6779c0e8cdf12e337c665d96c5
-
-
mauris about 12 yearsFYI: i did not change the code to fit the new JSON structure. The code in your getJSON is wrong. You used
jsonc
instead ofjson
for getJSON. It's failing because of your wrong JSON structure. -
mauris about 12 yearsOnly the first example can use
jsonc
. The jQuery and PHP examples MUST usejson
. And I have updated the code to conform to the new JSON structure. I am using the code right now and it's working LIVE. so don't say it's not working without reading the changes. Thanks! -
Salman A about 12 yearsThe jQuery and PHP examples MUST use json... can you provide a reference? Also, requesting a specific API version number (
v=2
) takes care of API changes. -
Aaron about 11 yearsThe gdata call does not return whether maxresdefault.jpg is available for videos where it is available, only mq/hq/sd.
-
Lion789 over 10 yearsAre these images suppose to be placed in an img tag and should just work because I am trying to include the link with the right youtube video id and put them within the img tag but I keep getting a 404
-
Naren over 10 years@Lion789 These URLs works fine when it is placed in an img tag..The problem might be a wrong video Id or the requested resolution may not be available but you won't get the 404 with these errors.Please check the url of the img tag.
-
Ciaran Phillips over 10 yearsJust in case anyone else makes this dumb mistake - you can't use
http://www.img.youtube.com
, onlyhttp://img.youtube.com
-
TRiG over 10 years@KeyLimePiePhotonAndroid, I think queries about playlists would work better as a separate question.
-
mpgn almost 10 yearsthis is a bad solution ! why not use Youtube api v3 ?
-
Modder almost 10 yearsAdditionally, you can use these hostnames
i.ytimg.com
,i1.ytimg.com
,i2.ytimg.com
,i3.ytimg.com
,i4.ytimg.com
ors.ytimg.com
. -
Ragaisis over 9 yearsYou can also achieve same result with official youtube link img.youtube.com/vi/mJ8tq8AnNis/mqdefault.jpg where mJ8tq8AnNis is video id
-
Adonis K. Kakoulidis over 9 yearsI guess this is the right way to do it since it's coming from a Google Dev relations member for the youtube api
-
CashCow over 9 yearsThis is useful as I am trying to eliminate "still picture" videos from my search results. If thumbnails 2 and 3 both exist and are identical it's a very strong likelihood I have a still photo video and I can eliminate it
-
CashCow over 9 yearsAPI v3 is VERY badly documented. They give you sample code but the "reference" section is very short of detail. Some things that should be relatively "obvious" seem to require extreme workarounds.
-
CashCow over 9 yearsI just tried it and even though they look identical there were small differences. 1 and 3 were closer matching as they were identical size but differed in a few places.
-
clami219 about 9 years
sddefault.jpg
andmaxresdefault.jpg
formats are no longer working -
jave.web about 9 years@munissor that is NOT TRUE, 0.jpg IS NOT ALWAYS fullsize version... It might be, but it is not a rule...
-
Daniele B about 9 yearsIs it confirmed that the urls will keep working after API v2 will be stopped?
-
Naren about 9 years@DanieleB These URLs still work. Do you know how to check whether these URLs will work after API v2?
-
Dean Whitehouse about 9 yearsOn maxresdefault.jpg, if using a custom thumbnail in YouTube, we found the image must be a 16:9 aspect ratio otherwise it will default to the YouTube image.
-
KNU over 8 yearsto fetch the ID this question might be helpful: stackoverflow.com/questions/3452546/…
-
Nico Haase over 8 yearsI found an example where 0.jpg has a resolution of 480x360, while maxresdefault.jpg has 1280x720
-
NickG almost 8 yearsThis is not available for many videos. Eg: i.ytimg.com/vi_webp/mJ8tq8AnNis/mqdefault.webp
-
LuTz almost 8 years@NickG : It works if you remove
_webp
and change the extension to.jpg
. Working example : i.ytimg.com/vi/mJ8tq8AnNis/mqdefault.jpg , I'm not sure about the aspect ratio. -
Ne AS over 7 years@jave.web @clami219 Since the
sddefault
andmaxresdefault
are not always available . Does themqdefault
works for all videos? What is the best URL to get thumbnail without the 2 black boders (at the top and the bottom)? -
jave.web over 7 years@Llg 2 black boders is something you can't really predict, it depends in what ratio is thumbnail vs in what ratio is video. I am not sure about which are always available, if you are too concerned about that, you should use some YouTube API. Sidenote: lately I haven't noticed video without
maxresdefault
but that is just subjective... -
fluffyBatman over 7 yearsWhile this works for most of the videos I tried last couple hours but strangely it didn't work out for this: img.youtube.com/vi/vJ5ZCBbAPk/0.jpg . But Youtube seems to generate the video thumnail in website just fine. Any thoughts guys?
-
Tempus over 7 yearsI have noticed that depending on the age of the video and the uploaded quality, some images will not be a standard expected size. Mainly the "maxresdefault.jpg ". So for testing image size, I made this page myonlinelocation.com/YouTube_Play/VideoThumbnails
-
giovannipds over 7 yearsThe tool "YouTube Video Information Generator" is currently not working. Consider editing the answer.
-
Saggy about 7 yearsIs there any api hit restriction for this web server? Or it's completely free to hit N numbers of hit.?
-
Alexander about 7 years@Saggy If there is a limit, it's high. My website uses YouTube thumbnails heavily and hasn't had any problems. This is also not an API call; it's a link to an image file.
-
Arun Ravindranath over 6 yearsIt's not working when there is a ' - ' symbol in the video id (-oCXMrICJgY in m.youtube.com/watch?v=-oCXMrICJgY ) how to solve this issue ??
-
Admin over 6 yearsI have to manually select
<insert-youtube-video-id-here>
every time. Can this be updated for easier use? If it washttps://img.youtube.com/vi/YOUTUBEVIDEOID/default.jpg
it would just need a double click. -
Ma Kobi about 6 yearsThis only works when the url has no other query parameters.
-
Sodruldeen Mustapha about 6 yearsyes, all youtube videos follows the format while watching, you can copy the link from the browser
-
Ma Kobi about 6 yearsWhat if you have another parameter like
&t=227s
? -
Sodruldeen Mustapha about 6 yearsi understand your point, but the standard youtube videos come without other parameters but when the need arise, you can filter the link before the first line of the function. Do you need help in filtering the link to make sure it ignores every other thing except the needed?
-
Hernan over 5 yearsIf someone is wondering how to get sizes for not default: I just tried (and it actually works) that format names work as prefix. So you can get
maxres2.jpg
,hq2.jpg
,hq3.jpg
, etc. -
Peter Mortensen almost 5 yearsLinks
https://www.youtube.com/watch?v=mXde7q59BI8
andhttps://www.youtube.com/watch?v=9f6E8MeM6PI
are (effectively) broken. -
Hassan Saeed almost 5 yearsyoutube banned this video because of its policies i hope this article will help you www.hzonesp.com/php/get-youtube-video-thumbnail-using-id/
-
Bob Stein over 4 yearsSuggest removing all three links. None of them produce anything useful that I could see.
-
Rajesh about 4 yearsMethod 1 is simple to implement
-
Youssef Boudaya over 3 yearsi get this in laravel controller get_headers(): This function may only be used against URLs
-
Akshay K Nair about 3 yearsIs there a way to get the YouTube Data API key of the user (after auth)
-
Zippp about 3 yearsFYI - it seems like it does not fetch existing images but the first available.
-
mordad almost 3 yearsis this somewhere officially documented?!!
-
Akshay K Nair almost 3 yearsThank you. Is there a way to get
sddefault
without black bars? img.youtube.com/vi_webp/dQw4w9WgXcQ/sddefault.webp keeps returning image with black bars. -
Nathan B over 2 yearssome videos do not have the thumnail. why?
-
Mattwmaster58 over 2 yearsFWIW, the YouTube frontend hardcodes
mqdefault.jpg
to be 320x180 -
imrok over 2 yearsWorks well but, could you provide sources ?