From 617d9fa80f810dc4bd97a6ab50781e557e53a182 Mon Sep 17 00:00:00 2001 From: Joaquim Monteiro Date: Thu, 17 Jul 2025 08:03:51 +0100 Subject: [PATCH] [pp/FFmpegThumbnailsConvertor] Correct the extension of all thumbnails --- yt_dlp/postprocessor/embedthumbnail.py | 2 +- yt_dlp/postprocessor/ffmpeg.py | 24 ++++++++++++++---------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/yt_dlp/postprocessor/embedthumbnail.py b/yt_dlp/postprocessor/embedthumbnail.py index d8ba220cab..aacc46d5ac 100644 --- a/yt_dlp/postprocessor/embedthumbnail.py +++ b/yt_dlp/postprocessor/embedthumbnail.py @@ -73,7 +73,7 @@ class EmbedThumbnailPP(FFmpegPostProcessor): # Correct extension for WebP file with wrong extension (see #25687, #25717) convertor = FFmpegThumbnailsConvertorPP(self._downloader) - convertor.fixup_webp(info, idx) + convertor.fixup_thumbnail_ext(info, idx) original_thumbnail = thumbnail_filename = info['thumbnails'][idx]['filepath'] diff --git a/yt_dlp/postprocessor/ffmpeg.py b/yt_dlp/postprocessor/ffmpeg.py index 59a49aa578..c75b18f729 100644 --- a/yt_dlp/postprocessor/ffmpeg.py +++ b/yt_dlp/postprocessor/ffmpeg.py @@ -1081,17 +1081,21 @@ class FFmpegThumbnailsConvertorPP(FFmpegPostProcessor): deprecation_warning(f'{cls.__module__}.{cls.__name__}.is_webp is deprecated') return imghdr.what(path) == 'webp' - def fixup_webp(self, info, idx=-1): + def fixup_thumbnail_ext(self, info, idx=-1): thumbnail_filename = info['thumbnails'][idx]['filepath'] + actual_format = imghdr.what(thumbnail_filename) + if not actual_format: + return + _, thumbnail_ext = os.path.splitext(thumbnail_filename) - if thumbnail_ext: - if thumbnail_ext.lower() != '.webp' and imghdr.what(thumbnail_filename) == 'webp': - self.to_screen(f'Correcting thumbnail "{thumbnail_filename}" extension to webp') - webp_filename = replace_extension(thumbnail_filename, 'webp') - 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), 'webp') + thumbnail_ext = thumbnail_ext.removeprefix('.').lower() + if thumbnail_ext != actual_format and not (actual_format == 'jpeg' and thumbnail_ext == 'jpg'): + self.to_screen(f'Correcting thumbnail "{thumbnail_filename}" extension to {actual_format}') + correct_filename = replace_extension(thumbnail_filename, actual_format) + os.replace(thumbnail_filename, correct_filename) + info['thumbnails'][idx]['filepath'] = correct_filename + info['__files_to_move'][correct_filename] = replace_extension( + info['__files_to_move'].pop(thumbnail_filename), actual_format) @staticmethod def _options(target_ext): @@ -1118,7 +1122,7 @@ class FFmpegThumbnailsConvertorPP(FFmpegPostProcessor): if not original_thumbnail: continue has_thumbnail = True - self.fixup_webp(info, idx) + self.fixup_thumbnail_ext(info, idx) original_thumbnail = thumbnail_dict['filepath'] # Path can change during fixup thumbnail_ext = os.path.splitext(original_thumbnail)[1][1:].lower() if thumbnail_ext == 'jpeg':