File size: 2,508 Bytes
cc21d37 44e25b0 cc21d37 5652790 ccfeb27 cc21d37 b1da0fd cc21d37 46385e7 d3eefa6 46385e7 d3eefa6 46385e7 d3eefa6 44e25b0 46385e7 d3eefa6 46385e7 d3eefa6 46385e7 d3eefa6 46385e7 d3eefa6 46385e7 9eaacbc cc21d37 bbcb424 cc21d37 d3eefa6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
from flask import Flask, request, send_file
import soundfile as sf
import os
import requests
import subprocess
from pathlib import Path
import separate
app = Flask(__name__)
@app.route('/', methods=['GET'])
def hello():
return "Hello! This is an api server, and it is running successfully. For usage, please contact the person who hosted this api server."
@app.route('/api/audio_separation', methods=['GET'])
def audio_separation():
try:
# 获取MP3文件的直链地址
mp3_url = request.args.get('url')
if not mp3_url:
return "Error: URL parameter is required", 400
# 下载MP3文件到本地
response = requests.get(mp3_url)
mp3_filename = mp3_url.split('/')[-1] # 使用下载的文件名
with open("/tmp/" + mp3_filename, 'wb') as f:
f.write(response.content)
# 执行音频分离操作
audio_worker = separate.Predictor(args={
"files": [f"/tmp/{mp3_filename}"],
"output": Path("/tmp"),
"model_path": Path("./models/MDX_Net_Models/UVR-MDX-NET-Inst_HQ_3.onnx"),
"denoise": False,
"margin": 44100,
"chunks": 15,
"n_fft": 6144,
"dim_t": 8,
"dim_f": 2048
})
vocals, no_vocals, sampling_rate = audio_worker.predict("/tmp/" + mp3_filename)
sf.write(os.path.join("/tmp", mp3_filename + "_no_vocals.wav"), no_vocals, sampling_rate)
sf.write(os.path.join("/tmp", mp3_filename + "_vocals.wav"), vocals, sampling_rate)
# subprocess.run(['python', 'separate.py', '/tmp/' + mp3_filename, '-o', '/tmp', '-m', './models/MDX_Net_Models/UVR-MDX-NET-Inst_HQ_3.onnx'])
# 生成分离后的文件名
vocals_filename = f"{os.path.splitext(mp3_filename)[0]}_vocals.wav"
no_vocals_filename = f"{os.path.splitext(mp3_filename)[0]}_no_vocals.wav"
# 提供文件的永久直链
vocals_url = f"/download/{os.path.basename(vocals_filename)}"
no_vocals_url = f"/download/{os.path.basename(no_vocals_filename)}"
# 返回结果
result = {
"vocals_url": vocals_url,
"no_vocals_url": no_vocals_url
}
return result
except Exception as e:
return "Error: " + str(e), 500
@app.route('/download/<filename>', methods=['GET'])
def download(filename):
return send_file("/tmp/" + filename, as_attachment=True)
if __name__ == '__main__':
app.run(debug=False) |