如果我有一个YouTube视频的URL,有没有办法用PHP和cURL从YouTube的API获得相关的缩略图?
每个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
列表中的第一张是全尺寸图片,其他是缩略图。默认的缩略图(即 "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
还有一个中等质量版本的缩略图,使用类似于HQ的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上也可以使用。此外,稍短的主机名i3.ytimg.com
可以代替上述URL示例中的img.youtube.com
。
另外,你可以使用YouTube数据API(v3)来获取缩略图。
你可以使用YouTube数据API来检索视频缩略图、标题、描述、评级、统计数据等。API版本3需要一个密钥*。获取密钥并创建一个视频:列表请求。
https://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请求的数量。然而,每天几百万次请求是免费的。
文章来源.
Asaph说的是对的。 然而,并不是每个YouTube视频都包含所有九个缩略图。 另外,缩略图'。 图像大小取决于视频(下面的数字是基于一个)。
有七个缩略图保证存在。
| 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 |
此外,另外两个缩略图可能存在,也可能不存在。 它们的存在可能是基于视频是否高质量。
| 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 |
你可以找到JavaScript和PHP脚本来检索缩略图和其他。 YouTube信息中。
您也可以使用YouTube视频信息生成器工具,通过提交URL或视频ID来获取YouTube视频的所有信息。
h:
[r]: http://techtweaker.com/retrieve-youtube-video-details-using-javascript
在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的一个优点是,这些URL不会被防火墙屏蔽。
如果你想从YouTube上获取特定视频ID的最大图片,那么URL应该是这样的:
http://i3.ytimg.com/vi/SomeVideoIDHere/0.jpg
。
http://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);
。
//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那样做,你可以使用。
https://i.ytimg.com/vi_webp/<video id>/mqdefault.webp
如果你不能使用.webp
文件扩展名,你可以这样做。
https://i.ytimg.com/vi/<video id>/mqdefault.jpg
另外,如果你需要不缩放的版本,请使用maxresdefault
代替mqdefault
。
注意事项。
如果你打算使用maxresdefault
,我不确定长宽比。
我做了一个只从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 Data API v3中,你可以通过[videos->list][2]函数获取视频的缩略图。 从[snippet.thumbnails.(key)][3]中,你可以选择默认的、中等或高分辨率的缩略图,并获得它的宽度、高度和URL。
您还可以使用[thumbnails->set][4]功能更新缩略图。
关于例子,你可以查看[YouTube API Samples][5]项目。 ([PHP的][6]。)
1:
[2]: https://developers.google.com/youtube/v3/docs/videos/list [3]: https://developers.google.com/youtube/v3/docs/videos#snippet.thumbnails.%28key%29 [4]: https://developers.google.com/youtube/v3/docs/thumbnails/set [5]: https://code.google.com/p/youtube-api-samples/source/browse/#git/samples [6]: https://code.google.com/p/youtube-api-samples/source/browse/#git/samples/php
// 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;
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
如果你想做的是搜索YouTube,并获得相关属性。
获取一个公共API--[这个链接给出了一个很好的方向][1] 。
使用下面的查询字符串。 URL字符串中的搜索查询(用q=表示)是stackoverflow为例。 然后,YouTube会给你发回一个JSON回复,你可以在其中解析出缩略图、片段、作者等信息。
[1]: http://www.robshouse.net/content/youtube-data-api-v3-how-get-developerkey
使用。
https://www.googleapis.com/youtube/v3/videoCategories?part=snippet,id&maxResults=100®ionCode=us&key=**Your YouTube ID**
以上是链接。 使用它,你可以找到视频的YouTube特征。 找到特征后,你可以得到选定类别的视频。 之后就可以使用[Asaph's answer][1]找到选定的视频图片。
试试上面的方法,你可以从YouTube的[API][2]中解析一切。
[1]: https://stackoverflow.com/questions/2068344/how-do-i-get-a-youtube-video-thumbnail-from-the-youtube-api/2068371#2068371 [2]: http://en.wikipedia.org/wiki/Application_programming_interface
只是为了补充/扩展给定的解决方案,我觉得有必要指出,因为我自己也有这个问题,实际上可以通过一个HTTP请求抓取多个YouTube视频内容,在这种情况下,缩略图。
使用一个Rest客户端,在这种情况下,HTTPFUL,你可以做这样的事情。
<?php
header("Content-type", "application/json");
//download the httpfull.phar file from http://phphttpclient.com
include("httpful.phar");
$youtubeVidIds= array("nL-rk4bgJWU", "__kupr7KQos", "UCSynl4WbLQ", "joPjqEGJGqU", "PBwEBjX3D3Q");
$response = \Httpful\Request::get("https://www.googleapis.com/youtube/v3/videos?key=YourAPIKey4&part=snippet&id=".implode (",",$youtubeVidIds)."")
->send();
print ($response);
?>
我发现了这个奇特的工具,它可以让你创建的图像与YouTube播放按钮放置在图像。
*安装在服务器上的脚本。 https://github.com/halgatewood/youtube-thumbnail-enhancer
另一个不错的选择是使用YouTube支持的oEmbedAPI。
你只需将你的YouTube URL添加到oEmbed URL中,你就会收到一个包含缩略图和HTML代码的JSON,用于嵌入。
例子:
http://www.youtube.com/oembed?format=json&url=http%3A//youtube.com/watch%3Fv%3DDLzxrzFCyOs
会给你。
{
thumbnail_url: "https://i.ytimg.com/vi/DLzxrzFCyOs/hqdefault.jpg",
width: 459,
author_name: "AllKindsOfStuff",
version: "1.0",
author_url: "https://www.youtube.com/channel/UCLNd5EtH77IyN1frExzwPRQ",
thumbnail_width: 480,
type: "video",
provider_url: "https://www.youtube.com/",
html: "<iframe width="459" height="344" src=" frameborder="0" allowfullscreen></iframe>",
title: "Some title bla bla foo bar",
thumbnail_height: 360,
provider_name: "YouTube",
height: 344
}
阅读文档了解更多[信息][2]。
1:
你可以使用[parse_url][1],[parse_str][2]从YouTube视频url中获取视频ID,然后插入到图像的预测url中。 感谢YouTube提供的预测URLs
$videoUrl = "
parse_str( parse_url( $videoUrl, PHP_URL_QUERY ), $my_array_of_vars );
$ytID = $my_array_of_vars['v']; //gets video ID
print "https://img.youtube.com/vi/<?php print $ytID?>/maxresdefault.jpg";
print "https://img.youtube.com/vi/<?php print $ytID?>/mqdefault.jpg";
print "https://img.youtube.com/vi/<?php print $ytID?>/hqdefault.jpg";
print "https://img.youtube.com/vi/<?php print $ytID?>/sddefault.jpg";
print "https://img.youtube.com/vi/<?php print $ytID?>/default.jpg";
你可以使用这个工具来生成YouTube的缩略图。
https://tools.tutsplanet.com/index.php/get-youtube-video-thumbnails
[1]: http://php.net/manual/en/function.parse-url.php [2]: http://php.net/manual/en/function.parse-str.php
我为YouTube缩略图创建了一个简单的PHP函数,类型是
&amp;nbsp;
function get_youtube_thumb($link,$type){
$video_id = explode("?v=", $link);
if (empty($video_id[1])){
$video_id = explode("/v/", $link);
$video_id = explode("&", $video_id[1]);
$video_id = $video_id[0];
}
$thumb_link = "";
if($type == 'default' || $type == 'hqdefault' ||
$type == 'mqdefault' || $type == 'sddefault' ||
$type == 'maxresdefault'){
$thumb_link = 'http://img.youtube.com/vi/'.$video_id.'/'.$type.'.jpg';
}elseif($type == "id"){
$thumb_link = $video_id;
}
return $thumb_link;}
如果你使用的是公共API,最好的方法是使用if
语句。
如果视频是公开的或未上市的,你使用URL方法设置缩略图。 如果视频是私有的,你使用API来获取缩略图。
<?php
if($video_status == 'unlisted'){
$video_thumbnail = 'http://img.youtube.com/vi/'.$video_url.'/mqdefault.jpg';
$video_status = '<i class="fa fa-lock"></i> Unlisted';
}
elseif($video_status == 'public'){
$video_thumbnail = 'http://img.youtube.com/vi/'.$video_url.'/mqdefault.jpg';
$video_status = '<i class="fa fa-eye"></i> Public';
}
elseif($video_status == 'private'){
$video_thumbnail = $playlistItem['snippet']['thumbnails']['maxres']['url'];
$video_status = '<i class="fa fa-lock"></i> Private';
}