[O] Re-encode with opus
This commit is contained in:
+27
-5
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user