aboutsummaryrefslogtreecommitdiffstats
path: root/yt_dlp/compat/imghdr.py
diff options
context:
space:
mode:
authorpukkandan <pukkandan.ytdlp@gmail.com>2022-05-17 19:39:28 +0530
committerpukkandan <pukkandan.ytdlp@gmail.com>2022-05-17 19:46:01 +0530
commit5792c950bfd9f8b6730659b3046b41c1aea64c98 (patch)
tree73e86196fef2070260eefc43ed053741af13a9f4 /yt_dlp/compat/imghdr.py
parent7a96d0b39c34c6c8c42cc6aaac90dd8d0f5a51d7 (diff)
downloadhypervideo-pre-5792c950bfd9f8b6730659b3046b41c1aea64c98.tar.lz
hypervideo-pre-5792c950bfd9f8b6730659b3046b41c1aea64c98.tar.xz
hypervideo-pre-5792c950bfd9f8b6730659b3046b41c1aea64c98.zip
[compat] Implement `compat.imghdr`
Python 3.11 deprecates `imghdr` module
Diffstat (limited to 'yt_dlp/compat/imghdr.py')
-rw-r--r--yt_dlp/compat/imghdr.py14
1 files changed, 14 insertions, 0 deletions
diff --git a/yt_dlp/compat/imghdr.py b/yt_dlp/compat/imghdr.py
new file mode 100644
index 000000000..734b0d876
--- /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)