php 获取 YouTube 最高缩略图分辨率
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18029550/
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
Fetch YouTube highest thumbnail resolution
提问by Jim
I want to get youtube highest thumbnail "maxresdefault.jpg"
我想获得 youtube 最高缩略图“maxresdefault.jpg”
Like this one
像这个
http://i.ytimg.com/vi/Cj6ho1-G6tw/maxresdefault.jpg
http://i.ytimg.com/vi/Cj6ho1-G6tw/maxresdefault.jpg
I'm using this simple php code
我正在使用这个简单的 php 代码
<?php
$youtub_id = "Cj6ho1-G6tw";
echo "http://i.ytimg.com/vi/".$youtub_id."/maxresdefault.jpg";
?>
The problem with the code above is there is videos like this one http://youtu.be/VGazSZUYyf4NOT HD
上面代码的问题是有这样的视频http://youtu.be/VGAzSZUYyf4 NOT HD
And the result is gray small 404 image of youtube
结果是youtube的灰色小404图像
http://i.ytimg.com/vi/VGazSZUYyf4/maxresdefault.jpg
http://i.ytimg.com/vi/VGAzSZUYyf4/maxresdefault.jpg
So how to get the highest youtube thumbnail so if "maxresdefault" not available get the next big thumbnail "hqdefault", if not get the next "mqdefault" etc ...
那么如何获得最高的youtube缩略图,所以如果“maxresdefault”不可用,则获取下一个大缩略图“hqdefault”,如果没有获取下一个“mqdefault”等......
I tried to use gdata youtubebut either way the video hd or not "maxresdefault" not showing.
我尝试使用gdata youtube,但无论哪种方式都没有显示视频高清或不显示“maxresdefault”。
回答by Dave Chen
The reason is because the resolution on Making the Most of Mapsis not at least 720p.
原因是“充分利用地图”的分辨率至少不是 720p。
Looking at the api for this specific video, you can see that there is no maxresdefault
.
查看此特定视频的api,您可以看到没有maxresdefault
.
Only videos that are 720p or higher in resolution have maxresdefault
. This is not listed in the API in videos that are higher. So in order to get the highest resolution, you should also check if the maxresdefault
works as well.
只有分辨率为 720p 或更高的视频具有maxresdefault
. 这在更高的视频中的API中没有列出。因此,为了获得最高分辨率,您还应该检查是否maxresdefault
有效。
<media:thumbnail url='http://i1.ytimg.com/vi/VGazSZUYyf4/default.jpg' height='90' width='120' time='00:15:12.500' yt:name='default'/>
<media:thumbnail url='http://i1.ytimg.com/vi/VGazSZUYyf4/mqdefault.jpg' height='180' width='320' yt:name='mqdefault'/>
<media:thumbnail url='http://i1.ytimg.com/vi/VGazSZUYyf4/hqdefault.jpg' height='360' width='480' yt:name='hqdefault'/>
<media:thumbnail url='http://i1.ytimg.com/vi/VGazSZUYyf4/1.jpg' height='90' width='120' time='00:07:36.250' yt:name='start'/>
<media:thumbnail url='http://i1.ytimg.com/vi/VGazSZUYyf4/2.jpg' height='90' width='120' time='00:15:12.500' yt:name='middle'/>
<media:thumbnail url='http://i1.ytimg.com/vi/VGazSZUYyf4/3.jpg' height='90' width='120' time='00:22:48.750' yt:name='end'/>
Your best bet for the highest quality thumbnail is to use the API and get the image with the largest yt:name
attribute.
获得最高质量缩略图的最佳选择是使用 API 并获取具有最大yt:name
属性的图像。
The order is:
顺序是:
default
mqdefault
hqdefault
sddefault
Example code of this in action:
此操作的示例代码:
<?php
$youtub_id = "VGazSZUYyf4";
$images = json_decode(file_get_contents("http://gdata.youtube.com/feeds/api/videos/".$youtub_id."?v=2&alt=json"), true);
$images = $images['entry']['media$group']['media$thumbnail'];
$image = $images[count($images)-4]['url'];
$maxurl = "http://i.ytimg.com/vi/".$youtub_id."/maxresdefault.jpg";
$max = get_headers($maxurl);
if (substr($max[0], 9, 3) !== '404') {
$image = $maxurl;
}
echo '<img src="'.$image.'">';
This works both on $youtub_id = "Cj6ho1-G6tw";
and $youtub_id = "VGazSZUYyf4";
.
这适用于$youtub_id = "Cj6ho1-G6tw";
和$youtub_id = "VGazSZUYyf4";
。
回答by Amal Murali
You can use getimagesize()
and check if the image exists (there's file_exists()
too, but it may not work very well, in this case).
您可以使用getimagesize()
并检查图像是否存在(也有file_exists()
,但在这种情况下可能效果不佳)。
You can use this function to fetch the greatest resolution screenshot of a particular video.
您可以使用此功能获取特定视频的最大分辨率屏幕截图。
Code:
代码:
function fetch_highest_res ($videoid) {
$resolutions = array('maxresdefault', 'hqdefault', 'mqdefault');
foreach($resolutions as $res) {
$imgUrl = "http://i.ytimg.com/vi/$videoid/$res.jpg";
if(@getimagesize(($imgUrl)))
return $imgUrl;
}
}
Usage:
用法:
echo fetch_highest_res('Cj6ho1-G6tw').'<br>';
echo fetch_highest_res('VGazSZUYyf4');
Output:
输出:
http://i.ytimg.com/vi/Cj6ho1-G6tw/maxresdefault.jpg
http://i.ytimg.com/vi/VGazSZUYyf4/hqdefault.jpg
Note: This may not be the best solution, and it's a work-around if you don't want to use the API.
注意:这可能不是最好的解决方案,如果您不想使用 API,这是一种解决方法。
回答by Ibrahim Ulukaya
All the other answers that are dependent on static url scraping are not supported. They can get changed and you may need to change your application at everytime.
不支持依赖于静态 url 抓取的所有其他答案。它们可能会发生变化,您可能需要随时更改您的应用程序。
For this you should use Data API v3. You should go with a videos->listrequest with id=videoId and part=snippet. In the response you will check snippet.thumbnails.['high'].url
为此,您应该使用Data API v3。您应该使用带有 id=videoId 和 part=snippet的视频->列表请求。在响应中,您将检查snippet.thumbnails.['high'].url
回答by MikeSchinkel
Thanks to Ibrahim Ulukaya's answer I was able to understand why David Chen's previously correct answer no longer works; Google has deprecated that prior working API. (Wicked, bad, naughty, evil, Google.)
感谢 Ibrahim Ulukaya 的回答,我能够理解为什么 David Chen 之前的正确答案不再有效;谷歌已经弃用了之前的工作 API。(邪恶的,坏的,顽皮的,邪恶的,谷歌。)
The following URL will return a list of thumbnail objects using the Data API v3:
以下 URL 将使用Data API v3返回缩略图对象列表:
https://www.googleapis.com/youtube/v3/videos?id={VIDEO_ID}&key={API_KEY}&part=snippet&fields=items/snippet/thumbnails
The {VIDEO_ID}
can be found as part of any YouTube video URL and the {API_KEY}
requires you to:
该{VIDEO_ID}
可以发现在任何一个YouTube视频网址的一部分,{API_KEY}
要求您:
- First Create a Project
- Then Enable YouTube's Data API v3and
- Finally Create an API key.
Once you have the URL with your VIDEO_ID
and API_KEY
substituted for the placeholders you should get back JSON that looks something like this:
一旦您拥有带有您的 URLVIDEO_ID
并API_KEY
替换占位符,您应该返回如下所示的 JSON:
{
"items": [
{
"snippet": {
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/7Ey-Xkwn1s0/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/7Ey-Xkwn1s0/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/7Ey-Xkwn1s0/hqdefault.jpg",
"width": 480,
"height": 360
},
"standard": {
"url": "https://i.ytimg.com/vi/7Ey-Xkwn1s0/sddefault.jpg",
"width": 640,
"height": 480
},
"maxres": {
"url": "https://i.ytimg.com/vi/7Ey-Xkwn1s0/maxresdefault.jpg",
"width": 1280,
"height": 720
}
}
}
}
]
}
From there I am guessing you'll know what to do...?
从那里我猜你会知道该怎么做......?
Hope this helps.
希望这可以帮助。