如果我有一个YouTube视频URL,有没有任何方法可以使用PHP和cURL从YouTube API获取相关的缩略图?


当前回答

在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数据API检索视频缩略图、标题、描述、评级、统计信息等。API版本3需要密钥*。获取密钥并创建视频:列表请求:

https://www.googleapis.com/youtube/v3/videos?key=YOUR_API_KEY&part=snippet&id=VIDEO_ID

示例PHP代码

$data = file_get_contents("https://www.googleapis.com/youtube/v3/videos?key=YOUR_API_KEY&part=snippet&id=T0Jqdjbed40");
$json = json_decode($data);
var_dump($json->items[0]->snippet->thumbnails);

输出

object(stdClass)#5 (5) {
  ["default"]=>
  object(stdClass)#6 (3) {
    ["url"]=>
    string(46) "https://i.ytimg.com/vi/T0Jqdjbed40/default.jpg"
    ["width"]=>
    int(120)
    ["height"]=>
    int(90)
  }
  ["medium"]=>
  object(stdClass)#7 (3) {
    ["url"]=>
    string(48) "https://i.ytimg.com/vi/T0Jqdjbed40/mqdefault.jpg"
    ["width"]=>
    int(320)
    ["height"]=>
    int(180)
  }
  ["high"]=>
  object(stdClass)#8 (3) {
    ["url"]=>
    string(48) "https://i.ytimg.com/vi/T0Jqdjbed40/hqdefault.jpg"
    ["width"]=>
    int(480)
    ["height"]=>
    int(360)
  }
  ["standard"]=>
  object(stdClass)#9 (3) {
    ["url"]=>
    string(48) "https://i.ytimg.com/vi/T0Jqdjbed40/sddefault.jpg"
    ["width"]=>
    int(640)
    ["height"]=>
    int(480)
  }
  ["maxres"]=>
  object(stdClass)#10 (3) {
    ["url"]=>
    string(52) "https://i.ytimg.com/vi/T0Jqdjbed40/maxresdefault.jpg"
    ["width"]=>
    int(1280)
    ["height"]=>
    int(720)
  }
}

*不仅需要密钥,还可能会根据您计划发出的API请求的数量要求您提供计费信息。然而,每天有几千个请求是免费的。

源文章。

Use:

https://www.googleapis.com/youtube/v3/videoCategories?part=snippet,id&maxResults=100&regionCode=us&key=**Your YouTube ID**

上面是链接。使用它,你可以找到YouTube视频的特点。找到特征后,您可以获得所选类别的视频。之后,您可以使用Asaph的答案找到选定的视频图像。

尝试以上方法,您可以解析YouTube API中的所有内容。

另一个好的选择是使用YouTube支持的oEmbed API。

您只需将YouTube URL添加到oEmbed URL,就会收到一个JSON,其中包含缩略图和用于嵌入的HTML代码。

例子:

http://www.youtube.com/oembed?format=json&url=http%3A//youtube.com/watch%3Fv%3DxUeJdWYdMmQ

会给你:

{
  "height":270,
  "width":480,
  "title":"example video for 2020",
  "thumbnail_width":480,
  "html":"...",
  "thumbnail_height":360,
  "version":"1.0",
  "provider_name":"YouTube",
  "author_url":"https:\/\/www.youtube.com\/channel\/UCza6VSQUzCON- AzlsrOLwaA",
  "thumbnail_url":"https:\/\/i.ytimg.com\/vi\/xUeJdWYdMmQ\/hqdefault.jpg",
  "author_name":"Pokics",
  "provider_url":"https:\/\/www.youtube.com\/",
  "type":"video"
}

有关详细信息,请阅读文档。

每个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缩略图的方式如下:

$url = 'http://img.youtube.com/vi/' . $youtubeId . '/0.jpg';
$img = dirname(__FILE__) . '/youtubeThumbnail_'  . $youtubeId . '.jpg';
file_put_contents($img, file_get_contents($url));

请记住,YouTube禁止直接从其服务器中包含图像。