A feature-rich command-line audio/video downloader
[compat] Implement `compat.imghdr`
pukkandan5792c950bfd9
diff --git a/yt_dlp/YoutubeDL.py b/yt_dlp/YoutubeDL.pyindex c9de2437d8..d1094a01b6 100644--- a/yt_dlp/YoutubeDL.py+++ b/yt_dlp/YoutubeDL.py@@ -806,7 +806,7 @@ class YoutubeDL:if self.params.get('logger'):self.params['logger'].error(message)else:- self._write_string(f'{self._bidi_workaround(message)}\n' , self._out_files.error, only_once=only_once)+ self._write_string(f'{self._bidi_workaround(message)}\n', self._out_files.error, only_once=only_once)def _send_console_code(self, code):if compat_os_name == 'nt' or not self._out_files.console:diff --git a/yt_dlp/compat/imghdr.py b/yt_dlp/compat/imghdr.pynew file mode 100644index 0000000000..734b0d876c--- /dev/null+++ b/yt_dlp/compat/imghdr.py@@ -0,0 +1,14 @@+tests = {+ 'webp': lambda h: h[0:4] == b'RIFF' and h[8:] == b'WEBP',+ 'png': lambda h: h[:8] == b'\211PNG\r\n\032\n',+ 'jpeg': lambda h: h[6:10] in (b'JFIF', b'Exif'),+}+++def what(path):+ """Detect format of image (Currently supports jpeg, png, webp only)+ Ref: https://github.com/python/cpython/blob/3.10/Lib/imghdr.py+ """+ with open(path, 'rb') as f:+ head = f.read(12)+ return next((type_ for type_, test in tests.items() if test(head)), None)diff --git a/yt_dlp/postprocessor/embedthumbnail.py b/yt_dlp/postprocessor/embedthumbnail.pyindex d36e0008e8..e031d344f8 100644--- a/yt_dlp/postprocessor/embedthumbnail.py+++ b/yt_dlp/postprocessor/embedthumbnail.py@@ -1,11 +1,11 @@import base64-import imghdrimport osimport reimport subprocessfrom .common import PostProcessorfrom .ffmpeg import FFmpegPostProcessor, FFmpegThumbnailsConvertorPP+from ..compat import imghdrfrom ..dependencies import mutagenfrom ..utils import (Popen,diff --git a/yt_dlp/postprocessor/ffmpeg.py b/yt_dlp/postprocessor/ffmpeg.pyindex d1d8e1687c..09eb33b8d1 100644--- a/yt_dlp/postprocessor/ffmpeg.py+++ b/yt_dlp/postprocessor/ffmpeg.py@@ -7,7 +7,7 @@ import subprocessimport timefrom .common import AudioConversionError, PostProcessor-from ..compat import compat_str+from ..compat import imghdrfrom ..utils import (ISO639Utils,Popen,@@ -27,6 +27,7 @@ from ..utils import (traverse_obj,variadic,write_json_file,+ write_string,)EXT_TO_OUT_FORMATS = {@@ -1030,8 +1031,8 @@ class FFmpegSplitChaptersPP(FFmpegPostProcessor):self.to_screen('Chapter %03d; Destination: %s' % (number, destination))return (destination,- ['-ss', compat_str(chapter['start_time']),- '-t', compat_str(chapter['end_time'] - chapter['start_time'])])+ ['-ss', str(chapter['start_time']),+ '-t', str(chapter['end_time'] - chapter['start_time'])])@PostProcessor._restrict_to(images=False)def run(self, info):@@ -1059,18 +1060,16 @@ class FFmpegThumbnailsConvertorPP(FFmpegPostProcessor):super().__init__(downloader)self.format = format- @staticmethod- 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'+ @classmethod+ def is_webp(cls, path):+ write_string(f'DeprecationWarning: {cls.__module__}.{cls.__name__}.is_webp is deprecated')+ return imghdr.what(path) == 'webp'def fixup_webp(self, info, idx=-1):thumbnail_filename = info['thumbnails'][idx]['filepath']_, thumbnail_ext = os.path.splitext(thumbnail_filename)if thumbnail_ext:- thumbnail_ext = thumbnail_ext[1:].lower()- if thumbnail_ext != 'webp' and self.is_webp(thumbnail_filename):+ if thumbnail_ext.lower() != '.webp' and imghdr.what(thumbnail_filename) == 'webp':self.to_screen('Correcting thumbnail "%s" extension to webp' % thumbnail_filename)webp_filename = replace_extension(thumbnail_filename, 'webp')os.replace(thumbnail_filename, webp_filename)