|
|
|
@ -13,8 +13,10 @@ try:
|
|
|
|
|
except ImportError:
|
|
|
|
|
has_mutagen = False
|
|
|
|
|
|
|
|
|
|
from .ffmpeg import FFmpegPostProcessor
|
|
|
|
|
|
|
|
|
|
from .ffmpeg import (
|
|
|
|
|
FFmpegPostProcessor,
|
|
|
|
|
FFmpegThumbnailsConvertorPP,
|
|
|
|
|
)
|
|
|
|
|
from ..utils import (
|
|
|
|
|
check_executable,
|
|
|
|
|
encodeArgument,
|
|
|
|
@ -23,7 +25,6 @@ from ..utils import (
|
|
|
|
|
PostProcessingError,
|
|
|
|
|
prepend_extension,
|
|
|
|
|
process_communicate_or_kill,
|
|
|
|
|
replace_extension,
|
|
|
|
|
shell_quote,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
@ -35,7 +36,7 @@ class EmbedThumbnailPPError(PostProcessingError):
|
|
|
|
|
class EmbedThumbnailPP(FFmpegPostProcessor):
|
|
|
|
|
|
|
|
|
|
def __init__(self, downloader=None, already_have_thumbnail=False):
|
|
|
|
|
super(EmbedThumbnailPP, self).__init__(downloader)
|
|
|
|
|
FFmpegPostProcessor.__init__(self, downloader)
|
|
|
|
|
self._already_have_thumbnail = already_have_thumbnail
|
|
|
|
|
|
|
|
|
|
def run(self, info):
|
|
|
|
@ -46,44 +47,21 @@ class EmbedThumbnailPP(FFmpegPostProcessor):
|
|
|
|
|
self.to_screen('There aren\'t any thumbnails to embed')
|
|
|
|
|
return [], info
|
|
|
|
|
|
|
|
|
|
initial_thumbnail = original_thumbnail = thumbnail_filename = info['thumbnails'][-1]['filepath']
|
|
|
|
|
|
|
|
|
|
thumbnail_filename = info['thumbnails'][-1]['filepath']
|
|
|
|
|
if not os.path.exists(encodeFilename(thumbnail_filename)):
|
|
|
|
|
self.report_warning('Skipping embedding the thumbnail because the file is missing.')
|
|
|
|
|
return [], info
|
|
|
|
|
|
|
|
|
|
def is_webp(path):
|
|
|
|
|
with open(encodeFilename(path), 'rb') as f:
|
|
|
|
|
b = f.read(12)
|
|
|
|
|
return b[0:4] == b'RIFF' and b[8:] == b'WEBP'
|
|
|
|
|
|
|
|
|
|
# Correct extension for WebP file with wrong extension (see #25687, #25717)
|
|
|
|
|
_, thumbnail_ext = os.path.splitext(thumbnail_filename)
|
|
|
|
|
if thumbnail_ext:
|
|
|
|
|
thumbnail_ext = thumbnail_ext[1:].lower()
|
|
|
|
|
if thumbnail_ext != 'webp' and is_webp(thumbnail_filename):
|
|
|
|
|
self.to_screen('Correcting extension to webp and escaping path for thumbnail "%s"' % thumbnail_filename)
|
|
|
|
|
thumbnail_webp_filename = replace_extension(thumbnail_filename, 'webp')
|
|
|
|
|
if os.path.exists(thumbnail_webp_filename):
|
|
|
|
|
os.remove(thumbnail_webp_filename)
|
|
|
|
|
os.rename(encodeFilename(thumbnail_filename), encodeFilename(thumbnail_webp_filename))
|
|
|
|
|
original_thumbnail = thumbnail_filename = thumbnail_webp_filename
|
|
|
|
|
thumbnail_ext = 'webp'
|
|
|
|
|
convertor = FFmpegThumbnailsConvertorPP(self._downloader)
|
|
|
|
|
convertor.fixup_webp(info, -1)
|
|
|
|
|
|
|
|
|
|
original_thumbnail = thumbnail_filename = info['thumbnails'][-1]['filepath']
|
|
|
|
|
|
|
|
|
|
# Convert unsupported thumbnail formats to JPEG (see #25687, #25717)
|
|
|
|
|
if thumbnail_ext not in ['jpg', 'png']:
|
|
|
|
|
# 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')
|
|
|
|
|
# Rename back to unescaped for further processing
|
|
|
|
|
os.rename(encodeFilename(escaped_thumbnail_filename), encodeFilename(thumbnail_filename))
|
|
|
|
|
os.rename(encodeFilename(escaped_thumbnail_jpg_filename), encodeFilename(thumbnail_jpg_filename))
|
|
|
|
|
thumbnail_filename = thumbnail_jpg_filename
|
|
|
|
|
_, thumbnail_ext = os.path.splitext(thumbnail_filename)
|
|
|
|
|
if thumbnail_ext not in ('jpg', 'png'):
|
|
|
|
|
thumbnail_filename = convertor.convert_thumbnail(thumbnail_filename, 'jpg')
|
|
|
|
|
thumbnail_ext = 'jpg'
|
|
|
|
|
|
|
|
|
|
mtime = os.stat(encodeFilename(filename)).st_mtime
|
|
|
|
@ -194,9 +172,6 @@ class EmbedThumbnailPP(FFmpegPostProcessor):
|
|
|
|
|
|
|
|
|
|
files_to_delete = [thumbnail_filename]
|
|
|
|
|
if self._already_have_thumbnail:
|
|
|
|
|
info['__files_to_move'][original_thumbnail] = replace_extension(
|
|
|
|
|
info['__files_to_move'][initial_thumbnail],
|
|
|
|
|
os.path.splitext(original_thumbnail)[1][1:])
|
|
|
|
|
if original_thumbnail == thumbnail_filename:
|
|
|
|
|
files_to_delete = []
|
|
|
|
|
elif original_thumbnail != thumbnail_filename:
|
|
|
|
|