This commit is contained in:
Marek Lenczewski
2026-04-15 21:53:37 +02:00
parent 2156bb3226
commit 1cef5fa1e8
18 changed files with 154 additions and 71 deletions

View File

@@ -7,10 +7,10 @@ VIDEOS_DIR = "/videos"
CHUNK_SIZE = 64 * 1024
def streamAndSave(videoId: int, youtubeUrl: str):
def streamAndSave(videoId: int, youtubeUrl: str, maxHeight: int = 1080):
from model.video import Video # Lazy-Import gegen Zirkular
outputPath = f"{VIDEOS_DIR}/{videoId}.mp4"
yield from streamVideoLive(videoId, youtubeUrl)
yield from streamVideoLive(videoId, youtubeUrl, maxHeight)
if Path(outputPath).exists():
db = SessionLocal()
try:
@@ -19,11 +19,14 @@ def streamAndSave(videoId: int, youtubeUrl: str):
db.close()
def _getStreamUrls(youtubeUrl: str):
def _getStreamUrls(youtubeUrl: str, maxHeight: int = 1080):
formatFilter = f"bestvideo[ext=mp4][vcodec^=avc][height<={maxHeight}]+bestaudio[ext=m4a]/best[ext=mp4]"
result = subprocess.run(
[
"yt-dlp",
"-f", "bestvideo[ext=mp4][vcodec^=avc]+bestaudio[ext=m4a]/best[ext=mp4]",
"--cookies", "/app/cookies.txt",
"--remote-components", "ejs:github",
"-f", formatFilter,
"--print", "urls",
youtubeUrl,
],
@@ -40,10 +43,10 @@ def _getStreamUrls(youtubeUrl: str):
return None, None
def streamVideoLive(videoId: int, youtubeUrl: str):
def streamVideoLive(videoId: int, youtubeUrl: str, maxHeight: int = 1080):
outputPath = f"{VIDEOS_DIR}/{videoId}.mp4"
videoUrl, audioUrl = _getStreamUrls(youtubeUrl)
videoUrl, audioUrl = _getStreamUrls(youtubeUrl, maxHeight)
if not videoUrl:
return