Skip to content

Commit

Permalink
add api support
Browse files Browse the repository at this point in the history
  • Loading branch information
jianchang512 committed Jan 11, 2024
1 parent 5806544 commit 5d11a4c
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 14 deletions.
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[English README](./README_EN.md) / [Discord](https://discord.gg/TMCM2PfHzQ)
[English README](./README_EN.md) / [👑捐助该项目](https://github.com/jianchang512/pyvideotrans/blob/main/about.md) / [Discord](https://discord.gg/TMCM2PfHzQ) / [Q群 902124277](https://qm.qq.com/cgi-bin/qm/qr?k=9VNSiJyInxyQ3HK3vmOTibo8pzcDekju&jump_from=webapi&authKey=xcW1+7N1E4SM+DXqyl5d61UOtNooA1C9WR5A/yfA0fmfyUDRRSZex1WD0l+9x1kO) <img src="https://github.com/jianchang512/clone-voice/assets/3378335/20858f50-6d47-411d-939b-272fe35e474c" width="50" title="点击看大图">

# 音乐人声分离工具

Expand Down Expand Up @@ -59,6 +59,43 @@ https://github.com/jianchang512/vocal-separate/assets/3378335/8e6b1b20-70d4-45e3
7. 执行 `python start.py `,等待自动打开本地浏览器窗口。


# API 接口

接口地址: http://127.0.0.1:9999/api

请求方法: POST

请求参数:

file: 要分离的音视频文件

model: 模型名称 2stems,4stems,5stems

返回响应: json
code:int, 0 处理成功完成,>0 出错

msg:str, 出错时填充错误信息

data: List[str], 每个分离后的wav url地址,例如 ['http://127.0.0.1:9999/static/files/2/accompaniment.wav']

status_text: dict[str,str], 每个分离后wav文件的包含信息,{'accompaniment': '伴奏', 'bass': '低音', 'drums': '鼓', 'other': '其他', 'piano': '琴', 'vocals': '人声'}

```
import requests
# 请求地址
url = "http://127.0.0.1:9999/api"
files = {"file": open("C:\\Users\\c1\\Videos\\2.wav", "rb")}
data={"model":"2stems"}
response = requests.request("POST", url, timeout=600, data=data,files=files)
print(response.json())
{'code': 0, 'data': ['http://127.0.0.1:9999/static/files/2/accompaniment.wav', 'http://127.0.0.1:9999/static/files/2/vocals.wav'], 'msg': '分离成功
', 'status_text': {'accompaniment': '伴奏', 'bass': '低音', 'drums': '鼓', 'other': '其他', 'piano': '琴', 'vocals': '人声'}}
```



# CUDA 加速支持

Expand Down
39 changes: 38 additions & 1 deletion README_EN.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[English README](./README_EN.md) / [Discord](https://discord.gg/TMCM2PfHzQ) / QQ群 902124277
[English README](./README_EN.md) / [👑Donate project](https://github.com/jianchang512/pyvideotrans/blob/main/about.md) / [Discord](https://discord.gg/TMCM2PfHzQ)

# Music Vocal Separation Tool

Expand Down Expand Up @@ -60,6 +60,43 @@ https://github.com/jianchang512/vocal-separate/assets/3378335/fd377ed4-ae76-43d6
6. Execute `python start.py`, and wait for the local browser window to open automatically.


# API

api url: http://127.0.0.1:9999/api

Method: POST

Request params:

file: audio file

model: model name, 2stems,4stems,5stems

Response: json
code:int, 0 succeed,>0 is error

msg:str, error infomation

data: List[str], all wav separate result, eg. ['http://127.0.0.1:9999/static/files/2/accompaniment.wav']

status_text: dict[str,str], every wav name, {'accompaniment.wav': 'accompaniment audio', 'bass.wav': 'bass audio', 'drums.wav': 'drums audio', 'other.wav': 'other audio', 'piano.wav': 'piano audio', 'vocals.wav': 'vocals audio'}

```
import requests
# api url
url = "http://127.0.0.1:9999/api"
files = {"file": open("C:\\Users\\c1\\Videos\\2.wav", "rb")}
data={"model":"2stems"}
response = requests.request("POST", url, timeout=600, data=data,files=files)
print(response.json())
{'code': 0, 'data': ['http://127.0.0.1:9999/static/files/2/accompaniment.wav', 'http://127.0.0.1:9999/static/files/2/vocals.wav'], 'msg': 'ok
', 'status_text': {'accompaniment.wav': 'accompaniment', 'bass.wav': 'bass', 'drums.wav': 'drums', 'other.wav': 'other', 'piano.wav': 'piano', 'vocals.wav': 'vocals'}}
```



# CUDA Acceleration Support

Expand Down
81 changes: 77 additions & 4 deletions start.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import logging
import threading
import sys
from flask import Flask, request, render_template, jsonify, send_from_directory
import os
from gevent.pywsgi import WSGIServer, WSGIHandler,LoggingLogAdapter
from gevent.pywsgi import WSGIServer, WSGIHandler
from logging.handlers import RotatingFileHandler

import vocal
from vocal import cfg, tool
from vocal.cfg import ROOT_DIR
import subprocess
Expand Down Expand Up @@ -44,7 +45,7 @@ def static_files(filename):

@app.route('/')
def index():
return render_template("index.html",cuda=cfg.cuda, language=cfg.LANG,root_dir=ROOT_DIR.replace('\\', '/'))
return render_template("index.html",version=vocal.version_str,cuda=cfg.cuda, language=cfg.LANG,root_dir=ROOT_DIR.replace('\\', '/'))


# 上传音频
Expand Down Expand Up @@ -79,7 +80,7 @@ def upload():
audio_file.save(wav_file)
else:
return jsonify({"code": 1, "msg": f"{cfg.transobj['lang3']} {ext}"})

# 返回成功的响应
return jsonify({'code': 0, 'msg': cfg.transobj['lang1']+msg, "data": os.path.basename(wav_file)})
except Exception as e:
Expand Down Expand Up @@ -134,6 +135,78 @@ def process():
return jsonify({"code": 0, "msg": cfg.transobj['lang6'], "data": data, "urllist": urllist,"dirname":dirname})


@app.route('/api',methods=['POST'])
def api():
try:
# 获取上传的文件
audio_file = request.files['file']
model = request.form.get("model")
# 如果是mp4
noextname, ext = os.path.splitext(audio_file.filename)
ext = ext.lower()
# 如果是视频,先分离
wav_file = os.path.join(cfg.TMP_DIR, f'{noextname}.wav')
if not os.path.exists(wav_file) or os.path.getsize(wav_file) == 0:
if ext in ['.mp4', '.mov', '.avi', '.mkv', '.mpeg', '.mp3', '.flac']:
video_file = os.path.join(cfg.TMP_DIR, f'{noextname}{ext}')
audio_file.save(video_file)
params = [
"-i",
video_file,
]
if ext not in ['.mp3', '.flac']:
params.append('-vn')
params.append(wav_file)
rs = tool.runffmpeg(params)
if rs != 'ok':
return jsonify({"code": 1, "msg": rs})
elif ext == '.wav':
audio_file.save(wav_file)
else:
return jsonify({"code": 1, "msg": f"{cfg.transobj['lang3']} {ext}"})

# 返回成功的响应
if not os.path.exists(wav_file):
return jsonify({"code": 1, "msg": f"{wav_file} {cfg.langlist['lang5']}"})
if not os.path.exists(os.path.join(cfg.MODEL_DIR, model, 'model.meta')):
return jsonify({"code": 1, "msg": f"{model} {cfg.transobj['lang4']}"})
try:
p = subprocess.run(['ffprobe', '-v', 'error', '-show_entries', "format=duration", '-of',
"default=noprint_wrappers=1:nokey=1", wav_file], capture_output=True)
if p.returncode == 0:
sec = float(p.stdout)
except:
sec = 1800
print(f'{sec=}')
separator = Separator(f'spleeter:{model}', multiprocess=False)
dirname = os.path.join(cfg.FILES_DIR, noextname)
try:
separator.separate_to_file(wav_file, destination=dirname, filename_format="{instrument}.{codec}",
duration=sec)
except Exception as e:
return jsonify({"code": 1, "msg": str(e)})
status = {
"accompaniment.wav":"accompaniment audio" if cfg.LANG=='en' else "伴奏",
"bass.wav": "bass audio" if cfg.LANG=='en' else"低音",
"drums.wav": "drums audio" if cfg.LANG=='en' else"鼓",
"piano.wav": "piano audio" if cfg.LANG=='en' else"琴",
"vocals.wav": "vocals audio" if cfg.LANG=='en' else"人声",
"other.wav": "other audio" if cfg.LANG=='en' else"其他"
}
# data = []
urllist = []
for it in os.listdir(dirname):
if it.endswith('.wav'):
urllist.append(f'http://{cfg.web_address}/static/files/{noextname}/{it}')

return jsonify({"code": 0, "msg": cfg.transobj['lang6'], "data": urllist,"status_text":status})
except Exception as e:
app.logger.error(f'[upload]error: {e}')
return jsonify({'code': 2, 'msg': cfg.transobj['lang2']})




@app.route('/checkupdate', methods=['GET', 'POST'])
def checkupdate():
return jsonify({'code': 0, "msg": cfg.updatetips})
Expand Down
Binary file removed static/tmp/2.flac
Binary file not shown.
6 changes: 3 additions & 3 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
<body>
<div class="layui-layout layui-layout-admin">
<div class="layui-header">
<div class="layui-logo layui-hide-xs" data-en="Vocal Separate" data-cn="人声分离"></div>
<div class="layui-logo layui-hide-xs" data-en="Vocal Separate v{{ version }}" data-cn="人声分离 v{{ version }}"></div>
<!-- 头部区域(可配合layui 已有的水平导航) -->
<ul class="layui-nav layui-layout-right">
<!-- 移动端显示 -->
Expand All @@ -87,8 +87,8 @@
target="_blank">Github</a></li>
<li class="layui-nav-item layui-hide-xs"><a href="https://github.com/jianchang512/vocal-separate/issue"
target="_blank" data-en="Post Isue" data-cn="遇到问题?"></a></li>
<li class="layui-nav-item layui-hide-xs"><a href="https://discord.gg/TMCM2PfHzQ" target="_blank">Discord</a>
</li>
<li class="layui-nav-item layui-hide-xs"><a href="https://discord.gg/TMCM2PfHzQ" target="_blank">Discord</a></li>
<li class="layui-nav-item layui-hide-xs" ><a class="layui-font-green" data-en="Donate the project" data-cn="捐助该项目" href="https://github.com/jianchang512/pyvideotrans/blob/main/about.md" target="_blank"></a></li>
</ul>
</div>
<div id="content">
Expand Down
13 changes: 12 additions & 1 deletion test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
# cli 示例
from spleeter.separator import Separator
import os
separator = Separator('spleeter:2stems', multiprocess=False)
noextname="1"
dirname=os.path.join("./static/files",noextname)

separator.separate_to_file("./lao59.mp3", destination=dirname, filename_format="{instrument}.{codec}")
a_name = f"{dirname}/{noextname}vocals.wav"
a_name = f"{dirname}/{noextname}vocals.wav"

# api 请求示例

import requests
# 请求地址
url = "http://127.0.0.1:9999/api"
files = {"file": open("C:\\Users\\c1\\Videos\\2.wav", "rb")}
data={"model":"2stems"}
response = requests.request("POST", url, timeout=600, data=data,files=files)
print(response.json())
4 changes: 2 additions & 2 deletions version.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version":"v0.0.3",
"version_num":3
"version":"v0.0.4",
"version_num":4
}
3 changes: 2 additions & 1 deletion vocal/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
VERSION=3
VERSION=4
version_str='0.0.4'
Binary file modified vocal/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file modified vocal/__pycache__/cfg.cpython-310.pyc
Binary file not shown.
Binary file modified vocal/__pycache__/tool.cpython-310.pyc
Binary file not shown.
3 changes: 2 additions & 1 deletion vocal/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def checkupdate():
if d['version_num']>vocal.VERSION:
cfg.updatetips=f'New version {d["version"]}'
except Exception as e:
pass
print(e)


def openweb(web_address):
webbrowser.open("http://"+web_address)
Expand Down

0 comments on commit 5d11a4c

Please sign in to comment.