mirror of
https://github.com/yt-dlp/yt-dlp.git
synced 2025-10-04 20:54:50 +00:00
[ThumbnailsConvertor] Support conversion to png
and make it the default (#333)
PNG, being a lossless format, should be a better default here compared to JPG since we won't be compressing to a lossy format and losing some of the original image data PNG is also supported for embedding in all the formats similar to JPEG Authored by: louie-github
This commit is contained in:
@@ -77,11 +77,14 @@ class EmbedThumbnailPP(FFmpegPostProcessor):
|
||||
|
||||
original_thumbnail = thumbnail_filename = info['thumbnails'][-1]['filepath']
|
||||
|
||||
# Convert unsupported thumbnail formats to JPEG (see #25687, #25717)
|
||||
# Convert unsupported thumbnail formats to PNG (see #25687, #25717)
|
||||
# Original behavior was to convert to JPG, but since JPG is a lossy
|
||||
# format, there will be some additional data loss.
|
||||
# PNG, on the other hand, is lossless.
|
||||
thumbnail_ext = os.path.splitext(thumbnail_filename)[1][1:]
|
||||
if thumbnail_ext not in ('jpg', 'png'):
|
||||
thumbnail_filename = convertor.convert_thumbnail(thumbnail_filename, 'jpg')
|
||||
thumbnail_ext = 'jpg'
|
||||
thumbnail_filename = convertor.convert_thumbnail(thumbnail_filename, 'png')
|
||||
thumbnail_ext = 'png'
|
||||
|
||||
mtime = os.stat(encodeFilename(filename)).st_mtime
|
||||
|
||||
|
@@ -849,24 +849,30 @@ class FFmpegThumbnailsConvertorPP(FFmpegPostProcessor):
|
||||
info['__files_to_move'].pop(thumbnail_filename), 'webp')
|
||||
|
||||
def convert_thumbnail(self, thumbnail_filename, ext):
|
||||
if ext != 'jpg':
|
||||
raise FFmpegPostProcessorError('Only conversion to jpg is currently supported')
|
||||
if ext == 'jpg':
|
||||
format_name = 'JPEG'
|
||||
opts = ['-bsf:v', 'mjpeg2jpeg']
|
||||
elif ext == 'png':
|
||||
format_name = 'PNG'
|
||||
opts = []
|
||||
else:
|
||||
raise FFmpegPostProcessorError('Only conversion to either jpg or png is currently supported')
|
||||
# NB: % is supposed to be escaped with %% but this does not work
|
||||
# for input files so working around with standard substitution
|
||||
escaped_thumbnail_filename = thumbnail_filename.replace('%', '#')
|
||||
os.rename(encodeFilename(thumbnail_filename), encodeFilename(escaped_thumbnail_filename))
|
||||
escaped_thumbnail_jpg_filename = replace_extension(escaped_thumbnail_filename, 'jpg')
|
||||
self.to_screen('Converting thumbnail "%s" to JPEG' % escaped_thumbnail_filename)
|
||||
self.run_ffmpeg(escaped_thumbnail_filename, escaped_thumbnail_jpg_filename, ['-bsf:v', 'mjpeg2jpeg'])
|
||||
thumbnail_jpg_filename = replace_extension(thumbnail_filename, 'jpg')
|
||||
escaped_thumbnail_conv_filename = replace_extension(escaped_thumbnail_filename, ext)
|
||||
self.to_screen('Converting thumbnail "%s" to %s' % (escaped_thumbnail_filename, format_name))
|
||||
self.run_ffmpeg(escaped_thumbnail_filename, escaped_thumbnail_conv_filename, opts)
|
||||
thumbnail_conv_filename = replace_extension(thumbnail_filename, ext)
|
||||
# Rename back to unescaped
|
||||
os.rename(encodeFilename(escaped_thumbnail_filename), encodeFilename(thumbnail_filename))
|
||||
os.rename(encodeFilename(escaped_thumbnail_jpg_filename), encodeFilename(thumbnail_jpg_filename))
|
||||
return thumbnail_jpg_filename
|
||||
os.rename(encodeFilename(escaped_thumbnail_conv_filename), encodeFilename(thumbnail_conv_filename))
|
||||
return thumbnail_conv_filename
|
||||
|
||||
def run(self, info):
|
||||
if self.format != 'jpg':
|
||||
raise FFmpegPostProcessorError('Only conversion to jpg is currently supported')
|
||||
if self.format not in ('jpg', 'png'):
|
||||
raise FFmpegPostProcessorError('Only conversion to either jpg or png is currently supported')
|
||||
files_to_delete = []
|
||||
has_thumbnail = False
|
||||
|
||||
|
Reference in New Issue
Block a user