This commit is contained in:
Marek
2026-04-05 15:38:01 +02:00
parent b5659069b1
commit 6e96c5ee99
13 changed files with 174 additions and 65 deletions

View File

@@ -1,6 +1,4 @@
from datetime import datetime
from sqlalchemy import Column, DateTime, Integer, String
from sqlalchemy import Column, Integer, String
from database import Base
@@ -14,4 +12,3 @@ class Video(Base):
thumbnail_url = Column(String, nullable=False)
youtube_url = Column(String, nullable=False)
file_path = Column(String, nullable=True)
created_at = Column(DateTime, default=datetime.utcnow)

View File

@@ -8,7 +8,8 @@ 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, stream_video_live
from services.download_service import download_video
from services.stream_service import stream_video_live
router = APIRouter(prefix="/videos", tags=["videos"])

View File

@@ -1,5 +1,3 @@
from datetime import datetime
from pydantic import BaseModel
@@ -17,7 +15,6 @@ class VideoResponse(BaseModel):
thumbnail_url: str
youtube_url: str
is_downloaded: bool
created_at: datetime
class Config:
from_attributes = True
@@ -31,5 +28,4 @@ class VideoResponse(BaseModel):
thumbnail_url=video.thumbnail_url,
youtube_url=video.youtube_url,
is_downloaded=video.file_path is not None,
created_at=video.created_at,
)

View File

@@ -25,34 +25,3 @@ 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()

View File

@@ -0,0 +1,32 @@
import subprocess
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()