File size: 971 Bytes
b35c517
 
 
 
7451bec
 
 
 
 
 
 
b35c517
7451bec
b35c517
 
 
7451bec
b35c517
 
 
 
 
 
 
 
 
 
 
7451bec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from fastapi import FastAPI, HTTPException
from fastapi.responses import RedirectResponse
import subprocess

app = FastAPI(
    title="YouTube Audio Streamer",
    description="Stream best audio of a YouTube video using yt-dlp.",
    version="1.0.0",
    docs_url="/docs",
    redoc_url="/redoc"
)

@app.get("/", tags=["Health"])
async def root():
    return {"status": "FastAPI working", "message": "YT-DLP backend ready"}

@app.get("/stream/{vidid}", tags=["Stream"])
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=f"Error: {str(e)}")