Create app.py
Browse files
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))
|