new version

This commit is contained in:
UrloMythus
2026-01-11 14:29:22 +01:00
parent b8a40b5afc
commit 7785e8c604
45 changed files with 5463 additions and 832 deletions

View File

@@ -22,6 +22,7 @@ from .utils.http_utils import (
)
from .utils.m3u8_processor import M3U8Processor
from .utils.mpd_utils import pad_base64
from .configs import settings
logger = logging.getLogger(__name__)
@@ -102,7 +103,8 @@ async def handle_hls_stream_proxy(
# If force_playlist_proxy is enabled, skip detection and directly process as m3u8
if hls_params.force_playlist_proxy:
return await fetch_and_process_m3u8(
streamer, hls_params.destination, proxy_headers, request, hls_params.key_url, hls_params.force_playlist_proxy
streamer, hls_params.destination, proxy_headers, request,
hls_params.key_url, hls_params.force_playlist_proxy, hls_params.key_only_proxy, hls_params.no_proxy
)
parsed_url = urlparse(hls_params.destination)
@@ -111,7 +113,8 @@ async def handle_hls_stream_proxy(
0
] in ["m3u", "m3u8", "m3u_plus"]:
return await fetch_and_process_m3u8(
streamer, hls_params.destination, proxy_headers, request, hls_params.key_url, hls_params.force_playlist_proxy
streamer, hls_params.destination, proxy_headers, request,
hls_params.key_url, hls_params.force_playlist_proxy, hls_params.key_only_proxy, hls_params.no_proxy
)
# Create initial streaming response to check content type
@@ -120,7 +123,8 @@ async def handle_hls_stream_proxy(
if "mpegurl" in response_headers.get("content-type", "").lower():
return await fetch_and_process_m3u8(
streamer, hls_params.destination, proxy_headers, request, hls_params.key_url, hls_params.force_playlist_proxy
streamer, hls_params.destination, proxy_headers, request,
hls_params.key_url, hls_params.force_playlist_proxy, hls_params.key_only_proxy, hls_params.no_proxy
)
return EnhancedStreamingResponse(
@@ -224,7 +228,14 @@ async def proxy_stream(method: str, destination: str, proxy_headers: ProxyReques
async def fetch_and_process_m3u8(
streamer: Streamer, url: str, proxy_headers: ProxyRequestHeaders, request: Request, key_url: str = None, force_playlist_proxy: bool = None
streamer: Streamer,
url: str,
proxy_headers: ProxyRequestHeaders,
request: Request,
key_url: str = None,
force_playlist_proxy: bool = None,
key_only_proxy: bool = False,
no_proxy: bool = False
):
"""
Fetches and processes the m3u8 playlist on-the-fly, converting it to an HLS playlist.
@@ -236,6 +247,8 @@ async def fetch_and_process_m3u8(
request (Request): The incoming HTTP request.
key_url (str, optional): The HLS Key URL to replace the original key URL. Defaults to None.
force_playlist_proxy (bool, optional): Force all playlist URLs to be proxied through MediaFlow. Defaults to None.
key_only_proxy (bool, optional): Only proxy the key URL, leaving segment URLs direct. Defaults to False.
no_proxy (bool, optional): If True, returns the manifest without proxying any URLs. Defaults to False.
Returns:
Response: The HTTP response with the processed m3u8 playlist.
@@ -246,7 +259,7 @@ async def fetch_and_process_m3u8(
await streamer.create_streaming_response(url, proxy_headers.request)
# Initialize processor and response headers
processor = M3U8Processor(request, key_url, force_playlist_proxy)
processor = M3U8Processor(request, key_url, force_playlist_proxy, key_only_proxy, no_proxy)
response_headers = {
"content-disposition": "inline",
"accept-ranges": "none",
@@ -378,7 +391,13 @@ async def get_segment(
Response: The HTTP response with the processed segment.
"""
try:
init_content = await get_cached_init_segment(segment_params.init_url, proxy_headers.request)
live_cache_ttl = settings.mpd_live_init_cache_ttl if segment_params.is_live else None
init_content = await get_cached_init_segment(
segment_params.init_url,
proxy_headers.request,
cache_token=segment_params.key_id,
ttl=live_cache_ttl,
)
segment_content = await download_file_with_retry(segment_params.segment_url, proxy_headers.request)
except Exception as e:
return handle_exceptions(e)