41 lines
993 B
Python
41 lines
993 B
Python
import subprocess
|
|
import threading
|
|
from pathlib import Path
|
|
|
|
from database.database import SessionLocal
|
|
from model.video import Video
|
|
|
|
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 downloadVideo(videoId: int, youtubeUrl: str):
|
|
outputPath = f"{VIDEOS_DIR}/{videoId}.mp4"
|
|
|
|
subprocess.run(
|
|
[
|
|
"yt-dlp",
|
|
"-f", "bestvideo[ext=mp4][vcodec^=avc]+bestaudio[ext=m4a]/best[ext=mp4]",
|
|
"-o", outputPath,
|
|
"--merge-output-format", "mp4",
|
|
"--force-overwrites",
|
|
"--no-continue",
|
|
youtubeUrl,
|
|
],
|
|
check=True,
|
|
)
|
|
|
|
path = Path(outputPath)
|
|
if not path.exists() or path.stat().st_size < MIN_VALID_SIZE:
|
|
return
|
|
|
|
db = SessionLocal()
|
|
try:
|
|
Video.updateFilePath(db, videoId, outputPath)
|
|
finally:
|
|
db.close()
|