Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, Query
|
2 |
+
from fastapi.responses import JSONResponse
|
3 |
+
import subprocess
|
4 |
+
import json
|
5 |
+
|
6 |
+
app = FastAPI()
|
7 |
+
|
8 |
+
@app.get("/")
|
9 |
+
def root():
|
10 |
+
return {"message": "Welcome to yt-dlp API (FastAPI)"}
|
11 |
+
|
12 |
+
@app.get("/download")
|
13 |
+
def download_info(url: str = Query(..., description="Video URL")):
|
14 |
+
try:
|
15 |
+
result = subprocess.run(
|
16 |
+
["yt-dlp", "-J", url],
|
17 |
+
capture_output=True, text=True, timeout=20
|
18 |
+
)
|
19 |
+
if result.returncode != 0:
|
20 |
+
return JSONResponse(status_code=500, content={"error": result.stderr})
|
21 |
+
|
22 |
+
data = json.loads(result.stdout)
|
23 |
+
return {
|
24 |
+
"title": data.get("title"),
|
25 |
+
"uploader": data.get("uploader"),
|
26 |
+
"duration": data.get("duration"),
|
27 |
+
"formats": [
|
28 |
+
{
|
29 |
+
"format_id": f["format_id"],
|
30 |
+
"ext": f["ext"],
|
31 |
+
"resolution": f.get("resolution") or f"{f.get('height')}p",
|
32 |
+
"url": f["url"]
|
33 |
+
}
|
34 |
+
for f in data.get("formats", [])
|
35 |
+
]
|
36 |
+
}
|
37 |
+
except Exception as e:
|
38 |
+
return JSONResponse(status_code=500, content={"error": str(e)})
|