From 8f9d3e27f3a577bae6a48acb8ebec2bee86257d4 Mon Sep 17 00:00:00 2001 From: ArabCoders Date: Sun, 31 Dec 2023 18:51:59 +0300 Subject: [PATCH] Made the player more static. --- app/main.py | 8 +++++--- app/player/M3u8.py | 15 ++++++--------- app/player/Segments.py | 11 ++++------- 3 files changed, 15 insertions(+), 19 deletions(-) diff --git a/app/main.py b/app/main.py index c8b2e245..3fe4d74e 100644 --- a/app/main.py +++ b/app/main.py @@ -285,7 +285,10 @@ class Main: raise web.HTTPBadRequest(reason='file is required') return web.Response( - text=M3u8(self.config).make_stream(file), + text=M3u8(url=f"{self.config.url_host}{self.config.url_prefix}").make_stream( + download_path=self.config.download_path, + file=file + ), headers={ 'Content-Type': 'application/x-mpegURL', 'Cache-Control': 'no-cache', @@ -308,7 +311,6 @@ class Main: raise web.HTTPBadRequest(reason='segment is required') segmenter = Segments( - config=self.config, segment_index=int(segment), segment_duration=float('{:.6f}'.format( float(sd if sd else M3u8.segment_duration))), @@ -317,7 +319,7 @@ class Main: ) return web.Response( - body=await segmenter.stream(file), + body=await segmenter.stream(download_path=self.config.download_path, file=file), headers={ 'Content-Type': 'video/mpegts', 'Connection': 'keep-alive', diff --git a/app/player/M3u8.py b/app/player/M3u8.py index eb0db205..28ef74fa 100644 --- a/app/player/M3u8.py +++ b/app/player/M3u8.py @@ -3,7 +3,6 @@ import os from urllib.parse import quote from Utils import calcDownloadPath from .ffprobe import FFProbe -from Config import Config class M3u8: @@ -11,17 +10,15 @@ class M3u8: ok_acodecs: tuple = ('aac', 'mp3',) segment_duration: float = 10.000000 - config: Config = None - download_path: str = None + url: str = None - def __init__(self, config: Config, segment_duration: float = 6.000000): - self.config = config - self.download_path = self.config.download_path + def __init__(self, url: str, segment_duration: float = 6.000000): + self.url = url self.segment_duration = float(segment_duration) - def make_stream(self, file: str): + def make_stream(self, download_path: str, file: str): realFile: str = calcDownloadPath( - basePath=self.download_path, + basePath=download_path, folder=file, createPath=False ) @@ -68,7 +65,7 @@ class M3u8: else: m3u8 += f"#EXTINF:{segmentSize}, nodesc\n" - m3u8 += f"{self.config.url_host}{self.config.url_prefix}segments/{i}/{quote(file)}" + m3u8 += f"{self.url}segments/{i}/{quote(file)}" if len(segmentParams) > 0: m3u8 += '?'+'&'.join([f"{key}={value}" for key, value in segmentParams.items()]) diff --git a/app/player/Segments.py b/app/player/Segments.py index 7ba8bed5..9fa4de46 100644 --- a/app/player/Segments.py +++ b/app/player/Segments.py @@ -8,25 +8,22 @@ from Config import Config log = logging.getLogger('segments') + class Segments: - config: Config = None - download_path: str = None segment_duration: int segment_index: int vconvert: bool aconvert: bool - def __init__(self, config: Config, segment_index: int, segment_duration: float, vconvert: bool, aconvert: bool): - self.config = config - self.download_path = self.config.download_path + def __init__(self, segment_index: int, segment_duration: float, vconvert: bool, aconvert: bool): self.segment_duration = float(segment_duration) self.segment_index = int(segment_index) self.vconvert = bool(vconvert) self.aconvert = bool(aconvert) - async def stream(self, file: str) -> bytes: + async def stream(self, download_path: str, file: str) -> bytes: realFile: str = calcDownloadPath( - basePath=self.download_path, + basePath=download_path, folder=file, createPath=False )