forked from v3ucn/DH_live_webui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
257 lines (168 loc) · 6.01 KB
/
Copy pathapi.py
File metadata and controls
257 lines (168 loc) · 6.01 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import time
import io, os, sys
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
from pydub import AudioSegment
import numpy as np
from flask import Flask, request, Response,send_from_directory,render_template
import torch
import torchaudio
import ffmpeg
from flask_cors import CORS
from flask import make_response,redirect
import json
import platform
from subprocess import Popen
import subprocess
import edge_tts
import asyncio
ps1a=[]
system=platform.system()
def kill_process(pid):
if(system=="Windows"):
cmd = "taskkill /t /f /pid %s" % pid
os.system(cmd)
else:
kill_proc_tree(pid)
def convert_wav(input_file):
# 加载音频文件
audio = AudioSegment.from_wav(input_file)
# 转换为单声道
audio = audio.set_channels(1)
# 设置采样率为16kHz
audio = audio.set_frame_rate(16000)
# 设置采样位数为16位
audio = audio.set_sample_width(2) # 2 bytes = 16 bits
# 导出处理后的音频
audio.export("new.wav", format="wav")
return "new.wav"
app = Flask(__name__,static_folder='static', static_url_path='/static')
CORS(app, cors_allowed_origins="*")
CORS(app, supports_credentials=True)
@app.route("/test", methods=['GET'])
def sft_test():
return {"msg": "ok"}, 200
@app.route("/", methods=['POST'])
def sft_post():
question_data = request.get_json()
face = question_data.get('face')
wav = question_data.get('wav')
if not face:
return {"error": "face不能为空"}, 400
if not wav:
return {"error": "wav不能为空"}, 400
convert_wav(wav)
cmd = fr".\py311\python.exe demo.py video_data/{face} new.wav file/1.mp4"
print(cmd)
res = subprocess.Popen(cmd)
res.wait()
# return {"video":"/file/1.mp4"}, 200
# 读取视频文件,使用流式传输
def generate():
with open("file/1.mp4", "rb") as f:
while True:
chunk = f.read(4096) # 读取4KB数据
if not chunk:
break
yield chunk
# 返回视频数据
return Response(generate(), mimetype="video/mp4",content_type='video/mp4', direct_passthrough=True)
@app.route("/push_talk", methods=['GET'])
def push_talk():
global ps1a
for p_asr in ps1a:
kill_process(p_asr.pid)
face = request.args.get('face')
# if not face_video:
# return {"error": "face不能为空"}, 400
# cmd = fr"ffmpeg -stream_loop -1 -i quiet_output/gakki_quiet.mp4 -vcodec copy -acodec copy -f flv -y rtmp://localhost/live/livestream"
# print(cmd)
# res = subprocess.Popen(cmd)
# res.wait()
cmd = f"ffmpeg -stream_loop -1 -i {face} -vf scale=350:640 -b:v 200k -vcodec libx264 -acodec copy -f flv -y rtmp://localhost/live/livestream"
p = Popen(cmd, shell=True)
p.wait()
cmd = f"ffmpeg -stream_loop -1 -i quiet_output/gakki.mp4 -vf scale=350:640 -b:v 200k -vcodec libx264 -acodec copy -f flv -y rtmp://localhost/live/livestream"
p = Popen(cmd, shell=True)
ps1a.append(p)
return {"msg": "ok"}, 200
@app.route("/push_quiet", methods=['GET'])
def push_quiet():
global ps1a
for p_asr in ps1a:
kill_process(p_asr.pid)
face = request.args.get('face')
# if not face_video:
# return {"error": "face不能为空"}, 400
# cmd = fr"ffmpeg -stream_loop -1 -i quiet_output/gakki_quiet.mp4 -vcodec copy -acodec copy -f flv -y rtmp://localhost/live/livestream"
# print(cmd)
# res = subprocess.Popen(cmd)
# res.wait()
cmd = f"ffmpeg -stream_loop -1 -i quiet_output/{face} -vf scale=350:640 -b:v 200k -vcodec libx264 -acodec copy -f flv -y rtmp://localhost/live/livestream"
p = Popen(cmd, shell=True)
ps1a.append(p)
return {"msg": "ok"}, 200
async def amain(TEXT) -> None:
"""Main function"""
communicate = edge_tts.Communicate(TEXT, "zh-CN-XiaoxiaoNeural")
await communicate.save("edge_tts.wav")
@app.route("/make_video", methods=['GET'])
def make_video():
face = request.args.get('face')
text = request.args.get('text')
asyncio.run(amain(text))
# convert_wav("edge_tts.wav")
cmd = fr"ffmpeg -i edge_tts.wav -acodec pcm_s16le -ac 1 -ar 16000 -y new.wav"
print(cmd)
res = subprocess.Popen(cmd)
res.wait()
cmd = fr".\py311\python.exe demo.py video_data/{face} new.wav static/1.mp4"
print(cmd)
res = subprocess.Popen(cmd)
res.wait()
# if not face_video:
# return {"error": "face不能为空"}, 400
# cmd = fr"ffmpeg -stream_loop -1 -i quiet_output/gakki_quiet.mp4 -vcodec copy -acodec copy -f flv -y rtmp://localhost/live/livestream"
# print(cmd)
# res = subprocess.Popen(cmd)
# res.wait()
return {"msg": "ok"}, 200
@app.route("/", methods=['GET'])
def sft_get():
face = request.args.get('face')
wav = request.args.get('wav')
if not face:
return {"error": "face不能为空"}, 400
if not wav:
return {"error": "wav不能为空"}, 400
convert_wav(wav)
cmd = fr".\py311\python.exe demo.py video_data/{face} new.wav file/1.mp4"
print(cmd)
res = subprocess.Popen(cmd)
res.wait()
# return redirect("/file/1.mp4")
# 读取视频文件,使用流式传输
def generate():
with open("file/1.mp4", "rb") as f:
while True:
chunk = f.read(4096) # 读取4KB数据
if not chunk:
break
yield chunk
# 返回视频数据
return Response(generate(), mimetype="video/mp4")
# 网页模版
@app.route('/player')
def player():
return render_template('player.html')
@app.route('/video')
def video():
return render_template('video.html')
@app.route('/testhtml')
def testhtml():
return render_template('test.html')
@app.route('/file/<filename>')
def uploaded_file(filename):
return send_from_directory("file", filename)
if __name__ == "__main__":
print("实时数字人请访问 http://localhost:9880/video ")
app.run(host='0.0.0.0',port=9880,debug=True)