Spaces:
Sleeping
Sleeping
from fastapi import FastAPI, Query | |
from fastapi.responses import FileResponse, JSONResponse | |
import yt_dlp | |
import uuid | |
import os | |
app = FastAPI() | |
def download_video(url: str = Query(..., description="Video URL to download")): | |
video_id = str(uuid.uuid4()) | |
output_file = f"{video_id}.mp4" | |
with open("cookies.txt", "r") as f: | |
print(f.readline()) # just to check access | |
ydl_opts = { | |
'format': 'best', | |
'outtmpl': output_file, | |
'cookiefile': f # <-- Use the exported cookie file here | |
} | |
try: | |
with yt_dlp.YoutubeDL(ydl_opts) as ydl: | |
ydl.download([url]) | |
return FileResponse(path=output_file, media_type="video/mp4", filename="video.mp4") | |
except Exception as e: | |
return JSONResponse(status_code=400, content={"error": str(e)}) | |
finally: | |
if os.path.exists(output_file): | |
os.remove(output_file) | |