Cleanup the video player.

This commit is contained in:
ArabCoders 2024-03-26 00:16:00 +03:00
parent 70cff70247
commit 46e7d28fc8
3 changed files with 23 additions and 23 deletions

View file

@ -443,14 +443,14 @@ class Main:
raise web.HTTPBadRequest(reason='segment is required') raise web.HTTPBadRequest(reason='segment is required')
segmenter = Segments( segmenter = Segments(
segment_index=int(segment), index=int(segment),
segment_duration=float('{:.6f}'.format(float(sd if sd else M3u8.segment_duration))), duration=float('{:.6f}'.format(float(sd if sd else M3u8.duration))),
vconvert=True if vc == 1 else False, vconvert=True if vc == 1 else False,
aconvert=True if ac == 1 else False aconvert=True if ac == 1 else False
) )
return web.Response( return web.Response(
body=await segmenter.stream(download_path=self.config.download_path, file=file), body=await segmenter.stream(path=self.config.download_path, file=file),
headers={ headers={
'Content-Type': 'video/mpegts', 'Content-Type': 'video/mpegts',
'Cache-Control': 'no-cache', 'Cache-Control': 'no-cache',

View file

@ -9,12 +9,12 @@ class M3u8:
ok_vcodecs: tuple = ('h264', 'x264', 'avc',) ok_vcodecs: tuple = ('h264', 'x264', 'avc',)
ok_acodecs: tuple = ('aac', 'mp3',) ok_acodecs: tuple = ('aac', 'mp3',)
segment_duration: float = 10.000000
url: str = None url: str = None
duration: float = 6.000000
def __init__(self, url: str, segment_duration: float = 6.000000): def __init__(self, url: str, segment_duration: float = None):
self.url = url self.url = url
self.segment_duration = float(segment_duration) self.duration = float(segment_duration) if segment_duration is not None else self.duration
async def make_stream(self, download_path: str, file: str): async def make_stream(self, download_path: str, file: str):
realFile: str = calcDownloadPath(basePath=download_path, folder=file, createPath=False) realFile: str = calcDownloadPath(basePath=download_path, folder=file, createPath=False)
@ -35,12 +35,12 @@ class M3u8:
m3u8 = "#EXTM3U\n" m3u8 = "#EXTM3U\n"
m3u8 += "#EXT-X-VERSION:3\n" m3u8 += "#EXT-X-VERSION:3\n"
m3u8 += f"#EXT-X-TARGETDURATION:{int(self.segment_duration)}\n" m3u8 += f"#EXT-X-TARGETDURATION:{int(self.duration)}\n"
m3u8 += "#EXT-X-MEDIA-SEQUENCE:0\n" m3u8 += "#EXT-X-MEDIA-SEQUENCE:0\n"
m3u8 += "#EXT-X-PLAYLIST-TYPE:VOD\n" m3u8 += "#EXT-X-PLAYLIST-TYPE:VOD\n"
segmentSize: float = '{:.6f}'.format(self.segment_duration) segmentSize: float = '{:.6f}'.format(self.duration)
splits: int = math.ceil(duration / self.segment_duration) splits: int = math.ceil(duration / self.duration)
segmentParams: dict = {} segmentParams: dict = {}
@ -54,7 +54,7 @@ class M3u8:
for i in range(splits): for i in range(splits):
if (i + 1) == splits: if (i + 1) == splits:
segmentParams.update({'sd': '{:.6f}'.format(duration - (i * self.segment_duration))}) segmentParams.update({'sd': '{:.6f}'.format(duration - (i * self.duration))})
m3u8 += f"#EXTINF:{segmentParams['sd']}, nodesc\n" m3u8 += f"#EXTINF:{segmentParams['sd']}, nodesc\n"
else: else:
m3u8 += f"#EXTINF:{segmentSize}, nodesc\n" m3u8 += f"#EXTINF:{segmentSize}, nodesc\n"

View file

@ -10,19 +10,19 @@ LOG = logging.getLogger('segments')
class Segments: class Segments:
segment_duration: int duration: int
segment_index: int index: int
vconvert: bool vconvert: bool
aconvert: bool aconvert: bool
def __init__(self, segment_index: int, segment_duration: float, vconvert: bool, aconvert: bool): def __init__(self, index: int, duration: float, vconvert: bool, aconvert: bool):
self.segment_duration = float(segment_duration) self.index = int(index)
self.segment_index = int(segment_index) self.duration = float(duration)
self.vconvert = bool(vconvert) self.vconvert = bool(vconvert)
self.aconvert = bool(aconvert) self.aconvert = bool(aconvert)
async def stream(self, download_path: str, file: str) -> bytes: async def stream(self, path: str, file: str) -> bytes:
realFile: str = calcDownloadPath(basePath=download_path, folder=file, createPath=False) realFile: str = calcDownloadPath(basePath=path, folder=file, createPath=False)
if not os.path.exists(realFile): if not os.path.exists(realFile):
raise Exception(f"File {realFile} does not exist.") raise Exception(f"File {realFile} does not exist.")
@ -33,10 +33,10 @@ class Segments:
if not os.path.exists(tmpFile): if not os.path.exists(tmpFile):
os.symlink(realFile, tmpFile) os.symlink(realFile, tmpFile)
if self.segment_index == 0: if self.index == 0:
startTime: float = '{:.6f}'.format(0) startTime: float = '{:.6f}'.format(0)
else: else:
startTime: float = '{:.6f}'.format((self.segment_duration * self.segment_index)) startTime: float = '{:.6f}'.format((self.duration * self.index))
fargs = [] fargs = []
fargs.append('-xerror') fargs.append('-xerror')
@ -47,7 +47,7 @@ class Segments:
fargs.append('-ss') fargs.append('-ss')
fargs.append(str(startTime if startTime else '0.00000')) fargs.append(str(startTime if startTime else '0.00000'))
fargs.append('-t') fargs.append('-t')
fargs.append(str('{:.6f}'.format(self.segment_duration))) fargs.append(str('{:.6f}'.format(self.duration)))
fargs.append('-copyts') fargs.append('-copyts')
@ -99,7 +99,7 @@ class Segments:
fargs.append('mpegts') fargs.append('mpegts')
fargs.append('pipe:1') fargs.append('pipe:1')
LOG.debug(f"Streaming '{realFile}' segment '{self.segment_index}'. " + " ".join(fargs)) LOG.debug(f"Streaming '{realFile}' segment '{self.index}'. " + " ".join(fargs))
proc = await asyncio.subprocess.create_subprocess_exec( proc = await asyncio.subprocess.create_subprocess_exec(
'ffmpeg', *fargs, 'ffmpeg', *fargs,
@ -111,7 +111,7 @@ class Segments:
data, err = await proc.communicate() data, err = await proc.communicate()
if 0 != proc.returncode: if 0 != proc.returncode:
LOG.error(f'Failed to stream {realFile} segment {self.segment_index}. {err.decode("utf-8")}') LOG.error(f'Failed to stream {realFile} segment {self.index}. {err.decode("utf-8")}')
raise Exception(f'Failed to stream {realFile} segment {self.segment_index}.') raise Exception(f'Failed to stream {realFile} segment {self.index}.')
return data return data