File size: 1,606 Bytes
cc21d37
 
 
 
 
 
 
b1da0fd
 
 
 
cc21d37
 
 
 
 
 
 
 
 
 
c393000
cc21d37
 
 
bbcb424
cc21d37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bbcb424
cc21d37
 
9b39159
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
from flask import Flask, request, send_file
import os
import requests
import subprocess

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():
    # 获取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)

    # 执行音频分离操作
    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

@app.route('/download/<filename>', methods=['GET'])
def download(filename):
    return send_file("/tmp/" + filename, as_attachment=True)

if __name__ == '__main__':
    app.run(debug=False)