如果我有一个YouTube视频URL,有没有任何方法可以使用PHP和cURL从YouTube API获取相关的缩略图?
当前回答
我认为缩略图有很多答案,但我想添加一些其他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
其他回答
如果你想要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数据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输出,为您提供代码片段变量缩略图中的四个链接(如果所有链接都可用)。
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
我为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归谷歌所有,谷歌喜欢为不同的屏幕大小提供合理数量的图像,因此其图像以不同的大小存储。下面是缩略图的示例:
低质量缩略图:
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
推荐文章
- 原则-如何打印出真正的sql,而不仅仅是准备好的语句?
- 如何从关联PHP数组中获得第一项?
- PHP/MySQL插入一行然后获取id
- 我如何排序一个多维数组在PHP
- 如何在PHP中截断字符串最接近于一定数量的字符?
- PHP错误:“zip扩展名和unzip命令都没有,跳过。”
- Nginx提供下载。php文件,而不是执行它们
- Json_encode()转义正斜杠
- 如何在PHP中捕获cURL错误
- 如何要求一个分叉与作曲家?
- 如何在php中创建可选参数?
- 在文本文件中创建或写入/追加
- 为什么PHP的json_encode函数转换UTF-8字符串为十六进制实体?
- 如何从一个查询插入多行使用雄辩/流利
- 在PHP单元测试执行期间,如何在CLI中输出?