如果我有一个YouTube视频URL,有没有任何方法可以使用PHP和cURL从YouTube API获取相关的缩略图?


当前回答

YouTube数据API

YouTube通过Data API(v3)为我们提供每个视频的四个生成图像,

https://i.ytimg.com/vi/V_zwalcR8DU/maxresdefault.jpghttps://i.ytimg.com/vi/V_zwalcR8DU/sddefault.jpghttps://i.ytimg.com/vi/V_zwalcR8DU/hqdefault.jpghttps://i.ytimg.com/vi/V_zwalcR8DU/mqdefault.jpg

通过API访问图像

首先在GoogleAPI控制台获取公共API密钥。根据API文档中YouTube的缩略图参考,您需要访问snippet.thumbnail上的资源。根据这一点,你需要这样表述你的URL:www.googleapis.com/youtube/v3/videos?part=片段&id=`yourVideoId`&key=`yourApiKey`

现在,将视频ID和API密钥更改为相应的视频ID和API密钥,其响应将是JSON输出,为您提供代码片段变量缩略图中的四个链接(如果所有链接都可用)。

其他回答

如果你想要YouTube上某个特定视频ID的最大图片,那么URL应该是这样的:

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);

为了添加/扩展所给出的解决方案,我觉得有必要注意到,正如我自己遇到的问题一样,实际上可以通过一个HTTP请求抓取多个YouTube视频内容,在本例中是缩略图:

使用Rest Client(在本例中为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);

?>

您可以使用parse_url、parse_str从YouTube视频url获取视频ID,然后插入到图像的预测url中。感谢YouTube提供的预测URL

$videoUrl = "https://www.youtube.com/watch?v=8zy7wGbQgfw";
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/$ytID/maxresdefault.jpg";
print "https://img.youtube.com/vi/$ytID/mqdefault.jpg";
print "https://img.youtube.com/vi/$ytID/hqdefault.jpg";
print "https://img.youtube.com/vi/$ytID/sddefault.jpg";
print "https://img.youtube.com/vi/$ytID/default.jpg";

您可以使用此工具生成YouTube缩略图

https://youtube-thumbnail-tool.com

YouTube数据API

YouTube通过Data API(v3)为我们提供每个视频的四个生成图像,

https://i.ytimg.com/vi/V_zwalcR8DU/maxresdefault.jpghttps://i.ytimg.com/vi/V_zwalcR8DU/sddefault.jpghttps://i.ytimg.com/vi/V_zwalcR8DU/hqdefault.jpghttps://i.ytimg.com/vi/V_zwalcR8DU/mqdefault.jpg

通过API访问图像

首先在GoogleAPI控制台获取公共API密钥。根据API文档中YouTube的缩略图参考,您需要访问snippet.thumbnail上的资源。根据这一点,你需要这样表述你的URL:www.googleapis.com/youtube/v3/videos?part=片段&id=`yourVideoId`&key=`yourApiKey`

现在,将视频ID和API密钥更改为相应的视频ID和API密钥,其响应将是JSON输出,为您提供代码片段变量缩略图中的四个链接(如果所有链接都可用)。

我认为缩略图有很多答案,但我想添加一些其他URL,以便非常容易地获得YouTube缩略图。我只是从亚萨的回答中提取一些文字。以下是获取YouTube缩略图的一些URL:

https://ytimg.googleusercontent.com/vi/<insert-youtube-video-id-here>/default.jpg

对于缩略图的高质量版本,请使用类似于以下内容的URL:

https://ytimg.googleusercontent.com/vi/<insert-youtube-video-id-here>/hqdefault.jpg

还有一个中等质量的缩略图版本,使用与高质量类似的URL:

https://ytimg.googleusercontent.com/vi/<insert-youtube-video-id-here>/mqdefault.jpg

对于缩略图的标准定义版本,请使用类似于以下内容的URL:

https://ytimg.googleusercontent.com/vi/<insert-youtube-video-id-here>/sddefault.jpg

对于缩略图的最大分辨率版本,请使用类似于以下内容的URL:

https://ytimg.googleusercontent.com/vi/<insert-youtube-video-id-here>/maxresdefault.jpg