Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, send_file
|
| 2 |
+
import os
|
| 3 |
+
import requests
|
| 4 |
+
import subprocess
|
| 5 |
+
|
| 6 |
+
app = Flask(__name__)
|
| 7 |
+
|
| 8 |
+
@app.route('/api/audio_separation', methods=['GET'])
|
| 9 |
+
def audio_separation():
|
| 10 |
+
# 获取MP3文件的直链地址
|
| 11 |
+
mp3_url = request.args.get('url')
|
| 12 |
+
if not mp3_url:
|
| 13 |
+
return "Error: URL parameter is required", 400
|
| 14 |
+
|
| 15 |
+
# 下载MP3文件到本地
|
| 16 |
+
response = requests.get(mp3_url)
|
| 17 |
+
mp3_filename = mp3_url.split('/')[-1] # 使用下载的文件名
|
| 18 |
+
with open(mp3_filename, 'wb') as f:
|
| 19 |
+
f.write(response.content)
|
| 20 |
+
|
| 21 |
+
# 执行音频分离操作
|
| 22 |
+
subprocess.run(['python', 'separate.py', mp3_filename, '-m', './models/MDX_Net_Models/UVR-MDX-NET-Inst_HQ_3.onnx'])
|
| 23 |
+
|
| 24 |
+
# 生成分离后的文件名
|
| 25 |
+
vocals_filename = f"{os.path.splitext(mp3_filename)[0]}_vocals.wav"
|
| 26 |
+
no_vocals_filename = f"{os.path.splitext(mp3_filename)[0]}_no_vocals.wav"
|
| 27 |
+
|
| 28 |
+
# 提供文件的永久直链
|
| 29 |
+
vocals_url = f"/download/{os.path.basename(vocals_filename)}"
|
| 30 |
+
no_vocals_url = f"/download/{os.path.basename(no_vocals_filename)}"
|
| 31 |
+
|
| 32 |
+
# 返回结果
|
| 33 |
+
result = {
|
| 34 |
+
"vocals_url": vocals_url,
|
| 35 |
+
"no_vocals_url": no_vocals_url
|
| 36 |
+
}
|
| 37 |
+
return result
|
| 38 |
+
|
| 39 |
+
@app.route('/download/<filename>', methods=['GET'])
|
| 40 |
+
def download(filename):
|
| 41 |
+
return send_file(filename, as_attachment=True)
|
| 42 |
+
|
| 43 |
+
if __name__ == '__main__':
|
| 44 |
+
app.run(debug=True)
|