Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
-
from fastapi import FastAPI, UploadFile, File
|
| 2 |
-
from fastapi.responses import FileResponse
|
| 3 |
-
import subprocess, uuid, os, shutil
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
|
@@ -14,6 +14,39 @@ FILTER = (
|
|
| 14 |
"aecho=0.8:0.9:1200:0.4"
|
| 15 |
)
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
@app.post("/slowed-reverb")
|
| 18 |
async def slowed_reverb(file: UploadFile = File(...)):
|
| 19 |
ext = os.path.splitext(file.filename)[1]
|
|
@@ -23,13 +56,23 @@ async def slowed_reverb(file: UploadFile = File(...)):
|
|
| 23 |
with open(inp, "wb") as f:
|
| 24 |
shutil.copyfileobj(file.file, f)
|
| 25 |
|
| 26 |
-
cmd = [
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
|
|
|
| 33 |
subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
| 34 |
|
| 35 |
return FileResponse(out, media_type="audio/mpeg", filename="slowed_reverb.mp3")
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File, Form
|
| 2 |
+
from fastapi.responses import HTMLResponse, FileResponse
|
| 3 |
+
import subprocess, uuid, os, shutil, requests
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
|
|
|
| 14 |
"aecho=0.8:0.9:1200:0.4"
|
| 15 |
)
|
| 16 |
|
| 17 |
+
HTML = """
|
| 18 |
+
<!DOCTYPE html>
|
| 19 |
+
<html>
|
| 20 |
+
<head>
|
| 21 |
+
<title>Slowed + Reverb</title>
|
| 22 |
+
<style>
|
| 23 |
+
body { background:#111; color:#fff; font-family:sans-serif; text-align:center; padding:40px }
|
| 24 |
+
input, button { padding:10px; margin:10px; }
|
| 25 |
+
</style>
|
| 26 |
+
</head>
|
| 27 |
+
<body>
|
| 28 |
+
<h1>🎧 Slowed + Reverb</h1>
|
| 29 |
+
|
| 30 |
+
<form action="/slowed-reverb" method="post" enctype="multipart/form-data">
|
| 31 |
+
<h3>Upload file</h3>
|
| 32 |
+
<input type="file" name="file">
|
| 33 |
+
<button type="submit">Process</button>
|
| 34 |
+
</form>
|
| 35 |
+
|
| 36 |
+
<h3>OR</h3>
|
| 37 |
+
|
| 38 |
+
<form action="/slowed-reverb-url" method="post">
|
| 39 |
+
<input type="text" name="url" placeholder="Direct audio URL" size="50">
|
| 40 |
+
<button type="submit">Process</button>
|
| 41 |
+
</form>
|
| 42 |
+
</body>
|
| 43 |
+
</html>
|
| 44 |
+
"""
|
| 45 |
+
|
| 46 |
+
@app.get("/", response_class=HTMLResponse)
|
| 47 |
+
def home():
|
| 48 |
+
return HTML
|
| 49 |
+
|
| 50 |
@app.post("/slowed-reverb")
|
| 51 |
async def slowed_reverb(file: UploadFile = File(...)):
|
| 52 |
ext = os.path.splitext(file.filename)[1]
|
|
|
|
| 56 |
with open(inp, "wb") as f:
|
| 57 |
shutil.copyfileobj(file.file, f)
|
| 58 |
|
| 59 |
+
cmd = ["ffmpeg", "-y", "-i", inp, "-af", FILTER, out]
|
| 60 |
+
subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
| 61 |
+
|
| 62 |
+
return FileResponse(out, media_type="audio/mpeg", filename="slowed_reverb.mp3")
|
| 63 |
+
|
| 64 |
+
@app.post("/slowed-reverb-url")
|
| 65 |
+
def slowed_reverb_url(url: str = Form(...)):
|
| 66 |
+
ext = os.path.splitext(url)[1] or ".mp3"
|
| 67 |
+
inp = os.path.join(TMP, f"{uuid.uuid4()}{ext}")
|
| 68 |
+
out = os.path.join(TMP, f"{uuid.uuid4()}_sr.mp3")
|
| 69 |
+
|
| 70 |
+
r = requests.get(url, stream=True, timeout=20)
|
| 71 |
+
with open(inp, "wb") as f:
|
| 72 |
+
for chunk in r.iter_content(1024):
|
| 73 |
+
f.write(chunk)
|
| 74 |
|
| 75 |
+
cmd = ["ffmpeg", "-y", "-i", inp, "-af", FILTER, out]
|
| 76 |
subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
| 77 |
|
| 78 |
return FileResponse(out, media_type="audio/mpeg", filename="slowed_reverb.mp3")
|