|
from fastapi import FastAPI, HTTPException |
|
from fastapi.responses import RedirectResponse |
|
import subprocess |
|
|
|
app = FastAPI() |
|
|
|
@app.get("/") |
|
async def root(): |
|
return {"status": "FastAPI working", "message": "YT-DLP backend ready"} |
|
|
|
@app.get("/stream/{vidid}") |
|
async def stream_audio(vidid: str): |
|
try: |
|
result = subprocess.run( |
|
["yt-dlp", "-f", "bestaudio", "--get-url", f"https://www.youtube.com/watch?v={vidid}"], |
|
capture_output=True, text=True |
|
) |
|
url = result.stdout.strip() |
|
if not url: |
|
raise HTTPException(status_code=404, detail="No audio URL found.") |
|
return RedirectResponse(url) |
|
except Exception as e: |
|
raise HTTPException(status_code=500, detail=str(e)) |