[embedthumbnail] add new cli option '--force-convert-thumbnails'

This commit is contained in:
Egor Halimonenko 2025-01-02 17:18:55 +03:00
parent 0b6b7742c2
commit 198c3e4faf
5 changed files with 29 additions and 7 deletions

View file

@ -1006,6 +1006,8 @@ ## Post-Processing Options:
syntax as "--remux-video". Use "--convert- syntax as "--remux-video". Use "--convert-
thumbnails none" to disable conversion thumbnails none" to disable conversion
(default) (default)
--force-convert-thumbnails Convert the thumbnails to another format even
if it's already in this format
--split-chapters Split video into multiple files based on --split-chapters Split video into multiple files based on
internal chapters. The "chapter:" prefix can internal chapters. The "chapter:" prefix can
be used with "--paths" and "--output" to set be used with "--paths" and "--output" to set

View file

@ -638,6 +638,7 @@ def get_postprocessors(opts):
yield { yield {
'key': 'FFmpegThumbnailsConvertor', 'key': 'FFmpegThumbnailsConvertor',
'format': opts.convertthumbnails, 'format': opts.convertthumbnails,
'force_convert_thumbnails': opts.force_convert_thumbnails,
'when': 'before_dl', 'when': 'before_dl',
} }
if opts.extractaudio: if opts.extractaudio:

View file

@ -1748,6 +1748,11 @@ def _alias_callback(option, opt_str, value, parser, opts, nargs):
f'(currently supported: {", ".join(sorted(FFmpegThumbnailsConvertorPP.SUPPORTED_EXTS))}). ' f'(currently supported: {", ".join(sorted(FFmpegThumbnailsConvertorPP.SUPPORTED_EXTS))}). '
'You can specify multiple rules using similar syntax as "--remux-video". ' 'You can specify multiple rules using similar syntax as "--remux-video". '
'Use "--convert-thumbnails none" to disable conversion (default)')) 'Use "--convert-thumbnails none" to disable conversion (default)'))
postproc.add_option(
'--force-convert-thumbnails',
dest='force_convert_thumbnails', action='store_true', default=False,
help=(
'Convert the thumbnails to another format even if it\'s already in this format'))
postproc.add_option( postproc.add_option(
'--split-chapters', '--split-tracks', '--split-chapters', '--split-tracks',
dest='split_chapters', action='store_true', default=False, dest='split_chapters', action='store_true', default=False,

View file

@ -73,7 +73,7 @@ def run(self, info):
# Correct extension for WebP file with wrong extension (see #25687, #25717) # Correct extension for WebP file with wrong extension (see #25687, #25717)
convertor = FFmpegThumbnailsConvertorPP(self._downloader) convertor = FFmpegThumbnailsConvertorPP(self._downloader)
convertor.fixup_webp(info, idx) convertor.fixup_thumbnail(info, idx)
original_thumbnail = thumbnail_filename = info['thumbnails'][idx]['filepath'] original_thumbnail = thumbnail_filename = info['thumbnails'][idx]['filepath']

View file

@ -1072,16 +1072,17 @@ class FFmpegThumbnailsConvertorPP(FFmpegPostProcessor):
SUPPORTED_EXTS = MEDIA_EXTENSIONS.thumbnails SUPPORTED_EXTS = MEDIA_EXTENSIONS.thumbnails
FORMAT_RE = create_mapping_re(SUPPORTED_EXTS) FORMAT_RE = create_mapping_re(SUPPORTED_EXTS)
def __init__(self, downloader=None, format=None): def __init__(self, downloader=None, format=None, force_convert_thumbnails=False):
super().__init__(downloader) super().__init__(downloader)
self.mapping = format self.mapping = format
self._force_convert_thumbnails = force_convert_thumbnails
@classmethod @classmethod
def is_webp(cls, path): def is_webp(cls, path):
deprecation_warning(f'{cls.__module__}.{cls.__name__}.is_webp is deprecated') deprecation_warning(f'{cls.__module__}.{cls.__name__}.is_webp is deprecated')
return imghdr.what(path) == 'webp' return imghdr.what(path) == 'webp'
def fixup_webp(self, info, idx=-1): def fixup_thumbnail(self, info, idx=-1):
thumbnail_filename = info['thumbnails'][idx]['filepath'] thumbnail_filename = info['thumbnails'][idx]['filepath']
_, thumbnail_ext = os.path.splitext(thumbnail_filename) _, thumbnail_ext = os.path.splitext(thumbnail_filename)
if thumbnail_ext: if thumbnail_ext:
@ -1092,6 +1093,13 @@ def fixup_webp(self, info, idx=-1):
info['thumbnails'][idx]['filepath'] = webp_filename info['thumbnails'][idx]['filepath'] = webp_filename
info['__files_to_move'][webp_filename] = replace_extension( info['__files_to_move'][webp_filename] = replace_extension(
info['__files_to_move'].pop(thumbnail_filename), 'webp') info['__files_to_move'].pop(thumbnail_filename), 'webp')
elif thumbnail_ext.lower() != '.png' and imghdr.what(thumbnail_filename) == 'png':
self.to_screen('Correcting thumbnail "%s" extension to png' % thumbnail_filename)
webp_filename = replace_extension(thumbnail_filename, 'png')
os.replace(thumbnail_filename, webp_filename)
info['thumbnails'][idx]['filepath'] = webp_filename
info['__files_to_move'][webp_filename] = replace_extension(
info['__files_to_move'].pop(thumbnail_filename), 'png')
@staticmethod @staticmethod
def _options(target_ext): def _options(target_ext):
@ -1118,17 +1126,23 @@ def run(self, info):
if not original_thumbnail: if not original_thumbnail:
continue continue
has_thumbnail = True has_thumbnail = True
self.fixup_webp(info, idx) self.fixup_thumbnail(info, idx)
original_thumbnail = thumbnail_dict['filepath'] # Path can change during fixup original_thumbnail = thumbnail_dict['filepath'] # Path can change during fixup
thumbnail_ext = os.path.splitext(original_thumbnail)[1][1:].lower() thumbnail_ext = os.path.splitext(original_thumbnail)[1][1:].lower()
if thumbnail_ext == 'jpeg': if thumbnail_ext == 'jpeg':
thumbnail_ext = 'jpg' thumbnail_ext = 'jpg'
target_ext, _skip_msg = resolve_mapping(thumbnail_ext, self.mapping) target_ext, _skip_msg = resolve_mapping(thumbnail_ext, self.mapping)
is_forced_converting = False
if _skip_msg: if _skip_msg:
self.to_screen(f'Not converting thumbnail "{original_thumbnail}"; {_skip_msg}') if self._force_convert_thumbnails and target_ext == thumbnail_ext:
continue self.to_screen(f'Force converting thumbnail "{original_thumbnail}" despite of: {_skip_msg}')
is_forced_converting = True
else:
self.to_screen(f'Not converting thumbnail "{original_thumbnail}"; {_skip_msg}')
continue
thumbnail_dict['filepath'] = self.convert_thumbnail(original_thumbnail, target_ext) thumbnail_dict['filepath'] = self.convert_thumbnail(original_thumbnail, target_ext)
files_to_delete.append(original_thumbnail) if is_forced_converting == False:
files_to_delete.append(original_thumbnail)
info['__files_to_move'][thumbnail_dict['filepath']] = replace_extension( info['__files_to_move'][thumbnail_dict['filepath']] = replace_extension(
info['__files_to_move'][original_thumbnail], target_ext) info['__files_to_move'][original_thumbnail], target_ext)