]> jfr.im git - yt-dlp.git/commitdiff
[youtube] Extract even more thumbnails and reduce testing
authorpukkandan <redacted>
Mon, 19 Jul 2021 22:10:35 +0000 (03:40 +0530)
committerpukkandan <redacted>
Mon, 19 Jul 2021 22:16:06 +0000 (03:46 +0530)
* Also fix bug where `_test_url` was being ignored

Ref: https://stackoverflow.com/a/20542029
Related: #340

yt_dlp/YoutubeDL.py
yt_dlp/extractor/youtube.py

index d4d1af4fd7dda3decd53eee894f0e28636c117e1..3ed104e0fb8dc71016d22dc8f85260ee6ba13dc6 100644 (file)
@@ -1949,12 +1949,15 @@ def _sanitize_thumbnails(self, info_dict):
 
             def thumbnail_tester():
                 if self.params.get('check_formats'):
-                    def to_screen(msg):
-                        return self.to_screen(f'[info] {msg}')
+                    test_all = True
+                    to_screen = lambda msg: self.to_screen(f'[info] {msg}')
                 else:
+                    test_all = False
                     to_screen = self.write_debug
 
                 def test_thumbnail(t):
+                    if not test_all and not t.get('_test_url'):
+                        return True
                     to_screen('Testing thumbnail %s' % t['id'])
                     try:
                         self.urlopen(HEADRequest(t['url']))
index ae1c1bca5e8a4a9d6b554501f0efbb0799423413..92b16c438be980119c79495be7204b533f72e352 100644 (file)
@@ -2658,8 +2658,6 @@ def feed_entry(name):
         owner_profile_url = microformat.get('ownerProfileUrl')
 
         thumbnails = []
-        thumbnail_types = ['maxresdefault', 'sddefault', 'hqdefault', '0', 'mqdefault', 'default', '1', '2', '3']
-
         for container in (video_details, microformat):
             for thumbnail in (try_get(
                     container,
@@ -2684,14 +2682,24 @@ def feed_entry(name):
             })
         # 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>
+        hq_thumbnail_names = ['maxresdefault', 'hq720', 'sddefault', 'sd1', 'sd2', 'sd3']
+        guaranteed_thumbnail_names = [
+            'hqdefault', 'hq1', 'hq2', 'hq3', '0',
+            'mqdefault', 'mq1', 'mq2', 'mq3',
+            'default', '1', '2', '3'
+        ]
+        thumbnail_names = hq_thumbnail_names + guaranteed_thumbnail_names
+        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,
                 webp='_webp' if ext == 'webp' else '', live='_live' if is_live else ''),
-            '_test_url': True,
-        } for name in thumbnail_types for ext in ('webp', 'jpg'))
+            '_test_url': name in hq_thumbnail_names,
+        } for name in thumbnail_names for ext in ('webp', 'jpg'))
         for thumb in thumbnails:
-            i = next((i for i, t in enumerate(thumbnail_types) if f'/{video_id}/{t}' in thumb['url']), 20)
+            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)