Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,11 @@
|
|
1 |
-
from fastapi import FastAPI, HTTPException
|
2 |
from fastapi.responses import RedirectResponse
|
3 |
import subprocess
|
4 |
|
5 |
app = FastAPI(
|
6 |
title="YouTube Audio Streamer",
|
7 |
description="Stream best audio of a YouTube video using yt-dlp.",
|
8 |
-
version="1.0.
|
9 |
docs_url="/docs",
|
10 |
redoc_url="/redoc"
|
11 |
)
|
@@ -14,16 +14,19 @@ app = FastAPI(
|
|
14 |
async def root():
|
15 |
return {"status": "FastAPI working", "message": "YT-DLP backend ready"}
|
16 |
|
17 |
-
@app.get("/stream
|
18 |
-
async def stream_audio(
|
|
|
|
|
|
|
19 |
try:
|
20 |
result = subprocess.run(
|
21 |
-
["yt-dlp", "-f", "bestaudio", "--get-url",
|
22 |
capture_output=True, text=True
|
23 |
)
|
24 |
-
|
25 |
-
if not
|
26 |
raise HTTPException(status_code=404, detail="No audio URL found.")
|
27 |
-
return RedirectResponse(
|
28 |
except Exception as e:
|
29 |
raise HTTPException(status_code=500, detail=f"Error: {str(e)}")
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException, Query
|
2 |
from fastapi.responses import RedirectResponse
|
3 |
import subprocess
|
4 |
|
5 |
app = FastAPI(
|
6 |
title="YouTube Audio Streamer",
|
7 |
description="Stream best audio of a YouTube video using yt-dlp.",
|
8 |
+
version="1.0.1",
|
9 |
docs_url="/docs",
|
10 |
redoc_url="/redoc"
|
11 |
)
|
|
|
14 |
async def root():
|
15 |
return {"status": "FastAPI working", "message": "YT-DLP backend ready"}
|
16 |
|
17 |
+
@app.get("/stream", tags=["Stream"])
|
18 |
+
async def stream_audio(url: str = Query(..., description="Full YouTube video URL")):
|
19 |
+
if "youtube.com/watch?v=" not in url and "youtu.be/" not in url:
|
20 |
+
raise HTTPException(status_code=400, detail="Invalid YouTube URL.")
|
21 |
+
|
22 |
try:
|
23 |
result = subprocess.run(
|
24 |
+
["yt-dlp", "-f", "bestaudio", "--get-url", url],
|
25 |
capture_output=True, text=True
|
26 |
)
|
27 |
+
audio_url = result.stdout.strip()
|
28 |
+
if not audio_url:
|
29 |
raise HTTPException(status_code=404, detail="No audio URL found.")
|
30 |
+
return RedirectResponse(audio_url)
|
31 |
except Exception as e:
|
32 |
raise HTTPException(status_code=500, detail=f"Error: {str(e)}")
|