How do I get a YouTube video thumbnail from the YouTube API?
如果我有YouTube视频网址,有什么方法可以使用PHP和cURL从YouTube API获取关联的缩略图吗?
Each YouTube video has 4 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 (ie. 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.
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 million requests per day are free.
Source article.
What Asaph said is right. However, not every YouTube video contains all nine thumbnails, but there are seven thumbnails for every video. They are:
(Image size depends on video.)
Player Background Thumbnail (480x360 pixels)
https://i1.ytimg.com/vi/G0wGs3useV8/0.jpg
Start Thumbnail (120x90 pixels)
https://i1.ytimg.com/vi/G0wGs3useV8/1.jpg
Middle Thumbnail (120x90 pixels)
https://i1.ytimg.com/vi/G0wGs3useV8/2.jpg
End Thumbnail (120x90 pixels)
https://i1.ytimg.com/vi/G0wGs3useV8/3.jpg
High Quality Thumbnail (480x360 pixels)
https://i1.ytimg.com/vi/G0wGs3useV8/hqdefault.jpg
Medium Quality Thumbnail (320x180 pixels)
https://i1.ytimg.com/vi/G0wGs3useV8/mqdefault.jpg
Normal Quality Thumbnail (120x90 pixels)
https://i1.ytimg.com/vi/G0wGs3useV8/default.jpg
And additionally, the next two thumbnails may or may not exist. For HQ videos they exist.
Standard Definition Thumbnail (640x480 pixels)
https://i1.ytimg.com/vi/G0wGs3useV8/sddefault.jpg
Maximum Resolution Thumbnail (1920x1080 pixels)
https://i1.ytimg.com/vi/G0wGs3useV8/maxresdefault.jpg
You can get the JavaScript and PHP scripts to retrieve thumbnails and other YouTube information in
Also use the tool YouTube Video Information Generator to get all the information of a YouTube video by submitting a URL or video ID.
链接地址: http://www.djcxy.com/p/758.html上一篇: 确定已安装的PowerShell版本