From ebe322f9504c89d380bcdbf0bb17bd8b275cf6c4 Mon Sep 17 00:00:00 2001 From: Azalea <22280294+hykilpikonna@users.noreply.github.com> Date: Sat, 22 Nov 2025 20:45:07 +0800 Subject: [PATCH] [O] Re-encode with opus --- .env.example | 2 +- scripts/README.md | 4 ++++ scripts/server.py | 32 +++++++++++++++++++++++++++----- src/hooks.server.ts | 3 +++ 4 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 src/hooks.server.ts diff --git a/.env.example b/.env.example index 9571a99..d519ff8 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1,2 @@ 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" diff --git a/scripts/README.md b/scripts/README.md index 3974d3a..3b72b08 100644 --- a/scripts/README.md +++ b/scripts/README.md @@ -13,3 +13,7 @@ pip install "audio-separator[cpu]" fastapi uvicorn python-multipart ```bash python server.py ``` + +## Compute requirements + +My hardware is V100 16GB, and the model took ~1.5GB. So probably any potato would work. \ No newline at end of file diff --git a/scripts/server.py b/scripts/server.py index 30cf1e8..e8a8c6e 100644 --- a/scripts/server.py +++ b/scripts/server.py @@ -3,6 +3,7 @@ import shutil import uuid import threading import uvicorn +from subprocess import check_call from pathlib import Path from fastapi import FastAPI, UploadFile, File, BackgroundTasks, HTTPException 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}...") output_files = sep.separate(str(input_path)) + logger.info(f"Output files {output_files}") results = {} for file in output_files: 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: - results['vocals'] = str(p) + results['vocals'] = str(opus_path) elif 'Instrumental' in p.name: - results['instrumental'] = str(p) + results['instrumental'] = str(opus_path) jobs[task_id]['results'] = results 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) @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_dir = TEMP_DIR / task_id 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) +@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__": - print("Starting Audio Separator API on port 8000...") - uvicorn.run(app, host="127.0.0.1", port=8000) + print("Starting Audio Separator API on port 24801...") + uvicorn.run(app, host="0.0.0.0", port=24801) diff --git a/src/hooks.server.ts b/src/hooks.server.ts new file mode 100644 index 0000000..d795584 --- /dev/null +++ b/src/hooks.server.ts @@ -0,0 +1,3 @@ +import { checkAudioSeparator } from '$lib/server/separator'; + +checkAudioSeparator().catch(e => console.error('Audio separator check failed:', e));