When the media contain a playlist, the thumbnail do not load correctly

Created on 23 November 2023, over 1 year ago
Updated 24 November 2023, over 1 year ago

Problem/Motivation

When a youtube iframe contains an embed list, for example "https://www.youtube.com/embed/videoseries?list=PLpVC00PAQQxGFC06mLqoPT4h...", the thumbnail do not load properly.
Example of the HTML:
It takes "videoseries" as the URL of the video.

<picture class="youtube-cookies__thumbnail__picture">
<source srcset="//i.ytimg.com/vi_webp/videoseries/sddefault.webp, //i.ytimg.com/vi_webp/videoseries/maxresdefault.webp 2x" type="image/webp">
<source srcset="//i.ytimg.com/vi/videoseries/sddefault.jpg, //i.ytimg.com/vi/videoseries/maxresdefault.jpg 2x" type="image/jpeg">
<img class="oembed-lazyload__img" src="//i.ytimg.com/vi/videoseries/sddefault.jpg" width="300" height="182">
</picture>
🐛 Bug report
Status

Needs work

Version

2.0

Component

Code

Created by

🇪🇸Spain eduardo morales alberti Spain, 🇪🇺

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • 🇪🇸Spain eduardo morales alberti Spain, 🇪🇺

    The way to get the thumbnail for video list is "https://www.youtube.com/oembed?url=https://www.youtube.com/embed/videose..."

    {
      "title": "Drupal Training - Site Building",
      "author_name": "Acquia",
      "author_url": "/@AcquiaTV",
      "type": "video",
      "height": 113,
      "width": 200,
      "version": "1.0",
      "provider_name": "YouTube",
      "provider_url": "https://www.youtube.com/",
      "thumbnail_height": 360,
      "thumbnail_width": 480,
      "thumbnail_url": "https://i.ytimg.com/vi/rAR1QDAS7og/hqdefault.jpg",
      "html": "<iframe width=\"200\" height=\"113\" src=\"https://www.youtube.com/embed/videoseries?list=PLpVC00PAQQxGFC06mLqoPT4hHaA1Ykn2Z\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen></iframe>"
    }
    

    And the max resolution can be get using "https://i.ytimg.com/vi/rAR1QDAS7og/maxresdefault.jpg"

  • 🇪🇸Spain eduardo morales alberti Spain, 🇪🇺

    We added an example of what we were trying to do. Obtain the thumbnail from oembed URL "https://www.youtube.com/oembed?url=https://www.youtube.com/embed/videose..." and because is an async call, then call "setYoutubeThumbnailFromId" function or something similar to overrides the thumbnail id.

    It could be possible to do it on the Filter plugin using PHP and set the thumbnail URL, it could avoid async petitions on the front end.

    At the moment we can not invest more time, because we will migrate to media, which has another strategy for the thumbnails.

    YoutubeCookies.prototype.getJsonFromUrl = function(url, callback) {
      let xhr = new XMLHttpRequest();
      xhr.open("GET", url, true);
      xhr.responseType = "json";
      xhr.onload = function() {
        var status = xhr.status;
        if (status === 200) {
          callback(null, xhr.response, this);
        } else {
          callback(status, xhr.response, this);
        }
      };
      xhr.send();
    };
    
    YoutubeCookies.prototype.setVideoIdPlaylist = function(url) {
    
      let oembed_url = 'https://www.youtube.com/oembed?url=' + url;
    
      this.getJsonFromUrl(oembed_url, function(err, data) {
        if (err == null) {
          let regex = /(?:https?:\/\/)(?:i\.ytimg\.com\/vi\/)([0-9A-Za-z_-]*)/i;
          let thumbnail_url = data.thumbnail_url;
          let match = thumbnail_url.match(regex);
          if (err == null && typeof data === "object"
            && data.hasOwnProperty("thumbnail_url")
            && data.thumbnail_url.match(regex)) {
            let videoIdMatches = data.thumbnail_url.match(regex);
            if (videoIdMatches !== null && videoIdMatches.length > 0) {
              this.setYoutubeThumbnailFromId(videoIdMatches[videoIdMatches.length - 1]);
            }
          }
    
        }
      });
    
    };
    
    /**
     * Given a youtube video URL get its ID.
     *
     * @param {string} url
     *   Youtube video URL.
     *
     * @return {null|*}
     *   Video id, if exists.
     */
    YoutubeCookies.prototype.getYoutubeIdFromUrl = function(url) {
      // if (url.includes('embed/videoseries')) {
      //   this.setVideoIdPlaylist(url);
      // }
    
Production build 0.71.5 2024