mirror of
https://github.com/yt-dlp/yt-dlp
synced 2025-07-10 07:44:01 -05:00
[extractor/wistia] Improve extension detection (#5415)
Closes #5053 Authored by: bashonly, Grub4k, pukkandan
This commit is contained in:
122
yt_dlp/utils.py
122
yt_dlp/utils.py
@ -3480,67 +3480,93 @@ def error_to_str(err):
|
||||
return f'{type(err).__name__}: {err}'
|
||||
|
||||
|
||||
def mimetype2ext(mt):
|
||||
if mt is None:
|
||||
def mimetype2ext(mt, default=NO_DEFAULT):
|
||||
if not isinstance(mt, str):
|
||||
if default is not NO_DEFAULT:
|
||||
return default
|
||||
return None
|
||||
|
||||
mt, _, params = mt.partition(';')
|
||||
mt = mt.strip()
|
||||
|
||||
FULL_MAP = {
|
||||
'audio/mp4': 'm4a',
|
||||
# Per RFC 3003, audio/mpeg can be .mp1, .mp2 or .mp3. Here use .mp3 as
|
||||
# it's the most popular one
|
||||
'audio/mpeg': 'mp3',
|
||||
'audio/x-wav': 'wav',
|
||||
'audio/wav': 'wav',
|
||||
'audio/wave': 'wav',
|
||||
}
|
||||
|
||||
ext = FULL_MAP.get(mt)
|
||||
if ext is not None:
|
||||
return ext
|
||||
|
||||
SUBTYPE_MAP = {
|
||||
MAP = {
|
||||
# video
|
||||
'3gpp': '3gp',
|
||||
'smptett+xml': 'tt',
|
||||
'ttaf+xml': 'dfxp',
|
||||
'ttml+xml': 'ttml',
|
||||
'x-flv': 'flv',
|
||||
'x-mp4-fragmented': 'mp4',
|
||||
'x-ms-sami': 'sami',
|
||||
'x-ms-wmv': 'wmv',
|
||||
'mp2t': 'ts',
|
||||
'mp4': 'mp4',
|
||||
'mpeg': 'mpeg',
|
||||
'mpegurl': 'm3u8',
|
||||
'x-mpegurl': 'm3u8',
|
||||
'vnd.apple.mpegurl': 'm3u8',
|
||||
'quicktime': 'mov',
|
||||
'webm': 'webm',
|
||||
'vp9': 'vp9',
|
||||
'x-flv': 'flv',
|
||||
'x-m4v': 'm4v',
|
||||
'x-matroska': 'mkv',
|
||||
'x-mng': 'mng',
|
||||
'x-mp4-fragmented': 'mp4',
|
||||
'x-ms-asf': 'asf',
|
||||
'x-ms-wmv': 'wmv',
|
||||
'x-msvideo': 'avi',
|
||||
|
||||
# application (streaming playlists)
|
||||
'dash+xml': 'mpd',
|
||||
'f4m+xml': 'f4m',
|
||||
'hds+xml': 'f4m',
|
||||
'vnd.apple.mpegurl': 'm3u8',
|
||||
'vnd.ms-sstr+xml': 'ism',
|
||||
'quicktime': 'mov',
|
||||
'mp2t': 'ts',
|
||||
'x-mpegurl': 'm3u8',
|
||||
|
||||
# audio
|
||||
'audio/mp4': 'm4a',
|
||||
# Per RFC 3003, audio/mpeg can be .mp1, .mp2 or .mp3.
|
||||
# Using .mp3 as it's the most popular one
|
||||
'audio/mpeg': 'mp3',
|
||||
'audio/webm': 'weba',
|
||||
'audio/x-matroska': 'mka',
|
||||
'audio/x-mpegurl': 'm3u',
|
||||
'midi': 'mid',
|
||||
'ogg': 'ogg',
|
||||
'wav': 'wav',
|
||||
'wave': 'wav',
|
||||
'x-aac': 'aac',
|
||||
'x-flac': 'flac',
|
||||
'x-m4a': 'm4a',
|
||||
'x-realaudio': 'ra',
|
||||
'x-wav': 'wav',
|
||||
'filmstrip+json': 'fs',
|
||||
|
||||
# image
|
||||
'avif': 'avif',
|
||||
'bmp': 'bmp',
|
||||
'gif': 'gif',
|
||||
'jpeg': 'jpg',
|
||||
'png': 'png',
|
||||
'svg+xml': 'svg',
|
||||
}
|
||||
'tiff': 'tif',
|
||||
'vnd.wap.wbmp': 'wbmp',
|
||||
'webp': 'webp',
|
||||
'x-icon': 'ico',
|
||||
'x-jng': 'jng',
|
||||
'x-ms-bmp': 'bmp',
|
||||
|
||||
_, _, subtype = mt.rpartition('/')
|
||||
ext = SUBTYPE_MAP.get(subtype.lower())
|
||||
if ext is not None:
|
||||
return ext
|
||||
# caption
|
||||
'filmstrip+json': 'fs',
|
||||
'smptett+xml': 'tt',
|
||||
'ttaf+xml': 'dfxp',
|
||||
'ttml+xml': 'ttml',
|
||||
'x-ms-sami': 'sami',
|
||||
|
||||
SUFFIX_MAP = {
|
||||
# misc
|
||||
'gzip': 'gz',
|
||||
'json': 'json',
|
||||
'xml': 'xml',
|
||||
'zip': 'zip',
|
||||
'gzip': 'gz',
|
||||
}
|
||||
|
||||
_, _, suffix = subtype.partition('+')
|
||||
ext = SUFFIX_MAP.get(suffix)
|
||||
if ext is not None:
|
||||
return ext
|
||||
mimetype = mt.partition(';')[0].strip().lower()
|
||||
_, _, subtype = mimetype.rpartition('/')
|
||||
|
||||
ext = traverse_obj(MAP, mimetype, subtype, subtype.rsplit('+')[-1])
|
||||
if ext:
|
||||
return ext
|
||||
elif default is not NO_DEFAULT:
|
||||
return default
|
||||
return subtype.replace('+', '.')
|
||||
|
||||
|
||||
@ -3634,7 +3660,7 @@ def get_compatible_ext(*, vcodecs, acodecs, vexts, aexts, preferences=None):
|
||||
return 'mkv' if allow_mkv else preferences[-1]
|
||||
|
||||
|
||||
def urlhandle_detect_ext(url_handle):
|
||||
def urlhandle_detect_ext(url_handle, default=NO_DEFAULT):
|
||||
getheader = url_handle.headers.get
|
||||
|
||||
cd = getheader('Content-Disposition')
|
||||
@ -3645,7 +3671,13 @@ def urlhandle_detect_ext(url_handle):
|
||||
if e:
|
||||
return e
|
||||
|
||||
return mimetype2ext(getheader('Content-Type'))
|
||||
meta_ext = getheader('x-amz-meta-name')
|
||||
if meta_ext:
|
||||
e = meta_ext.rpartition('.')[2]
|
||||
if e:
|
||||
return e
|
||||
|
||||
return mimetype2ext(getheader('Content-Type'), default=default)
|
||||
|
||||
|
||||
def encode_data_uri(data, mime_type):
|
||||
|
Reference in New Issue
Block a user