如果我有一个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视频都有四个生成的图像。可预测的格式如下:

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)获取缩略图图像。

为了添加/扩展所给出的解决方案,我觉得有必要注意到,正如我自己遇到的问题一样,实际上可以通过一个HTTP请求抓取多个YouTube视频内容,在本例中是缩略图:

使用Rest Client(在本例中为HTTPFUL),您可以执行以下操作:

<?php
header("Content-type", "application/json");

//download the httpfull.phar file from http://phphttpclient.com
include("httpful.phar");

$youtubeVidIds= array("nL-rk4bgJWU", "__kupr7KQos", "UCSynl4WbLQ", "joPjqEGJGqU", "PBwEBjX3D3Q");


$response = \Httpful\Request::get("https://www.googleapis.com/youtube/v3/videos?key=YourAPIKey4&part=snippet&id=".implode (",",$youtubeVidIds)."")

->send();

print ($response);

?>

我发现了一个很棒的工具,它可以让你用YouTube播放按钮创建图像:

安装在服务器上用于脚本编写:https://github.com/halgatewood/youtube-thumbnail-enhancer

将此代码保存在empty.php文件中并进行测试。

<img src="<?php echo youtube_img_src('9bZkp7q19f0', 'high');?>" />
<?php
// Get a YOUTUBE video thumb image's source url for IMG tag "src" attribute:
// $ID = YouYube video ID (string)
// $size = string (default, medium, high or standard)
function youtube_img_src ($ID = null, $size = 'default') {
    switch ($size) {
        case 'medium':
            $size = 'mqdefault';
            break;
        case 'high':
            $size = 'hqdefault';
            break;
        case 'standard':
            $size = 'sddefault';
            break;
        default:
            $size = 'default';
            break;
    }
    if ($ID) {
        return sprintf('https://img.youtube.com/vi/%s/%s.jpg', $ID, $size);
    }
    return 'https://img.youtube.com/vi/ERROR/1.jpg';
}

有一些缩略图保证存在:

Width | Height | URL
------|--------|----
120   | 90     | https://i.ytimg.com/vi/<VIDEO ID>/1.jpg
120   | 90     | https://i.ytimg.com/vi/<VIDEO ID>/2.jpg
120   | 90     | https://i.ytimg.com/vi/<VIDEO ID>/3.jpg
120   | 90     | https://i.ytimg.com/vi/<VIDEO ID>/default.jpg
320   | 180    | https://i.ytimg.com/vi/<VIDEO ID>/mq1.jpg
320   | 180    | https://i.ytimg.com/vi/<VIDEO ID>/mq2.jpg
320   | 180    | https://i.ytimg.com/vi/<VIDEO ID>/mq3.jpg
320   | 180    | https://i.ytimg.com/vi/<VIDEO ID>/mqdefault.jpg
480   | 360    | https://i.ytimg.com/vi/<VIDEO ID>/0.jpg
480   | 360    | https://i.ytimg.com/vi/<VIDEO ID>/hq1.jpg
480   | 360    | https://i.ytimg.com/vi/<VIDEO ID>/hq2.jpg
480   | 360    | https://i.ytimg.com/vi/<VIDEO ID>/hq3.jpg
480   | 360    | https://i.ytimg.com/vi/<VIDEO ID>/hqdefault.jpg

谢谢

Use:

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

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

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