Spaces:
Sleeping
Sleeping
Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File
|
| 2 |
+
from fastapi.responses import FileResponse
|
| 3 |
+
import subprocess, uuid, os, shutil
|
| 4 |
+
|
| 5 |
+
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
TMP = "/tmp/audio"
|
| 8 |
+
os.makedirs(TMP, exist_ok=True)
|
| 9 |
+
|
| 10 |
+
FILTER = (
|
| 11 |
+
"asetrate=44100*0.85,"
|
| 12 |
+
"atempo=1/0.85,"
|
| 13 |
+
"aresample=44100,"
|
| 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]
|
| 20 |
+
inp = os.path.join(TMP, f"{uuid.uuid4()}{ext}")
|
| 21 |
+
out = os.path.join(TMP, f"{uuid.uuid4()}_sr.mp3")
|
| 22 |
+
|
| 23 |
+
with open(inp, "wb") as f:
|
| 24 |
+
shutil.copyfileobj(file.file, f)
|
| 25 |
+
|
| 26 |
+
cmd = [
|
| 27 |
+
"ffmpeg", "-y", "-i", inp,
|
| 28 |
+
"-af", FILTER,
|
| 29 |
+
"-vn", "-acodec", "libmp3lame", "-ab", "192k",
|
| 30 |
+
out
|
| 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")
|