[O] Re-encode with opus

This commit is contained in:
2025-11-22 20:45:07 +08:00
parent 57d2b62be8
commit ebe322f950
4 changed files with 35 additions and 6 deletions
+1 -1
View File
@@ -1,2 +1,2 @@
MONGO_URL="mongodb://cat:meow@localhost:27017/" MONGO_URL="mongodb://cat:meow@localhost:27017/"
AUDIO_SEPARATOR_API="http://127.0.0.1:8000" AUDIO_SEPARATOR_API="http://127.0.0.1:24801"
+4
View File
@@ -13,3 +13,7 @@ pip install "audio-separator[cpu]" fastapi uvicorn python-multipart
```bash ```bash
python server.py python server.py
``` ```
## Compute requirements
My hardware is V100 16GB, and the model took ~1.5GB. So probably any potato would work.
+27 -5
View File
@@ -3,6 +3,7 @@ import shutil
import uuid import uuid
import threading import threading
import uvicorn import uvicorn
from subprocess import check_call
from pathlib import Path from pathlib import Path
from fastapi import FastAPI, UploadFile, File, BackgroundTasks, HTTPException from fastapi import FastAPI, UploadFile, File, BackgroundTasks, HTTPException
from fastapi.responses import FileResponse from fastapi.responses import FileResponse
@@ -44,14 +45,24 @@ def process_separation(task_id: str, task_dir: Path, input_path: Path):
logger.info(f"Separating {task_id}...") logger.info(f"Separating {task_id}...")
output_files = sep.separate(str(input_path)) output_files = sep.separate(str(input_path))
logger.info(f"Output files {output_files}")
results = {} results = {}
for file in output_files: for file in output_files:
p = Path(file) p = Path(file)
target_p = task_dir / p.name
if p != target_p:
shutil.move(str(p), str(target_p))
p = target_p
opus_path = p.with_suffix('.opus')
check_call(['ffmpeg', '-y', '-i', str(p), '-c:a', 'libopus', '-b:a', '96k', '-vbr', 'on', str(opus_path)])
p.unlink()
if 'Vocals' in p.name: if 'Vocals' in p.name:
results['vocals'] = str(p) results['vocals'] = str(opus_path)
elif 'Instrumental' in p.name: elif 'Instrumental' in p.name:
results['instrumental'] = str(p) results['instrumental'] = str(opus_path)
jobs[task_id]['results'] = results jobs[task_id]['results'] = results
jobs[task_id]['status'] = 'completed' jobs[task_id]['status'] = 'completed'
@@ -63,7 +74,7 @@ def process_separation(task_id: str, task_dir: Path, input_path: Path):
jobs[task_id]['error'] = str(e) jobs[task_id]['error'] = str(e)
@app.post("/separate") @app.post("/separate")
async def separate(file: UploadFile = File(...), background_tasks: BackgroundTasks): async def separate(background_tasks: BackgroundTasks, file: UploadFile = File(...)):
task_id = str(uuid.uuid4()) task_id = str(uuid.uuid4())
task_dir = TEMP_DIR / task_id task_dir = TEMP_DIR / task_id
task_dir.mkdir(parents=True, exist_ok=True) task_dir.mkdir(parents=True, exist_ok=True)
@@ -97,6 +108,17 @@ async def get_result(task_id: str, stem: str):
return FileResponse(file_path) return FileResponse(file_path)
@app.delete("/task/{task_id}")
async def delete_task(task_id: str):
if task_id in jobs:
del jobs[task_id]
task_dir = TEMP_DIR / task_id
if task_dir.exists():
shutil.rmtree(task_dir)
return {"status": "deleted"}
if __name__ == "__main__": if __name__ == "__main__":
print("Starting Audio Separator API on port 8000...") print("Starting Audio Separator API on port 24801...")
uvicorn.run(app, host="127.0.0.1", port=8000) uvicorn.run(app, host="0.0.0.0", port=24801)
+3
View File
@@ -0,0 +1,3 @@
import { checkAudioSeparator } from '$lib/server/separator';
checkAudioSeparator().catch(e => console.error('Audio separator check failed:', e));