From e3d1f76f0e83da02aed0e5e0ab64b6c344a7dd45 Mon Sep 17 00:00:00 2001 From: Andrea Lazzaretti Date: Tue, 13 Apr 2021 00:19:29 +0200 Subject: [PATCH 1/5] 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, From 437f1dc1595a7a0bbb89e94cf26a0ffd4d55c232 Mon Sep 17 00:00:00 2001 From: Andrea Lazzaretti Date: Tue, 13 Apr 2021 00:33:56 +0200 Subject: [PATCH 2/5] Trying to add a thumbnail-format feature --- youtube_dl/YoutubeDL.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/youtube_dl/YoutubeDL.py b/youtube_dl/YoutubeDL.py index eb591f6bc..8a7521f8b 100755 --- a/youtube_dl/YoutubeDL.py +++ b/youtube_dl/YoutubeDL.py @@ -2426,17 +2426,11 @@ 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') - thumbnailformat = try_and_return_int(self.params.get('thumbnailformat', False)) + thumbnailformat = self.params.get('thumbnailformat', False) if thumbnailformat: - if thumbnailformat in range(len(thumbnails)): - thumbnails = [thumbnails[thumbnailformat]] + if thumbnailformat in [i.get('id') for i in thumbnails]: + thumbnails = [i for i in thumbnails if i.get('id')==thumbnailformat] else: self.report_warning( 'Thumbnail ID unavailable, falling back to default.' From f4aa8c13b549069d14bf6139dcc831e6cd9125c4 Mon Sep 17 00:00:00 2001 From: Andrea Lazzaretti Date: Tue, 13 Apr 2021 13:36:49 +0200 Subject: [PATCH 3/5] Corrected a small error --- youtube_dl/YoutubeDL.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/youtube_dl/YoutubeDL.py b/youtube_dl/YoutubeDL.py index 8a7521f8b..0ba40684c 100755 --- a/youtube_dl/YoutubeDL.py +++ b/youtube_dl/YoutubeDL.py @@ -2428,17 +2428,18 @@ class YoutubeDL(object): if self.params.get('writethumbnail', False): thumbnails = info_dict.get('thumbnails') thumbnailformat = self.params.get('thumbnailformat', False) - if thumbnailformat: - if thumbnailformat in [i.get('id') for i in thumbnails]: - thumbnails = [i for i in thumbnails if i.get('id')==thumbnailformat] + if thumbnails: + if thumbnailformat: + if thumbnailformat in [i.get('id') for i in thumbnails]: + thumbnails = [i for i in thumbnails if i.get('id')==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: - 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') else: From f582c490195f5062a536f6261a8819fc351c3c59 Mon Sep 17 00:00:00 2001 From: lazzand <35269296+lazzand@users.noreply.github.com> Date: Tue, 13 Apr 2021 14:00:26 +0200 Subject: [PATCH 4/5] Added code corrections to satisfy flake8 reqs :) --- youtube_dl/YoutubeDL.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/youtube_dl/YoutubeDL.py b/youtube_dl/YoutubeDL.py index 0ba40684c..efb747b86 100755 --- a/youtube_dl/YoutubeDL.py +++ b/youtube_dl/YoutubeDL.py @@ -2431,12 +2431,11 @@ class YoutubeDL(object): if thumbnails: if thumbnailformat: if thumbnailformat in [i.get('id') for i in thumbnails]: - thumbnails = [i for i in thumbnails if i.get('id')==thumbnailformat] + thumbnails = [i for i in thumbnails if i.get('id') == thumbnailformat] else: - self.report_warning( - 'Thumbnail ID unavailable, falling back to default.' - ' Check available thumbnail formats with the option --list-thumbnails' - ) + 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]] From eaea25ca0e0b0af8b71da38e4afa256020c958eb Mon Sep 17 00:00:00 2001 From: lazzand <35269296+lazzand@users.noreply.github.com> Date: Tue, 13 Apr 2021 14:00:26 +0200 Subject: [PATCH 5/5] Added code corrections to satisfy flake8 reqs :) --- youtube_dl/YoutubeDL.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/youtube_dl/YoutubeDL.py b/youtube_dl/YoutubeDL.py index 0ba40684c..efb747b86 100755 --- a/youtube_dl/YoutubeDL.py +++ b/youtube_dl/YoutubeDL.py @@ -2431,12 +2431,11 @@ class YoutubeDL(object): if thumbnails: if thumbnailformat: if thumbnailformat in [i.get('id') for i in thumbnails]: - thumbnails = [i for i in thumbnails if i.get('id')==thumbnailformat] + thumbnails = [i for i in thumbnails if i.get('id') == thumbnailformat] else: - self.report_warning( - 'Thumbnail ID unavailable, falling back to default.' - ' Check available thumbnail formats with the option --list-thumbnails' - ) + 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]]