[ie/soundcloud] Various fixes (#11820)
[yt-dlp.git] / yt_dlp / compat / imghdr.py
blob4ae173fdecc92e471c72bdb520019926ba145f44
1 def what(file=None, h=None):
2 """Detect format of image (Currently supports jpeg, png, webp, gif only)
3 Ref: https://github.com/python/cpython/blob/3.11/Lib/imghdr.py
4 Ref: https://www.w3.org/Graphics/JPEG/itu-t81.pdf
5 """
6 if h is None:
7 with open(file, 'rb') as f:
8 h = f.read(12)
10 if h.startswith(b'RIFF') and h.startswith(b'WEBP', 8):
11 return 'webp'
13 if h.startswith(b'\x89PNG'):
14 return 'png'
16 if h.startswith(b'\xFF\xD8\xFF'):
17 return 'jpeg'
19 if h.startswith(b'GIF'):
20 return 'gif'
22 return None