Twan07 commited on
Commit
9529fcd
·
verified ·
1 Parent(s): afd2d2b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -0
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)})