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

1
backend/.gitignore vendored
View File

@@ -1,3 +1,4 @@
__pycache__/
*.pyc
videos/
cookies.txt

View File

@@ -12,25 +12,25 @@ router = APIRouter(prefix="/videos")
@router.post("/{videoId}/download")
def download(videoId: int, db: DbSession):
def download(videoId: int, db: DbSession, maxHeight: int = 1080):
video = Video.getById(db, videoId)
if not video:
raise HTTPException(404, "Video nicht gefunden")
if video.filePath:
return {"status": "already_downloaded"}
downloadAsync(videoId, video.youtubeUrl)
downloadAsync(videoId, video.youtubeUrl, maxHeight)
return {"status": "download_started"}
@router.get("/{videoId}/stream")
def stream(videoId: int, db: DbSession):
def stream(videoId: int, db: DbSession, maxHeight: int = 1080):
video = Video.getById(db, videoId)
if not video:
raise HTTPException(404, "Video nicht gefunden")
if not video.filePath:
return StreamingResponse(streamAndSave(videoId, video.youtubeUrl), media_type="video/mp4")
return StreamingResponse(streamAndSave(videoId, video.youtubeUrl, maxHeight), media_type="video/mp4")
path = Path(video.filePath)
if not path.exists():

View File

@@ -9,17 +9,20 @@ VIDEOS_DIR = "/videos"
MIN_VALID_SIZE = 1024 * 100 # 100 KB
def downloadAsync(videoId: int, youtubeUrl: str):
threading.Thread(target=downloadVideo, args=(videoId, youtubeUrl)).start()
def downloadAsync(videoId: int, youtubeUrl: str, maxHeight: int = 1080):
threading.Thread(target=downloadVideo, args=(videoId, youtubeUrl, maxHeight)).start()
def downloadVideo(videoId: int, youtubeUrl: str):
def downloadVideo(videoId: int, youtubeUrl: str, maxHeight: int = 1080):
outputPath = f"{VIDEOS_DIR}/{videoId}.mp4"
formatFilter = f"bestvideo[ext=mp4][vcodec^=avc][height<={maxHeight}]+bestaudio[ext=m4a]/best[ext=mp4]"
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,
"-o", outputPath,
"--merge-output-format", "mp4",
"--force-overwrites",

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