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

ID : 266

viewed : 108

Tags : phpcurlyoutubeyoutube-apiyoutube-data-apiphp

Top 5 Answer for php - How do I get a YouTube video thumbnail from the YouTube API?

vote vote

97

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.

vote vote

87

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.

vote vote

72

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.

vote vote

60

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.

vote vote

54

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 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); 

Top 3 video Explaining php - How do I get a YouTube video thumbnail from the YouTube API?

Related QUESTION?