From e3d1f76f0e83da02aed0e5e0ab64b6c344a7dd45 Mon Sep 17 00:00:00 2001 From: Andrea Lazzaretti Date: Tue, 13 Apr 2021 00:19:29 +0200 Subject: [PATCH] Trying to add a thumbnail-format feature --- youtube_dl/YoutubeDL.py | 19 ++++++++++++++++++- youtube_dl/__init__.py | 1 + youtube_dl/options.py | 4 ++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/youtube_dl/YoutubeDL.py b/youtube_dl/YoutubeDL.py index 8f65c6499..eb591f6bc 100755 --- a/youtube_dl/YoutubeDL.py +++ b/youtube_dl/YoutubeDL.py @@ -182,6 +182,7 @@ class YoutubeDL(object): writeannotations: Write the video annotations to a .annotations.xml file writethumbnail: Write the thumbnail image to a file write_all_thumbnails: Write all thumbnail formats to files + thumbnailformat: Thumbnail format ID writesubtitles: Write the video subtitles to a file writeautomaticsub: Write the automatically generated subtitles to a file allsubtitles: Downloads all the subtitles of the video @@ -2425,8 +2426,24 @@ class YoutubeDL(object): def _write_thumbnails(self, info_dict, filename): if self.params.get('writethumbnail', False): + def try_and_return_int(s): + try: + int(s) + return int(s) + except ValueError: + return -1 thumbnails = info_dict.get('thumbnails') - if thumbnails: + thumbnailformat = try_and_return_int(self.params.get('thumbnailformat', False)) + if thumbnailformat: + if thumbnailformat in range(len(thumbnails)): + thumbnails = [thumbnails[thumbnailformat]] + else: + self.report_warning( + 'Thumbnail ID unavailable, falling back to default.' + ' Check available thumbnail formats with the option --list-thumbnails' + ) + thumbnails = [thumbnails[-1]] + else: thumbnails = [thumbnails[-1]] elif self.params.get('write_all_thumbnails', False): thumbnails = info_dict.get('thumbnails') diff --git a/youtube_dl/__init__.py b/youtube_dl/__init__.py index e1bd67919..67518cd79 100644 --- a/youtube_dl/__init__.py +++ b/youtube_dl/__init__.py @@ -371,6 +371,7 @@ def _real_main(argv=None): 'writeannotations': opts.writeannotations, 'writeinfojson': opts.writeinfojson, 'writethumbnail': opts.writethumbnail, + 'thumbnailformat': opts.thumbnailformat, 'write_all_thumbnails': opts.write_all_thumbnails, 'writesubtitles': opts.writesubtitles, 'writeautomaticsub': opts.writeautomaticsub, diff --git a/youtube_dl/options.py b/youtube_dl/options.py index 241cf110f..4b0973dec 100644 --- a/youtube_dl/options.py +++ b/youtube_dl/options.py @@ -773,6 +773,10 @@ def parseOpts(overrideArguments=None): '--write-thumbnail', action='store_true', dest='writethumbnail', default=False, help='Write thumbnail image to disk') + thumbnail.add_option( + '--thumbnail-format', + action='store', dest='thumbnailformat', metavar='ID', default=None, + help='Thumbnail format ID') thumbnail.add_option( '--write-all-thumbnails', action='store_true', dest='write_all_thumbnails', default=False,