php 如何从 YouTube API 获取 YouTube 视频缩略图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2068344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get a YouTube video thumbnail from the YouTube API?
提问by Ryan
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?
如果我有 YouTube 视频 URL,有什么方法可以使用 PHP 和 cURL 从 YouTube API 获取关联的缩略图?
回答by Asaph
Each YouTube video has four generated images. They are predictably formatted as follows:
每个 YouTube 视频都有四个生成的图像。它们的格式可预测如下:
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:
列表中的第一个是全尺寸图像,其他是缩略图。默认的缩略图图像(即,一个1.jpg,2.jpg,3.jpg)为:
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:
对于缩略图的高质量版本,请使用类似于以下内容的 URL:
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:
还有一个中等质量版本的缩略图,使用类似于 HQ 的 URL:
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:
对于缩略图的标准定义版本,请使用类似于以下内容的 URL:
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:
对于缩略图的最大分辨率版本,请使用类似于以下内容的 URL:
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.comworks in place of img.youtube.comin the example URLs above.
上述所有 URL 也可通过 HTTP 访问。此外,稍短的主机名i3.ytimg.com可以代替img.youtube.com上面的示例 URL。
Alternatively, you can use the YouTube Data API (v3)to get thumbnail images.
或者,您可以使用YouTube 数据 API (v3)来获取缩略图。
回答by Salman A
You can use YouTube Data APIto retrieve video thumbnails, caption, description, rating, statistics and more. API version 3 requires a key*. Obtain the key and create a videos: listrequest:
您可以使用YouTube 数据 API来检索视频缩略图、标题、描述、评级、统计信息等。API 版本 3 需要密钥*。获取密钥并创建视频:列表请求:
https://www.googleapis.com/youtube/v3/videos?key=YOUR_API_KEY&part=snippet&id=VIDEO_ID
Example PHP Code
示例 PHP 代码
$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.
* 不仅您需要密钥,还可能会根据您计划发出的 API 请求数量要求您提供帐单信息。但是,每天有几百万个请求是免费的。
来源文章。
回答by AGMG
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).
亚萨说的是对的。但是,并非每个 YouTube 视频都包含所有九个缩略图。此外,缩略图的图像大小取决于视频(以下数字基于 1)。
There are seven thumbnails guaranteed to exist:
有七个缩略图保证存在:
| Thumbnail Name | Size (px) | URL |
|---------------------|-----------|--------------------------------------------------|
| Player Background | 480x360 | https://i1.ytimg.com/vi/<VIDEO ID>/0.jpg |
| Start | 120x90 | https://i1.ytimg.com/vi/<VIDEO ID>/1.jpg |
| Middle | 120x90 | https://i1.ytimg.com/vi/<VIDEO ID>/2.jpg |
| End | 120x90 | https://i1.ytimg.com/vi/<VIDEO ID>/3.jpg |
| High Quality | 480x360 | https://i1.ytimg.com/vi/<VIDEO ID>/hqdefault.jpg |
| Medium Quality | 320x180 | https://i1.ytimg.com/vi/<VIDEO ID>/mqdefault.jpg |
| Normal Quality | 120x90 | https://i1.ytimg.com/vi/<VIDEO ID>/default.jpg |
Additionally, the two other thumbnails may or may not exist. Their presence is probably based on whether the video is high-quality.
此外,另外两个缩略图可能存在也可能不存在。他们的存在可能取决于视频是否高质量。
| Thumbnail Name | Size (px) | URL |
|---------------------|-----------|------------------------------------------------------|
| Standard Definition | 640x480 | https://i1.ytimg.com/vi/<VIDEO ID>/sddefault.jpg |
| Maximum Resolution | 1920x1080 | https://i1.ytimg.com/vi/<VIDEO ID>/maxresdefault.jpg |
You can find JavaScript and PHP scripts to retrieve thumbnails and other YouTube information in:
您可以在以下位置找到用于检索缩略图和其他 YouTube 信息的 JavaScript 和 PHP 脚本:
- 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 Generatortool to get all the information about a YouTube video by submitting a URL or video id.
您还可以使用YouTube 视频信息生成器工具通过提交 URL 或视频 ID 来获取有关 YouTube 视频的所有信息。
回答by Naren
In YouTube API V3 we can also use these URLs for obtaining thumbnails... They are classified based on their quality.
在 YouTube API V3 中,我们还可以使用这些 URL 来获取缩略图……它们根据质量进行分类。
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.
这些 URL 相对于第一个答案中的 URL 的一个优点是这些 URL 不会被防火墙阻止。
回答by PoorBoy
If you want the biggest image from YouTube for a specific video ID, then the URL should be something like this:
如果您想要来自 YouTube 的特定视频 ID 的最大图像,则 URL 应如下所示:
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:
使用 API,您可以选择默认缩略图图像。简单的代码应该是这样的:
//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);
回答by LuTz
If you want to get rid of the "black bars" and do it like YouTube does it, you can use:
如果您想摆脱“黑条”并像 YouTube 那样做,您可以使用:
https://i.ytimg.com/vi_webp/<video id>/mqdefault.webp
And if you can't use the .webpfile extension you can do it like this:
如果你不能使用.webp文件扩展名,你可以这样做:
https://i.ytimg.com/vi/<video id>/mqdefault.jpg
Also, if you need the unscaled version, use maxresdefaultinstead of mqdefault.
另外,如果你需要的未缩放版本,使用maxresdefault代替mqdefault。
Note: I'm not sure about the aspect ratio if you're planning to use maxresdefault.
注意:如果您打算使用maxresdefault.
回答by Henrik Albrechtsson
I made a function to only fetch existing images from YouTube
我做了一个功能,只从 YouTube 获取现有图像
function youtube_image($id) {
$resolution = array (
'maxresdefault',
'sddefault',
'mqdefault',
'hqdefault',
'default'
);
for ($x = 0; $x < sizeof($resolution); $x++) {
$url = '//img.youtube.com/vi/' . $id . '/' . $resolution[$x] . '.jpg';
if (get_headers($url)[0] == 'HTTP/1.0 200 OK') {
break;
}
}
return $url;
}
回答by Ibrahim Ulukaya
In YouTube Data API v3, you can get video's thumbnails with the videos->listfunction. From snippet.thumbnails.(key), you can pick the default, medium or high resolution thumbnail, and get its width, height and URL.
在YouTube Data API v3 中,您可以使用视频- >列表功能获取视频的缩略图。从snippet.thumbnails.(key) 中,您可以选择默认、中等或高分辨率的缩略图,并获取其宽度、高度和URL。
You can also update thumbnails with the thumbnails->setfunctionality.
您还可以使用缩略图-> 设置功能更新缩略图。
For examples, you can check out the YouTube API Samplesproject. (PHP ones.)
例如,您可以查看YouTube API 示例项目。(PHP 的。)
回答by keyboardP
You can get the Video Entrywhich contains the URL to the video's thumbnail. There's example code in the link. Or, if you want to parse XML, there's information here. The XML returned has a media:thumbnailelement, which contains the thumbnail's URL.
您可以获得包含视频缩略图 URL的视频条目。链接中有示例代码。或者,如果你想解析XML,还有的信息在这里。返回的 XML 有一个media:thumbnail元素,其中包含缩略图的 URL。
回答by Mahendra Tyagi
// Get image form video URL
$url = $video['video_url'];
$urls = parse_url($url);
//Expect the URL to be http://youtu.be/abcd, where abcd is the video ID
if ($urls['host'] == 'youtu.be') :
$imgPath = ltrim($urls['path'],'/');
//Expect the URL to be http://www.youtube.com/embed/abcd
elseif (strpos($urls['path'],'embed') == 1) :
$imgPath = end(explode('/',$urls['path']));
//Expect the URL to be abcd only
elseif (strpos($url,'/') === false):
$imgPath = $url;
//Expect the URL to be http://www.youtube.com/watch?v=abcd
else :
parse_str($urls['query']);
$imgPath = $v;
endif;

