How to get a youtube playlist thumbnail?

☆樱花仙子☆ 提交于 2019-12-22 04:01:32

问题


Is there a way to get youtube playlist thumbnail like how you can get a thumbnail of a video, explained here: How do I get a YouTube video thumbnail from the YouTube API?


回答1:


With the YouTube API v3, you can get the playlist thumbnail using their v3/playlists endpoint and drilling down to items.snippet.thumbnails.high.url

For the following playlist:

https://www.youtube.com/playlist?list=PL50C17441DA8A565D

YouTube's API explorer has a playlist endpoint:

https://developers.google.com/youtube/v3/docs/playlists/list

This is the API call:

GET https://www.googleapis.com/youtube/v3/playlists?part=snippet&id=PL50C17441DA8A565D&key={YOUR_API_KEY}

And here is the response:

    {
     "kind": "youtube#playlistListResponse",
     "items": [
      {
       "kind": "youtube#playlist",
       "id": "PL50C17441DA8A565D",
       "snippet": {
        "title": "Jay Chou Playlist",
        "thumbnails": {
         "default": {
          "url": "https://i.ytimg.com/vi/kGbDymJ75PU/default.jpg",
          "width": 120,
          "height": 90
         },
         "medium": {
          "url": "https://i.ytimg.com/vi/kGbDymJ75PU/mqdefault.jpg",
          "width": 320,
          "height": 180
         },
         "high": {
          "url": "https://i.ytimg.com/vi/kGbDymJ75PU/hqdefault.jpg",
          "width": 480,
          "height": 360
         }
        },
        "channelTitle": "it23"
       }
      }
     ]
    }



回答2:


This is what worked for me. Similar to the other post regarding playlist items but I just changed the API call. Hope it helps.

//get playlist itmes

$data = file_get_contents("https://www.googleapis.com/youtube/v3/playlistItems?key=< YOUR API KEY >&part=snippet&playlistId="<Put your playlist id here>");

//decode response from youtube

$json = json_decode($data);

//if you want to see the full response use print_r($json);

//each items in the response corresponds to a video in the playlist so lets get the first video (items[0]) get its snippet and corresponding thumbnails and we will take the url for the default size. If you want the second video use items [1] etc.

$video_thumbnail = $json->items[0]->snippet->thumbnails->default->url;

//set the videos alt tag as the snippet description

$video_alt = $json->items[0]->snippet->description; 

//echo the image url into an image tag

echo '<img src="'. $video_thumbnail .'" alt="'. $video_alt .'" />';



回答3:


There is no API for this purpose. Here is a workaround I can think of.

1) What youtube playlist thumbnail is? Played around with my account and seems the rule is as below:

if playlist has 5+ videos
  thumbnail = first 5 video images
else if playlist has 1+ videos
  thumbnail = all video images
else shows this list is empty

2) Then you can use the same way to get video thumbnails.

3) combine the images either on the client side (some CSS) or server side



来源:https://stackoverflow.com/questions/10628760/how-to-get-a-youtube-playlist-thumbnail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!