如果我有一个YouTube视频URL,有没有任何方法可以使用PHP和cURL从YouTube API获取相关的缩略图?
当前回答
https://i.ytimg.com/vi/<--Video ID-->/default.jpg
图像大小权重120px高度90px
https://i.ytimg.com/vi/<--Video ID-->/mqdefault.jpg
图像大小重量320px高度180px
https://i.ytimg.com/vi/<--Video ID-->/hqdefault.jpg
图像大小重量480px高度360px
https://i.ytimg.com/vi/<--Video ID-->/sddefault.jpg
图像大小重量640px高度480px
https://i.ytimg.com/vi/<--Video ID-->/maxresdefault.jpg
图像大小重量1280px高度720px
其他回答
如果您使用的是公共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';
}
如果你想要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);
我为YouTube缩略图创建了一个简单的PHP函数,类型如下
违约hq默认值MQ默认值sd默认值最大默认值
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;}
虽然已经有很多答案,但对于新访客,我会留下两个获取缩略图的选项。
通过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);
https://i.ytimg.com/vi/<--Video ID-->/default.jpg
图像大小权重120px高度90px
https://i.ytimg.com/vi/<--Video ID-->/mqdefault.jpg
图像大小重量320px高度180px
https://i.ytimg.com/vi/<--Video ID-->/hqdefault.jpg
图像大小重量480px高度360px
https://i.ytimg.com/vi/<--Video ID-->/sddefault.jpg
图像大小重量640px高度480px
https://i.ytimg.com/vi/<--Video ID-->/maxresdefault.jpg
图像大小重量1280px高度720px
推荐文章
- 在PHP单元测试执行期间,如何在CLI中输出?
- 在PHP中使用heredoc的优势是什么?
- PHP中的echo, print和print_r有什么区别?
- 在Bash中将输出赋给变量
- 如何将XML转换成PHP数组?
- 如何将对象转换为数组?
- 从IP地址获取位置
- 获取数组值的键名
- HTTPS和SSL3_GET_SERVER_CERTIFICATE:证书验证失败,CA is OK
- PHP -获取bool值,当为false时返回false
- 在foreach中通过引用传递
- 如何触发命令行PHP脚本的XDebug分析器?
- 如何找出如果你使用HTTPS没有$_SERVER['HTTPS']
- 更好的方法检查变量为null或空字符串?
- 当使用Composer的开发/生产开关时,如何正确部署?