]> jfr.im git - yt-dlp.git/commitdiff
[youtube] Populate `thumbnail` with the best "known" thumbnail
authorpukkandan <redacted>
Sun, 24 Oct 2021 09:25:28 +0000 (14:55 +0530)
committerpukkandan <redacted>
Sun, 24 Oct 2021 09:30:18 +0000 (15:00 +0530)
Closes #402, Related: https://github.com/yt-dlp/yt-dlp/issues/340#issuecomment-950290624

yt_dlp/extractor/youtube.py

index 6a7a2ce1a4961c343a9f22361322168136d80c6b..658b45fe14fe2697b96bb0c86b165f9c92605a7b 100644 (file)
@@ -2696,6 +2696,8 @@ def feed_entry(name):
             thumbnails.append({
                 'url': thumbnail_url,
             })
+        original_thumbnails = thumbnails.copy()
+
         # The best resolution thumbnails sometimes does not appear in the webpage
         # See: https://github.com/ytdl-org/youtube-dl/issues/29049, https://github.com/yt-dlp/yt-dlp/issues/340
         # List of possible thumbnails - Ref: <https://stackoverflow.com/a/20542029>
@@ -2706,7 +2708,6 @@ def feed_entry(name):
             'default', '1', '2', '3'
         ]
         n_thumbnail_names = len(thumbnail_names)
-
         thumbnails.extend({
             'url': 'https://i.ytimg.com/vi{webp}/{video_id}/{name}{live}.{ext}'.format(
                 video_id=video_id, name=name, ext=ext,
@@ -2716,6 +2717,7 @@ def feed_entry(name):
             i = next((i for i, t in enumerate(thumbnail_names) if f'/{video_id}/{t}' in thumb['url']), n_thumbnail_names)
             thumb['preference'] = (0 if '.webp' in thumb['url'] else -1) - (2 * i)
         self._remove_duplicate_formats(thumbnails)
+        self._downloader._sort_thumbnails(original_thumbnails)
 
         category = get_first(microformats, 'category') or search_meta('genre')
         channel_id = str_or_none(
@@ -2745,6 +2747,9 @@ def feed_entry(name):
             'title': self._live_title(video_title) if is_live else video_title,
             'formats': formats,
             'thumbnails': thumbnails,
+            # The best thumbnail that we are sure exists. Prevents unnecessary
+            # URL checking if user don't care about getting the best possible thumbnail
+            'thumbnail': traverse_obj(original_thumbnails, (-1, 'url')),
             'description': video_description,
             'upload_date': unified_strdate(
                 get_first(microformats, 'uploadDate')