How do I get a YouTube video thumbnail from the Yo

2018-12-31 01:40发布

If I have a YouTube video URL, is there any way to use PHP and cURL to get the associated thumbnail from the YouTube API?

30条回答
情到深处是孤独
2楼-- · 2018-12-31 02:00

I made a function to only fetch existing images from YouTube

function youtube_image($id) {
    $resolution = array (
        'maxresdefault',
        'sddefault',
        'mqdefault',
        'hqdefault',
        'default'
    );

    for ($x = 0; $x < sizeof($resolution); $x++) {
        $url = '//img.youtube.com/vi/' . $id . '/' . $resolution[$x] . '.jpg';
        if (get_headers($url)[0] == 'HTTP/1.0 200 OK') {
            break;
        }
    }
    return $url;
}
查看更多
不再属于我。
3楼-- · 2018-12-31 02:00

If you're using the public api, the best way to do it is using if statements.

If the video is public or unlisted, you set the thumbnail using the url method. If the video is private you use the api to get the thumbnail.

<?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';
}
查看更多
只若初见
4楼-- · 2018-12-31 02:00

Save file as .js

  var maxVideos = 5;
  $(document).ready(function(){
  $.get(
    "https://www.googleapis.com/youtube/v3/videos",{
      part: 'snippet,contentDetails',
      id:'your_video_id',
      kind: 'youtube#videoListResponse',
      maxResults: maxVideos,
      regionCode: 'IN',
      key: 'Your_API_KEY'},
      function(data){
        var output;
        $.each(data.items, function(i, item){
          console.log(item);
                thumb = item.snippet.thumbnails.high.url;
          output = '<div id="img"><img src="' + thumb + '"></div>';
          $('#thumbnail').append(output);
        })
        
      }
    );
}); 
.main{
 width:1000px;
 margin:auto;
}
#img{
float:left;
display:inline-block;
margin:5px;
}
<!DOCTYPE html>
<html>
<head>
  <title>Thumbnails</title>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>
</head>
<body>
<div class="main">
 <ul id="thumbnail"> </ul>
</div>
</body>
</html>

查看更多
若你有天会懂
5楼-- · 2018-12-31 02:01

Each YouTube video has 4 generated images. They are predictably formatted as follows:

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

The first one in the list is a full size image and others are thumbnail images. The default thumbnail image (ie. one of 1.jpg, 2.jpg, 3.jpg) is:

https://img.youtube.com/vi/<insert-youtube-video-id-here>/default.jpg

For the high quality version of the thumbnail use a url similar to this:

https://img.youtube.com/vi/<insert-youtube-video-id-here>/hqdefault.jpg

There is also a medium quality version of the thumbnail, using a url similar to the HQ:

https://img.youtube.com/vi/<insert-youtube-video-id-here>/mqdefault.jpg

For the standard definition version of the thumbnail, use a url similar to this:

https://img.youtube.com/vi/<insert-youtube-video-id-here>/sddefault.jpg

For the maximum resolution version of the thumbnail use a url similar to this:

https://img.youtube.com/vi/<insert-youtube-video-id-here>/maxresdefault.jpg

All of the above urls are available over http too. Additionally, the slightly shorter hostname i3.ytimg.com works in place of img.youtube.com in the example urls above.

Alternatively, you can use the YouTube Data API (v3) to get thumbnail images.

查看更多
余欢
6楼-- · 2018-12-31 02:01

If you want to get rid of the "black bars" and do it like YouTube does it, you can use :

https://i.ytimg.com/vi_webp/<video id>/mqdefault.webp

And if you can't use .webp extension you can do it like this :

https://i.ytimg.com/vi/<video id>/mqdefault.jpg

Also, if you need the unscaled version use maxresdefault instead of mqdefault.

Note: I'm not sure about the aspect ratio if you're planning to use maxresdefault.

查看更多
萌妹纸的霸气范
7楼-- · 2018-12-31 02:01

You can get the Video Entry which contains the URL to the video's thumbnail. There's example code in the link. Or, if you want to parse XML, there's information here. The XML returned has a media:thumbnail element, which contains the thumbnail's URL.

查看更多
登录 后发表回答