Add support for data URIs from HLS playlists in ClearKey

This commit is contained in:
rlaphoenix 2023-02-23 17:21:48 +00:00
parent 221cd1c283
commit 45c9ba5198
1 changed files with 24 additions and 14 deletions

View File

@ -1,5 +1,6 @@
from __future__ import annotations from __future__ import annotations
import base64
import shutil import shutil
from pathlib import Path from pathlib import Path
from typing import Optional, Union from typing import Optional, Union
@ -58,8 +59,16 @@ class ClearKey:
if not m3u_key.uri: if not m3u_key.uri:
raise ValueError("No URI in M3U Key, unable to get Key.") raise ValueError("No URI in M3U Key, unable to get Key.")
if m3u_key.uri.startswith("data:"):
media_types, data = m3u_key.uri[5:].split(",")
media_types = media_types.split(";")
if "base64" in media_types:
data = base64.b64decode(data)
key = data
else:
url = urljoin(m3u_key.base_uri, m3u_key.uri)
res = requests.get( res = requests.get(
url=urljoin(m3u_key.base_uri, m3u_key.uri), url=url,
headers={ headers={
"User-Agent": "smartexoplayer/1.1.0 (Linux;Android 8.0.0) ExoPlayerLib/2.13.3" "User-Agent": "smartexoplayer/1.1.0 (Linux;Android 8.0.0) ExoPlayerLib/2.13.3"
}, },
@ -70,11 +79,12 @@ class ClearKey:
raise EOFError("Unexpected Empty Response by M3U Key URI.") raise EOFError("Unexpected Empty Response by M3U Key URI.")
if len(res.content) < 16: if len(res.content) < 16:
raise EOFError(f"Unexpected Length of Key ({len(res.content)} bytes) in M3U Key.") raise EOFError(f"Unexpected Length of Key ({len(res.content)} bytes) in M3U Key.")
key = res.content key = res.content
iv = None
if m3u_key.iv: if m3u_key.iv:
iv = bytes.fromhex(m3u_key.iv.replace("0x", "")) iv = bytes.fromhex(m3u_key.iv.replace("0x", ""))
else:
iv = None
return cls(key=key, iv=iv) return cls(key=key, iv=iv)