首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从YouTube接口获取YouTube视频缩略图?

如何从YouTube接口获取YouTube视频缩略图?
EN

Stack Overflow用户
提问于 2010-01-15 07:34:25
回答 26查看 1.8M关注 1票数 2.6K

如果我有一个YouTube视频地址,有没有办法使用PHP和cURL从YouTube应用程序接口中获取相关的缩略图?

EN

回答 26

Stack Overflow用户

回答已采纳

发布于 2010-01-15 07:40:26

每个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.jpg2.jpg3.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访问。此外,在上面的示例URL中,可以使用稍微短一些的主机名i3.ytimg.com来代替img.youtube.com

或者,您可以使用YouTube Data API (v3)来获取缩略图。

票数 5.1K
EN

Stack Overflow用户

发布于 2013-08-23 18:23:59

在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不会被防火墙阻止。

票数 84
EN

Stack Overflow用户

发布于 2010-12-22 13:44:05

如果你想要一个特定视频ID的YouTube的最大图片,那么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);
票数 58
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2068344

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档