aboutsummaryrefslogtreecommitdiffstats
path: root/yt_dlp/postprocessor/embedthumbnail.py
diff options
context:
space:
mode:
authorpukkandan <pukkandan.ytdlp@gmail.com>2021-04-11 03:48:52 +0530
committerpukkandan <pukkandan.ytdlp@gmail.com>2021-04-12 02:32:29 +0530
commit8fa43c73d83619722c7e30d70247eaa9b7f52810 (patch)
treeb0a277f6de2a8811546e464f4551374c895c567e /yt_dlp/postprocessor/embedthumbnail.py
parent56d868dbb7c72e4fbe9d28d4837cc59261d8fe55 (diff)
downloadhypervideo-pre-8fa43c73d83619722c7e30d70247eaa9b7f52810.tar.lz
hypervideo-pre-8fa43c73d83619722c7e30d70247eaa9b7f52810.tar.xz
hypervideo-pre-8fa43c73d83619722c7e30d70247eaa9b7f52810.zip
Add option `--convert-thumbnails`
Closes: https://github.com/yt-dlp/yt-dlp/issues/99 https://github.com/yt-dlp/yt-dlp/issues/102
Diffstat (limited to 'yt_dlp/postprocessor/embedthumbnail.py')
-rw-r--r--yt_dlp/postprocessor/embedthumbnail.py51
1 files changed, 13 insertions, 38 deletions
diff --git a/yt_dlp/postprocessor/embedthumbnail.py b/yt_dlp/postprocessor/embedthumbnail.py
index 25124161a..3be698bce 100644
--- a/yt_dlp/postprocessor/embedthumbnail.py
+++ b/yt_dlp/postprocessor/embedthumbnail.py
@@ -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: