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


当前回答

如果您使用的是公共API,最好的方法是使用If语句。

如果视频是公开的或未列出的,则可以使用URL方法设置缩略图。如果视频是私人的,则使用API获取缩略图。

<?php
    if($video_status == 'unlisted'){
        $video_thumbnail = 'http://img.youtube.com/vi/'.$video_url.'/mqdefault.jpg';
        $video_status = '<i class="fa fa-lock"></i>&nbsp;Unlisted';
    }
    elseif($video_status == 'public'){
        $video_thumbnail = 'http://img.youtube.com/vi/'.$video_url.'/mqdefault.jpg';
        $video_status = '<i class="fa fa-eye"></i>&nbsp;Public';
    }
    elseif($video_status == 'private'){
        $video_thumbnail = $playlistItem['snippet']['thumbnails']['maxres']['url'];
        $video_status = '<i class="fa fa-lock"></i>&nbsp;Private';
    }

其他回答

YouTube API版本3在2分钟内启动并运行

如果您只想搜索YouTube并获取相关的财产:

获取公共API--此链接提供了良好的方向使用以下查询字符串。出于示例目的,URL字符串中的搜索查询(由q=表示)是stackoverflow。然后YouTube将向您发送一个JSON回复,您可以在其中解析缩略图、代码段、作者等。https://www.googleapis.com/youtube/v3/search?part=id%2Csnippet&maxResults=50&q=stackoverflow&key=YOUR_API_KEY_HERE

// Get image form video URL
$url = $video['video_url'];

$urls = parse_url($url);

//Expect the URL to be http://youtu.be/abcd, where abcd is the video ID
if ($urls['host'] == 'youtu.be') :

    $imgPath = ltrim($urls['path'],'/');

//Expect the URL to be http://www.youtube.com/embed/abcd
elseif (strpos($urls['path'],'embed') == 1) :

    $imgPath = end(explode('/',$urls['path']));

//Expect the URL to be abcd only
elseif (strpos($url,'/') === false):

    $imgPath = $url;

//Expect the URL to be http://www.youtube.com/watch?v=abcd
else :

    parse_str($urls['query']);

    $imgPath = $v;

endif;
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

使用img.youtube.com/vi/YouTubeID/ImageFormat.jpg

这里的图像格式不同,最大默认值。

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

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