dragxd commited on
Commit
b35c517
·
verified ·
1 Parent(s): 710bf4d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -0
app.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from fastapi.responses import RedirectResponse
3
+ import subprocess
4
+
5
+ app = FastAPI()
6
+
7
+ @app.get("/")
8
+ async def root():
9
+ return {"status": "FastAPI working", "message": "YT-DLP backend ready"}
10
+
11
+ @app.get("/stream/{vidid}")
12
+ async def stream_audio(vidid: str):
13
+ try:
14
+ result = subprocess.run(
15
+ ["yt-dlp", "-f", "bestaudio", "--get-url", f"https://www.youtube.com/watch?v={vidid}"],
16
+ capture_output=True, text=True
17
+ )
18
+ url = result.stdout.strip()
19
+ if not url:
20
+ raise HTTPException(status_code=404, detail="No audio URL found.")
21
+ return RedirectResponse(url)
22
+ except Exception as e:
23
+ raise HTTPException(status_code=500, detail=str(e))