extract mime from url (YouTube)
This commit is contained in:
parent
fcae9bb0ce
commit
c40d305a1a
@ -3,6 +3,7 @@ import streamlink
|
|||||||
import requests
|
import requests
|
||||||
import asyncio
|
import asyncio
|
||||||
import html.parser
|
import html.parser
|
||||||
|
import urllib.parse
|
||||||
import expiringdict
|
import expiringdict
|
||||||
import json
|
import json
|
||||||
|
|
||||||
@ -102,6 +103,21 @@ class StreamProvider():
|
|||||||
proxy = str(proxy)
|
proxy = str(proxy)
|
||||||
if len(proxy) > 5:
|
if len(proxy) > 5:
|
||||||
self.proxy = proxy
|
self.proxy = proxy
|
||||||
|
def extract_mime(self, upstream):
|
||||||
|
try:
|
||||||
|
url = urllib.parse.urlparse(upstream)
|
||||||
|
query = urllib.parse.parse_qs(url.query.lower())
|
||||||
|
except Exception as e:
|
||||||
|
self.logger.info(e)
|
||||||
|
else:
|
||||||
|
mime_types = query.get("mime")
|
||||||
|
if isinstance(mime_types, list):
|
||||||
|
for mime in mime_types:
|
||||||
|
if isinstance(mime, str) and not mime.startswith("text"):
|
||||||
|
if "mpegurl" in mime:
|
||||||
|
return "application/vnd.apple.mpegurl"
|
||||||
|
return mime
|
||||||
|
return None
|
||||||
def init_stream(self):
|
def init_stream(self):
|
||||||
stream = {}
|
stream = {}
|
||||||
stream["upstream"] = None
|
stream["upstream"] = None
|
||||||
@ -113,8 +129,6 @@ class StreamProvider():
|
|||||||
return stream
|
return stream
|
||||||
def process(self):
|
def process(self):
|
||||||
data = self.stream()
|
data = self.stream()
|
||||||
if not isinstance(data, StreamData):
|
|
||||||
return self.init_stream()
|
|
||||||
if not isinstance(data.upstream(), str) or isinstance(data.ctype(), str):
|
if not isinstance(data.upstream(), str) or isinstance(data.ctype(), str):
|
||||||
return data
|
return data
|
||||||
proxies = None
|
proxies = None
|
||||||
@ -160,7 +174,7 @@ class StreamlinkRunner(StreamProvider):
|
|||||||
for key in reversed(streams):
|
for key in reversed(streams):
|
||||||
stream = streams.get(key)
|
stream = streams.get(key)
|
||||||
if hasattr(stream, "url"):
|
if hasattr(stream, "url"):
|
||||||
return StreamData(stream.url, None, None, None, None, False)
|
return StreamData(stream.url, self.extract_mime(stream.url), None, None, None, False)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.info("%s <%s>", e, self.upstream)
|
self.logger.info("%s <%s>", e, self.upstream)
|
||||||
return StreamData(None, None, None, None, None, False)
|
return StreamData(None, None, None, None, None, False)
|
||||||
@ -203,6 +217,7 @@ class YoutubeRunner(StreamProvider):
|
|||||||
best_format = vformat
|
best_format = vformat
|
||||||
best_stream["override"] = True
|
best_stream["override"] = True
|
||||||
best_stream["upstream"] = new_url
|
best_stream["upstream"] = new_url
|
||||||
|
best_stream["ctype"] = self.extract_mime(new_url)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.info("%s <%s>", e, self.upstream)
|
self.logger.info("%s <%s>", e, self.upstream)
|
||||||
return StreamData(**best_stream)
|
return StreamData(**best_stream)
|
||||||
|
Loading…
Reference in New Issue
Block a user