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
列表中的第一个是完整尺寸的图像,其余是缩略图像。默认的缩略图像(即其中之一:1.jpg
、2.jpg
、3.jpg
)为:
https://img.youtube.com/vi/<insert-youtube-video-id-here>/default.jpg
若要获取高质量的缩略图,请使用类似于此的URL:
https://img.youtube.com/vi/<insert-youtube-video-id-here>/hqdefault.jpg
还有一种中等质量的缩略图版本,使用类似于高清版的URL:
https://img.youtube.com/vi/<insert-youtube-video-id-here>/mqdefault.jpg
如果要使用标准定义版本的缩略图,请使用类似于此的URL:
https://img.youtube.com/vi/<insert-youtube-video-id-here>/sddefault.jpg
要获得缩略图的最大分辨率版本,请使用类似于此的URL:
https://img.youtube.com/vi/<insert-youtube-video-id-here>/maxresdefault.jpg
所有上述URL都可以通过HTTP访问。此外,在示例URL中,稍微缩短的主机名i3.ytimg.com也可以替代img.youtube.com
。
或者,您可以使用YouTube数据API(v3)获取缩略图图像。
http://www.img.youtube.com
,只能使用 http://img.youtube.com
。 - Ciaran Phillipssddefault.jpg
和maxresdefault.jpg
格式不再可用。 - clami219maxres2.jpg
,hq2.jpg
,hq3.jpg
等尺寸。 - Hernanhttps://www.googleapis.com/youtube/v3/videos?key=YOUR_API_KEY&part=snippet&id=VIDEO_ID
示例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);
输出
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)
}
}
* 不仅需要密钥,根据您计划进行的API请求数量,您可能会被要求提供账单信息。但是,每天少量的请求是免费的。
来源文章。
jsonc
而不是json
来获取JSON数据。由于你的JSON结构错误,它会失败。 翻译为:提醒您:我并没有更改代码以适应新的JSON结构。您的getJSON中的代码有误。您使用了 jsonc
而不是 json
来进行getJSON操作。这是因为您的JSON结构错误导致操作失败。 - maurisjsonc
。jQuery 和 PHP 示例 必须 使用 json
。我已经更新了代码以符合新的 JSON 结构。我现在正在使用该代码,它正在实时工作。所以请先阅读更改后再说它不起作用。谢谢! - maurisv=2
)可以处理API更改。需要使用JSON格式来演示jQuery和PHP的例子。 - Salman AWidth | Height | URL |
---|---|---|
120 | 90 | https://i.ytimg.com/vi//1.jpg |
120 | 90 | https://i.ytimg.com/vi//2.jpg |
120 | 90 | https://i.ytimg.com/vi//3.jpg |
120 | 90 | https://i.ytimg.com/vi//default.jpg |
320 | 180 | https://i.ytimg.com/vi//mq1.jpg |
320 | 180 | https://i.ytimg.com/vi//mq2.jpg |
320 | 180 | https://i.ytimg.com/vi//mq3.jpg |
320 | 180 | https://i.ytimg.com/vi//mqdefault.jpg |
480 | 360 | https://i.ytimg.com/vi//0.jpg |
480 | 360 | https://i.ytimg.com/vi//hq1.jpg |
480 | 360 | https://i.ytimg.com/vi//hq2.jpg |
480 | 360 | https://i.ytimg.com/vi//hq3.jpg |
480 | 360 | https://i.ytimg.com/vi//hqdefault.jpg |
Width | Height | URL |
---|---|---|
640 | 480 | https://i.ytimg.com/vi//sd1.jpg |
640 | 480 | https://i.ytimg.com/vi//sd2.jpg |
640 | 480 | https://i.ytimg.com/vi//sd3.jpg |
640 | 480 | https://i.ytimg.com/vi//sddefault.jpg |
1280 | 720 | https://i.ytimg.com/vi//hq720.jpg |
1920 | 1080 | https://i.ytimg.com/vi//maxresdefault.jpg |
你可以在以下地方找到用于获取缩略图和其他YouTube信息的JavaScript和PHP脚本:
你还可以使用YouTube视频信息生成器工具,通过提交URL或视频ID获取有关YouTube视频的所有信息。
mqdefault.jpg
为320x180。 - Mattwmaster58在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
对于最大分辨率...
https://i1.ytimg.com/vi/<insert-youtube-video-id-here>/maxresdefault.jpg
相对于第一个答案中的URL,这些URL的一个优点是它们不会被防火墙屏蔽。
如果您想摆脱“黑边”,并像YouTube一样处理,您可以使用:
https://i.ytimg.com/vi_webp/<video id>/mqdefault.webp
如果您无法使用.webp
文件扩展名,可以这样做:
https://i.ytimg.com/vi/<video id>/mqdefault.jpg
另外,如果您需要未缩放的版本,请使用maxresdefault
而不是mqdefault
。
注意:如果您打算使用maxresdefault
,我不确定它的纵横比。
sddefault
?https://img.youtube.com/vi_webp/dQw4w9WgXcQ/sddefault.webp 一直返回带有黑边的图像。 - Akshay K Nairhttp://i3.ytimg.com/vi/SomeVideoIDHere/0.jpg
使用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);
我写了一个函数,只从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;
}
YouTube属于Google,Google希望有适合不同屏幕尺寸的合理数量的图像,因此它的图像以不同的尺寸存储。以下是您的缩略图的示例:
低质量缩略图:
http://img.youtube.com/vi/<YouTube_Video_ID_HERE>/sddefault.jpg
中等质量缩略图:
http://img.youtube.com/vi/<YouTube_Video_ID_HERE>/mqdefault.jpg
高质量缩略图:
http://img.youtube.com/vi/<YouTube_Video_ID_HERE>/hqdefault.jpg
最高质量的缩略图:
http://img.youtube.com/vi/<YouTube_Video_ID_HERE>/maxresdefault.jpg
sddefault.jpg
实际上比hqdefault.jpg
质量更高。质量递增的顺序是default, mqdefault, hqdefault, sddefault, maxresdefault
。 - undefined