updated to lastest version

This commit is contained in:
UrloMythus
2025-09-01 18:41:27 +02:00
parent bc41be6194
commit 8f8c3b195e
21 changed files with 2389 additions and 390 deletions

View File

@@ -1,3 +1,4 @@
import asyncio
import codecs
import re
from typing import AsyncGenerator
@@ -6,6 +7,7 @@ from urllib import parse
from mediaflow_proxy.configs import settings
from mediaflow_proxy.utils.crypto_utils import encryption_handler
from mediaflow_proxy.utils.http_utils import encode_mediaflow_proxy_url, encode_stremio_proxy_url, get_original_scheme
from mediaflow_proxy.utils.hls_prebuffer import hls_prebuffer
class M3U8Processor:
@@ -24,6 +26,7 @@ class M3U8Processor:
self.mediaflow_proxy_url = str(
request.url_for("hls_manifest_proxy").replace(scheme=get_original_scheme(request))
)
self.playlist_url = None # Will be set when processing starts
async def process_m3u8(self, content: str, base_url: str) -> str:
"""
@@ -36,6 +39,9 @@ class M3U8Processor:
Returns:
str: The processed m3u8 content.
"""
# Store the playlist URL for prebuffering
self.playlist_url = base_url
lines = content.splitlines()
processed_lines = []
for line in lines:
@@ -45,6 +51,23 @@ class M3U8Processor:
processed_lines.append(await self.proxy_content_url(line, base_url))
else:
processed_lines.append(line)
# Pre-buffer segments if enabled and this is a playlist
if (settings.enable_hls_prebuffer and
"#EXTM3U" in content and
self.playlist_url):
# Extract headers from request for pre-buffering
headers = {}
for key, value in self.request.query_params.items():
if key.startswith("h_"):
headers[key[2:]] = value
# Start pre-buffering in background using the actual playlist URL
asyncio.create_task(
hls_prebuffer.prebuffer_playlist(self.playlist_url, headers)
)
return "\n".join(processed_lines)
async def process_m3u8_streaming(
@@ -52,6 +75,7 @@ class M3U8Processor:
) -> AsyncGenerator[str, None]:
"""
Processes the m3u8 content on-the-fly, yielding processed lines as they are read.
Optimized to avoid accumulating the entire playlist content in memory.
Args:
content_iterator: An async iterator that yields chunks of the m3u8 content.
@@ -60,8 +84,13 @@ class M3U8Processor:
Yields:
str: Processed lines of the m3u8 content.
"""
# Store the playlist URL for prebuffering
self.playlist_url = base_url
buffer = "" # String buffer for decoded content
decoder = codecs.getincrementaldecoder("utf-8")(errors="replace")
is_playlist_detected = False
is_prebuffer_started = False
# Process the content chunk by chunk
async for chunk in content_iterator:
@@ -72,6 +101,10 @@ class M3U8Processor:
decoded_chunk = decoder.decode(chunk)
buffer += decoded_chunk
# Check for playlist marker early to avoid accumulating content
if not is_playlist_detected and "#EXTM3U" in buffer:
is_playlist_detected = True
# Process complete lines
lines = buffer.split("\n")
if len(lines) > 1:
@@ -84,6 +117,25 @@ class M3U8Processor:
# Keep the last line in the buffer (it might be incomplete)
buffer = lines[-1]
# Start pre-buffering early once we detect this is a playlist
# This avoids waiting until the entire playlist is processed
if (settings.enable_hls_prebuffer and
is_playlist_detected and
not is_prebuffer_started and
self.playlist_url):
# Extract headers from request for pre-buffering
headers = {}
for key, value in self.request.query_params.items():
if key.startswith("h_"):
headers[key[2:]] = value
# Start pre-buffering in background using the actual playlist URL
asyncio.create_task(
hls_prebuffer.prebuffer_playlist(self.playlist_url, headers)
)
is_prebuffer_started = True
# Process any remaining data in the buffer plus final bytes
final_chunk = decoder.decode(b"", final=True)
if final_chunk:
@@ -209,4 +261,4 @@ class M3U8Processor:
full_url,
query_params=query_params,
encryption_handler=encryption_handler if has_encrypted else None,
)
)