diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..98778d9 --- /dev/null +++ b/app/.gitignore @@ -0,0 +1,5 @@ +.gradle/ +build/ +local.properties +*.iml +.idea/ diff --git a/backend/.dockerignore b/backend/.dockerignore new file mode 100644 index 0000000..36cf264 --- /dev/null +++ b/backend/.dockerignore @@ -0,0 +1,2 @@ +videos/ +__pycache__/ diff --git a/backend/.gitignore b/backend/.gitignore new file mode 100644 index 0000000..f9ce0cf --- /dev/null +++ b/backend/.gitignore @@ -0,0 +1,3 @@ +__pycache__/ +*.pyc +videos/ diff --git a/backend/routes/__pycache__/videos.cpython-312.pyc b/backend/routes/__pycache__/videos.cpython-312.pyc index 80a5230..aa80bbe 100644 Binary files a/backend/routes/__pycache__/videos.cpython-312.pyc and b/backend/routes/__pycache__/videos.cpython-312.pyc differ diff --git a/backend/routes/videos.py b/backend/routes/videos.py index 02ca860..132575f 100644 --- a/backend/routes/videos.py +++ b/backend/routes/videos.py @@ -8,7 +8,7 @@ from sqlalchemy.orm import Session from database import get_db from schemas import VideoCreate, VideoResponse from services import video_service -from services.download_service import download_video +from services.download_service import download_video, stream_video_live router = APIRouter(prefix="/videos", tags=["videos"]) @@ -51,8 +51,10 @@ def stream_video(video_id: int, db: Session = Depends(get_db)): raise HTTPException(status_code=404, detail="Video nicht gefunden") if not video.file_path: - download_video(video.id, video.youtube_url) - db.refresh(video) + return StreamingResponse( + stream_video_live(video.youtube_url), + media_type="video/mp4", + ) path = Path(video.file_path) if not path.exists(): diff --git a/backend/services/__pycache__/download_service.cpython-312.pyc b/backend/services/__pycache__/download_service.cpython-312.pyc index 540a64d..f08b639 100644 Binary files a/backend/services/__pycache__/download_service.cpython-312.pyc and b/backend/services/__pycache__/download_service.cpython-312.pyc differ diff --git a/backend/services/download_service.py b/backend/services/download_service.py index 6f2debe..baad68a 100644 --- a/backend/services/download_service.py +++ b/backend/services/download_service.py @@ -25,3 +25,34 @@ def download_video(video_id: int, youtube_url: str): update_file_path(db, video_id, output_path) finally: db.close() + + +def stream_video_live(youtube_url: str): + result = subprocess.run( + [ + "yt-dlp", + "-f", "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best", + "-g", youtube_url, + ], + capture_output=True, text=True, check=True, + ) + urls = result.stdout.strip().split("\n") + + cmd = ["ffmpeg"] + for url in urls: + cmd.extend(["-i", url]) + cmd.extend(["-c", "copy", "-movflags", "frag_keyframe+empty_moov", "-f", "mp4", "pipe:1"]) + + process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL) + try: + while True: + chunk = process.stdout.read(1024 * 1024) + if not chunk: + break + yield chunk + process.wait() + except GeneratorExit: + process.kill() + finally: + if process.poll() is None: + process.kill()