如果我有一个YouTube视频URL,有没有任何方法可以使用PHP和cURL从YouTube API获取相关的缩略图?
当前回答
如果你想要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 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正在从两个服务器提供缩略图。你只需要用你自己的YouTube视频ID替换<YouTube_Video_ID_HERE>。如今,由于图像尺寸较小,webP是快速加载图像的最佳格式。
https://img.youtube.comhttps://i.ytimg.com
示例包括https://i.ytimg.com服务器只是因为它更短,没有其他特别的原因。两者都可以使用。
播放机背景缩略图(480x360):
WebP
https://i.ytimg.com/vi_webp/<YouTube_Video_ID_HERE>/0.webp
JPG
https://i.ytimg.com/vi/<YouTube_Video_ID_HERE>/0.jpg
视频帧缩略图(120x90)
WebP:
Start: https://i.ytimg.com/vi_webp/<YouTube_Video_ID_HERE>/1.webp
Middle: https://i.ytimg.com/vi_webp/<YouTube_Video_ID_HERE>/2.webp
End: https://i.ytimg.com/vi_webp/<YouTube_Video_ID_HERE>/3.webp
JPG:
Start: https://i.ytimg.com/vi/<YouTube_Video_ID_HERE>/1.jpg
Middle: https://i.ytimg.com/vi/<YouTube_Video_ID_HERE>/2.jpg
End: https://i.ytimg.com/vi/<YouTube_Video_ID_HERE>/3.jpg
最低质量缩略图(120x90)
WebP
https://i.ytimg.com/vi_webp/<YouTube_Video_ID_HERE>/default.webp
JPG
https://i.ytimg.com/vi/<YouTube_Video_ID_HERE>/default.jpg
中等质量缩略图(320x180)
WebP
https://i.ytimg.com/vi_webp/<YouTube_Video_ID_HERE>/mqdefault.webp
JPG
https://i.ytimg.com/vi/<YouTube_Video_ID_HERE>/mqdefault.jpg
高品质缩略图(480x360)
WebP
https://i.ytimg.com/vi_webp/<YouTube_Video_ID_HERE>/hqdefault.webp
JPG
https://i.ytimg.com/vi/<YouTube_Video_ID_HERE>/hqdefault.jpg
标准质量缩略图(640x480)
WebP
https://i.ytimg.com/vi_webp/<YouTube_Video_ID_HERE>/sddefault.webp
JPG
https://i.ytimg.com/vi/<YouTube_Video_ID_HERE>/sddefault.jpg
未缩放缩略图分辨率
WebP
https://i.ytimg.com/vi_webp/<YouTube_Video_ID_HERE>/maxresdefault.webp
JPG
https://i.ytimg.com/vi/<YouTube_Video_ID_HERE>/maxresdefault.jpg
我发现了一个很棒的工具,它可以让你用YouTube播放按钮创建图像:
安装在服务器上用于脚本编写:https://github.com/halgatewood/youtube-thumbnail-enhancer
Use:
https://www.googleapis.com/youtube/v3/videoCategories?part=snippet,id&maxResults=100®ionCode=us&key=**Your YouTube ID**
上面是链接。使用它,你可以找到YouTube视频的特点。找到特征后,您可以获得所选类别的视频。之后,您可以使用Asaph的答案找到选定的视频图像。
尝试以上方法,您可以解析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输出,为您提供代码片段变量缩略图中的四个链接(如果所有链接都可用)。
推荐文章
- 原则-如何打印出真正的sql,而不仅仅是准备好的语句?
- 如何从关联PHP数组中获得第一项?
- PHP/MySQL插入一行然后获取id
- 我如何排序一个多维数组在PHP
- 如何在PHP中截断字符串最接近于一定数量的字符?
- PHP错误:“zip扩展名和unzip命令都没有,跳过。”
- Nginx提供下载。php文件,而不是执行它们
- Json_encode()转义正斜杠
- 如何在PHP中捕获cURL错误
- 如何要求一个分叉与作曲家?
- 如何在php中创建可选参数?
- 在文本文件中创建或写入/追加
- 为什么PHP的json_encode函数转换UTF-8字符串为十六进制实体?
- 如何从一个查询插入多行使用雄辩/流利
- 在PHP单元测试执行期间,如何在CLI中输出?