Made it possible to switch streamer used codecs

This commit is contained in:
ArabCoders 2024-04-15 22:06:14 +03:00
parent 1881df6c5c
commit 48ad232393
5 changed files with 19 additions and 21 deletions

4
.vscode/launch.json vendored
View file

@ -33,7 +33,7 @@
"YTP_DOWNLOAD_PATH": "${workspaceFolder}/var/downloads",
"YTP_TEMP_PATH": "${workspaceFolder}/var/tmp",
"YTP_URL_HOST": "http://localhost:8081",
"YTP_LOG_LEVEL": "DEBUG"
"YTP_LOG_LEVEL": "DEBUG",
}
},
{
@ -48,7 +48,7 @@
"YTP_DOWNLOAD_PATH": "${workspaceFolder}/var/downloads",
"YTP_TEMP_PATH": "${workspaceFolder}/var/tmp",
"YTP_URL_HOST": "http://localhost:8081",
"YTP_LOG_LEVEL": "DEBUG"
"YTP_LOG_LEVEL": "DEBUG",
}
},
{

View file

@ -28,6 +28,7 @@
"preferredcodec",
"preferredquality",
"tmpfilename",
"vcodec",
"vconvert",
"writedescription",
"xerror"

View file

@ -75,6 +75,9 @@ Certain values can be set via environment variables, using the `-e` parameter on
* __YTP_PORT__: Which port to bind to. Defaults to `8081`.
* __YTP_LOG_LEVEL__: Log level. Defaults to `info`.
* __YTP_MAX_WORKERS__: How many works to use for downloads. Defaults to `1`.
* __YTP_MAX_WORKERS__: How many works to use for downloads. Defaults to `1`.
* __YTP_STREAMER_VCODEC__: The video codec to use for in-browser streaming. Defaults to `libx264`.
* __YTP_STREAMER_ACODEC__: The audio codec to use for in-browser streaming. Defaults to `aac`.
## Running behind a reverse proxy

View file

@ -53,6 +53,9 @@ class Config:
started: int = 0
streamer_vcodec = 'libx264'
streamer_acodec = 'aac'
ytdlp_version: str = YTDLP_VERSION
_int_vars: tuple = ('port', 'max_workers', 'socket_timeout', 'extract_info_timeout',)

View file

@ -4,6 +4,7 @@ import logging
import os
import tempfile
from Utils import calcDownloadPath
from Config import Config
LOG = logging.getLogger('segments')
@ -13,12 +14,18 @@ class Segments:
index: int
vconvert: bool
aconvert: bool
vcodec: str
acodec: str
def __init__(self, index: int, duration: float, vconvert: bool, aconvert: bool):
config = Config.get_instance()
self.index = int(index)
self.duration = float(duration)
self.vconvert = bool(vconvert)
self.aconvert = bool(aconvert)
self.vcodec = config.streamer_vcodec
self.acodec = config.streamer_acodec
self.vconvert = True
async def stream(self, path: str, file: str) -> bytes:
realFile: str = calcDownloadPath(basePath=path, folder=file, createPath=False)
@ -66,29 +73,13 @@ class Segments:
fargs.append('-2')
fargs.append('-codec:v')
fargs.append('libx264' if self.vconvert else 'copy')
if self.vconvert:
fargs.append('-crf')
fargs.append('23')
fargs.append('-preset:v')
fargs.append('fast')
fargs.append('-level')
fargs.append('4.1')
fargs.append('-profile:v')
fargs.append('baseline')
fargs.append(self.vcodec if self.vconvert else 'copy')
# audio section.
fargs.append('-map')
fargs.append('0:a:0')
fargs.append('-codec:a')
fargs.append('aac' if self.aconvert else 'copy')
if self.aconvert:
fargs.append('-b:a')
fargs.append('192k')
fargs.append('-ar')
fargs.append('22050')
fargs.append('-ac')
fargs.append('2')
fargs.append(self.acodec if self.aconvert else 'copy')
fargs.append('-sn')
@ -110,7 +101,7 @@ class Segments:
data, err = await proc.communicate()
if 0 != proc.returncode:
LOG.error(f'Failed to stream {realFile} segment {self.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.index}.')
return data