File size: 926 Bytes
51c2f6e
 
 
 
ab2df11
51c2f6e
 
 
 
 
ab2df11
 
94cc57f
 
 
ab2df11
 
 
 
94cc57f
ab2df11
 
51c2f6e
 
 
ab2df11
51c2f6e
 
 
ab2df11
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from fastapi import FastAPI, Query
from fastapi.responses import FileResponse, JSONResponse
import yt_dlp
import uuid
import os

app = FastAPI()

@app.get("/download")
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)