1
0
Fork 0
mirror of https://github.com/yt-dlp/yt-dlp.git synced 2025-03-09 12:50:23 -05:00
This commit is contained in:
gavin 2025-03-09 04:30:18 +05:30 committed by GitHub
commit fdf3d2b219
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 27 additions and 3 deletions

View file

@ -304,6 +304,7 @@ class YoutubeDL:
unless writeinfojson is also given unless writeinfojson is also given
writeannotations: Write the video annotations to a .annotations.xml file writeannotations: Write the video annotations to a .annotations.xml file
writethumbnail: Write the thumbnail image to a file writethumbnail: Write the thumbnail image to a file
thumbnail_format: Format code of thumbnail to write
allow_playlist_files: Whether to write playlists' description, infojson etc allow_playlist_files: Whether to write playlists' description, infojson etc
also to disk when using the 'write*' options also to disk when using the 'write*' options
write_all_thumbnails: Write all thumbnail formats to files write_all_thumbnails: Write all thumbnail formats to files
@ -4385,9 +4386,24 @@ def _write_subtitles(self, info_dict, filename):
def _write_thumbnails(self, label, info_dict, filename, thumb_filename_base=None): def _write_thumbnails(self, label, info_dict, filename, thumb_filename_base=None):
""" Write thumbnails to file and return list of (thumb_filename, final_thumb_filename); or None if error """ """ Write thumbnails to file and return list of (thumb_filename, final_thumb_filename); or None if error """
write_all = self.params.get('write_all_thumbnails', False) write_all = self.params.get('write_all_thumbnails', False)
write_any = write_all or self.params.get('writethumbnail', False)
thumbnails, ret = [], [] thumbnails, ret = [], []
if write_all or self.params.get('writethumbnail', False):
thumbnails = info_dict.get('thumbnails') or [] if write_any:
all_thumbnails = info_dict.get('thumbnails') or []
thumbnail_id = self.params.get('thumbnail_format')
if thumbnail_id and not write_all:
for t in all_thumbnails:
if t.get('id') == thumbnail_id:
thumbnails.append(t)
break
else:
self.raise_no_formats(
info_dict, msg=('Invalid thumbnail ID specified. Use --list-thumbnails to see available IDs'),
)
else:
thumbnails = all_thumbnails
if not thumbnails: if not thumbnails:
self.to_screen(f'[info] There are no {label} thumbnails to download') self.to_screen(f'[info] There are no {label} thumbnails to download')
return ret return ret

View file

@ -877,6 +877,7 @@ def parse_options(argv=None):
'clean_infojson': opts.clean_infojson, 'clean_infojson': opts.clean_infojson,
'getcomments': opts.getcomments, 'getcomments': opts.getcomments,
'writethumbnail': opts.writethumbnail is True, 'writethumbnail': opts.writethumbnail is True,
'thumbnail_format': opts.thumbnail_format,
'write_all_thumbnails': opts.writethumbnail == 'all', 'write_all_thumbnails': opts.writethumbnail == 'all',
'writelink': opts.writelink, 'writelink': opts.writelink,
'writeurllink': opts.writeurllink, 'writeurllink': opts.writeurllink,

View file

@ -1533,6 +1533,10 @@ def _alias_callback(option, opt_str, value, parser, opts, nargs):
'--no-write-thumbnail', '--no-write-thumbnail',
action='store_false', dest='writethumbnail', action='store_false', dest='writethumbnail',
help='Do not write thumbnail image to disk (default)') help='Do not write thumbnail image to disk (default)')
thumbnail.add_option(
'--thumbnail-format',
metavar='format', dest='thumbnail_format',
help='Format code of thumbnail to write to disk')
thumbnail.add_option( thumbnail.add_option(
'--write-all-thumbnails', '--write-all-thumbnails',
action='store_const', dest='writethumbnail', const='all', action='store_const', dest='writethumbnail', const='all',

View file

@ -62,6 +62,9 @@ def run(self, info):
self.to_screen('There aren\'t any thumbnails to embed') self.to_screen('There aren\'t any thumbnails to embed')
return [], info return [], info
if self._downloader and (fmt := self._downloader.params.get('thumbnail_format')):
idx = next((i for i, t in enumerate(info['thumbnails']) if t.get('id') == fmt and t.get('filepath')), None)
else:
idx = next((-i for i, t in enumerate(info['thumbnails'][::-1], 1) if t.get('filepath')), None) idx = next((-i for i, t in enumerate(info['thumbnails'][::-1], 1) if t.get('filepath')), None)
if idx is None: if idx is None:
self.to_screen('There are no thumbnails on disk') self.to_screen('There are no thumbnails on disk')