如果我有一个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归谷歌所有,谷歌喜欢为不同的屏幕大小提供合理数量的图像,因此其图像以不同的大小存储。下面是缩略图的示例:

低质量缩略图:

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上某个特定视频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);

?>

每个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获取缩略图

在Google Cloud Platform注册应用程序并激活YouTube Data API v3库在凭据部分创建API密钥。这样您将获得访问API的密钥并发送有关视频信息的请求,包括获取缩略图。


$api_key = 'YOUR_API_KEY';
$youtube_video_id = 'jNQXAC9IVRw';
$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://www.googleapis.com/youtube/v3/videos?key='.$api_key.'&part=snippet&id='.$youtube_video_id,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'GET',
)); 

$response = curl_exec($curl); 
curl_close($curl);

$response = json_decode($response,true); 

print_r($response); // result with video information and thumbnails

从没有API的直接链接获取缩略图

除了API之外,还可以通过直接链接获取缩略图,如下所示:

https://i.ytimg.com/vi/jNQXAC9IVRw/hqdefault.jpg

让我们详细考虑一下这个选项:

 https://i.ytimg.com/vi/<YOUTUBE_VIDEO_ID>/<SIZE_VALUE>.jpg 

哪里:

YOUTUBE_VIDEO_ID:您的视频IDSIZE_VALUE:缩略图大小。变量可以包含以下值:default、mqdefault、hqdefault、sddefault、maxresdefault


$youtube_video_id = 'jNQXAC9IVRw';
$size = 'hqdefault';

$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => "https://i.ytimg.com/vi/{$youtube_video_id}/{$size}.jpg",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'GET',
));
$response = curl_exec($curl);
curl_close($curl);

// Write the file
$handle = fopen("image/filename.jpg", 'w'); // set your directory and filename
fwrite($handle, $response);
fclose($handle);