From 9c99de6d9bd73351bdb12e2dfd62de49119bbde6 Mon Sep 17 00:00:00 2001 From: 7x11x13 Date: Sat, 14 Dec 2024 00:10:04 -0500 Subject: [PATCH] Add --thumbnail-id option --- yt_dlp/YoutubeDL.py | 8 ++++++++ yt_dlp/__init__.py | 1 + yt_dlp/options.py | 4 ++++ 3 files changed, 13 insertions(+) diff --git a/yt_dlp/YoutubeDL.py b/yt_dlp/YoutubeDL.py index 65b72e026c..615c216b42 100644 --- a/yt_dlp/YoutubeDL.py +++ b/yt_dlp/YoutubeDL.py @@ -291,6 +291,7 @@ class YoutubeDL: unless writeinfojson is also given writeannotations: Write the video annotations to a .annotations.xml file writethumbnail: Write the thumbnail image to a file + thumbnail_id: ID of thumbnail to write allow_playlist_files: Whether to write playlists' description, infojson etc also to disk when using the 'write*' options write_all_thumbnails: Write all thumbnail formats to files @@ -2605,6 +2606,7 @@ class YoutubeDL: def _sort_thumbnails(self, thumbnails): thumbnails.sort(key=lambda t: ( + t.get('id') == self.params.get('thumbnail_id') if t.get('id') is not None else False, t.get('preference') if t.get('preference') is not None else -1, t.get('width') if t.get('width') is not None else -1, t.get('height') if t.get('height') is not None else -1, @@ -2630,6 +2632,12 @@ class YoutubeDL: continue yield t + thumbnail_id = self.params.get('thumbnail_id') + if thumbnail_id and thumbnail_id not in [t.get('id') for t in thumbnails]: + self.raise_no_formats(info_dict, msg=( + 'Invalid thumbnail ID specified. ' + 'Use --list-thumbnails to see available IDs')) + self._sort_thumbnails(thumbnails) for i, t in enumerate(thumbnails): if t.get('id') is None: diff --git a/yt_dlp/__init__.py b/yt_dlp/__init__.py index 20111175b1..20e9e8b78b 100644 --- a/yt_dlp/__init__.py +++ b/yt_dlp/__init__.py @@ -870,6 +870,7 @@ def parse_options(argv=None): 'clean_infojson': opts.clean_infojson, 'getcomments': opts.getcomments, 'writethumbnail': opts.writethumbnail is True, + 'thumbnail_id': opts.thumbnail_id, 'write_all_thumbnails': opts.writethumbnail == 'all', 'writelink': opts.writelink, 'writeurllink': opts.writeurllink, diff --git a/yt_dlp/options.py b/yt_dlp/options.py index 930d9d4bef..34c0b8571a 100644 --- a/yt_dlp/options.py +++ b/yt_dlp/options.py @@ -1524,6 +1524,10 @@ def create_parser(): '--no-write-thumbnail', action='store_false', dest='writethumbnail', help='Do not write thumbnail image to disk (default)') + thumbnail.add_option( + '--thumbnail-id', + metavar='ID', dest='thumbnail_id', + help='ID of thumbnail to write to disk') thumbnail.add_option( '--write-all-thumbnails', action='store_const', dest='writethumbnail', const='all',