How to get a youtube playlist thumbnail?

后端 未结 3 1893
梦如初夏
梦如初夏 2021-02-20 00:09

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?

相关标签:
3条回答
  • 2021-02-20 00:28

    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"
           }
          }
         ]
        }
    
    0 讨论(0)
  • 2021-02-20 00:34

    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

    0 讨论(0)
  • 2021-02-20 00:41

    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 .'" />';
    
    0 讨论(0)
提交回复
热议问题