How do I get a YouTube video thumbnail from the YouTube API?

1,874,946

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.

Source article.

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:

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);
Share:
1,874,946
CodeOverload
Author by

CodeOverload

Updated on July 11, 2022

Comments

  • CodeOverload
    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?

  • mauris
    mauris about 12 years
    FYI: i did not change the code to fit the new JSON structure. The code in your getJSON is wrong. You used jsonc instead of json for getJSON. It's failing because of your wrong JSON structure.
  • mauris
    mauris about 12 years
    Only the first example can use jsonc. The jQuery and PHP examples MUST use json. 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
    Salman A about 12 years
    The 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
    Aaron about 11 years
    The gdata call does not return whether maxresdefault.jpg is available for videos where it is available, only mq/hq/sd.
  • Lion789
    Lion789 over 10 years
    Are 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
    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
    Ciaran Phillips over 10 years
    Just in case anyone else makes this dumb mistake - you can't use http://www.img.youtube.com, only http://img.youtube.com
  • TRiG
    TRiG over 10 years
    @KeyLimePiePhotonAndroid, I think queries about playlists would work better as a separate question.
  • mpgn
    mpgn almost 10 years
    this is a bad solution ! why not use Youtube api v3 ?
  • Modder
    Modder almost 10 years
    Additionally, you can use these hostnames i.ytimg.com, i1.ytimg.com, i2.ytimg.com, i3.ytimg.com, i4.ytimg.com or s.ytimg.com.
  • Ragaisis
    Ragaisis over 9 years
    You can also achieve same result with official youtube link img.youtube.com/vi/mJ8tq8AnNis/mqdefault.jpg where mJ8tq8AnNis is video id
  • Adonis K. Kakoulidis
    Adonis K. Kakoulidis over 9 years
    I guess this is the right way to do it since it's coming from a Google Dev relations member for the youtube api
  • CashCow
    CashCow over 9 years
    This 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
    CashCow over 9 years
    API 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
    CashCow over 9 years
    I 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
    clami219 about 9 years
    sddefault.jpg and maxresdefault.jpg formats are no longer working
  • jave.web
    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
    Daniele B about 9 years
    Is it confirmed that the urls will keep working after API v2 will be stopped?
  • Naren
    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
    Dean Whitehouse about 9 years
    On 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
    KNU over 8 years
    to fetch the ID this question might be helpful: stackoverflow.com/questions/3452546/…
  • Nico Haase
    Nico Haase over 8 years
    I found an example where 0.jpg has a resolution of 480x360, while maxresdefault.jpg has 1280x720
  • NickG
    NickG almost 8 years
    This is not available for many videos. Eg: i.ytimg.com/vi_webp/mJ8tq8AnNis/mqdefault.webp
  • LuTz
    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
    Ne AS over 7 years
    @jave.web @clami219 Since the sddefault and maxresdefault are not always available . Does the mqdefault 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
    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
    fluffyBatman over 7 years
    While 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
    Tempus over 7 years
    I 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
    giovannipds over 7 years
    The tool "YouTube Video Information Generator" is currently not working. Consider editing the answer.
  • Saggy
    Saggy about 7 years
    Is there any api hit restriction for this web server? Or it's completely free to hit N numbers of hit.?
  • Alexander
    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
    Arun Ravindranath over 6 years
    It'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
    Admin over 6 years
    I have to manually select <insert-youtube-video-id-here> every time. Can this be updated for easier use? If it was https://img.youtube.com/vi/YOUTUBEVIDEOID/default.jpg it would just need a double click.
  • Ma Kobi
    Ma Kobi about 6 years
    This only works when the url has no other query parameters.
  • Sodruldeen Mustapha
    Sodruldeen Mustapha about 6 years
    yes, all youtube videos follows the format while watching, you can copy the link from the browser
  • Ma Kobi
    Ma Kobi about 6 years
    What if you have another parameter like &t=227s?
  • Sodruldeen Mustapha
    Sodruldeen Mustapha about 6 years
    i 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
    Hernan over 5 years
    If 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
    Peter Mortensen almost 5 years
    Links https://www.youtube.com/watch?v=mXde7q59BI8 and https://www.youtube.com/watch?v=9f6E8MeM6PI are (effectively) broken.
  • Hassan Saeed
    Hassan Saeed almost 5 years
    youtube 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
    Bob Stein over 4 years
    Suggest removing all three links. None of them produce anything useful that I could see.
  • Rajesh
    Rajesh about 4 years
    Method 1 is simple to implement
  • Youssef Boudaya
    Youssef Boudaya over 3 years
    i get this in laravel controller get_headers(): This function may only be used against URLs
  • Akshay K Nair
    Akshay K Nair about 3 years
    Is there a way to get the YouTube Data API key of the user (after auth)
  • Zippp
    Zippp about 3 years
    FYI - it seems like it does not fetch existing images but the first available.
  • mordad
    mordad almost 3 years
    is this somewhere officially documented?!!
  • Akshay K Nair
    Akshay K Nair almost 3 years
    Thank 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
    Nathan B over 2 years
    some videos do not have the thumnail. why?
  • Mattwmaster58
    Mattwmaster58 over 2 years
    FWIW, the YouTube frontend hardcodes mqdefault.jpg to be 320x180
  • imrok
    imrok over 2 years
    Works well but, could you provide sources ?