Skip to content

Commit

Permalink
新增web字幕打印机的接入
Browse files Browse the repository at this point in the history
  • Loading branch information
Ikaros-521 committed Aug 27, 2023
1 parent 7ccc327 commit efbcae6
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 11 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,10 @@ out_audio, out_sr = svc_model.infer(spk, tran, raw_path)
out_audio, out_sr, n_frames = svc_model.infer(spk, tran, raw_path)
```

### web字幕打印机
项目地址:[captions_printer](https://github.com/Ikaros-521/captions_printer)
具体使用参考项目文档即可。

### 聊天模式 百度
新用户+实名认证后有半年的免费额度可以使用。
申请地址:[https://console.bce.baidu.com/ai/#/ai/speech/overview/index](https://console.bce.baidu.com/ai/#/ai/speech/overview/index)
Expand Down Expand Up @@ -1486,6 +1490,10 @@ cmd运行`npm i docsify-cli -g`
- 2023-08-24
- audio合成 音频合成部分新增合成内容的日志,方便后期合成音频时的bug定位

- 2023-08-27
- 新增web字幕打印机的接入


</details>


Expand Down
11 changes: 10 additions & 1 deletion UI_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def setupUi(self, MainWindow):
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName("scrollArea")
self.scrollAreaWidgetContents = QtWidgets.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 984, 9496))
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, -8830, 984, 9572))
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
self.verticalLayout = QtWidgets.QVBoxLayout(self.scrollAreaWidgetContents)
self.verticalLayout.setContentsMargins(35, 20, 35, 20)
Expand Down Expand Up @@ -1419,6 +1419,14 @@ def setupUi(self, MainWindow):
self.gridLayout_19.addWidget(self.lineEdit_header_useragent, 0, 1, 1, 1)
self.gridLayout_18.addLayout(self.gridLayout_19, 0, 1, 1, 1)
self.verticalLayout.addWidget(self.groupBox_header)
self.groupBox_web_captions_printer = QtWidgets.QGroupBox(self.scrollAreaWidgetContents)
self.groupBox_web_captions_printer.setObjectName("groupBox_web_captions_printer")
self.gridLayout_69 = QtWidgets.QGridLayout(self.groupBox_web_captions_printer)
self.gridLayout_69.setObjectName("gridLayout_69")
self.gridLayout_web_captions_printer = QtWidgets.QGridLayout()
self.gridLayout_web_captions_printer.setObjectName("gridLayout_web_captions_printer")
self.gridLayout_69.addLayout(self.gridLayout_web_captions_printer, 0, 0, 1, 1)
self.verticalLayout.addWidget(self.groupBox_web_captions_printer)
self.groupBox_database = QtWidgets.QGroupBox(self.scrollAreaWidgetContents)
self.groupBox_database.setObjectName("groupBox_database")
self.gridLayout_67 = QtWidgets.QGridLayout(self.groupBox_database)
Expand Down Expand Up @@ -2101,6 +2109,7 @@ def retranslateUi(self, MainWindow):
self.groupBox_header.setTitle(_translate("MainWindow", "请求头"))
self.label_header_useragent.setText(_translate("MainWindow", "userAgent"))
self.lineEdit_header_useragent.setText(_translate("MainWindow", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.42"))
self.groupBox_web_captions_printer.setTitle(_translate("MainWindow", "web字幕打印机"))
self.groupBox_database.setTitle(_translate("MainWindow", "数据库"))
self.textBrowser.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
Expand Down
7 changes: 6 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"log": true,
"schedule": true,
"database": true,
"play_audio": true
"play_audio": true,
"web_captions_printer": true
},
"read_user_name": {
"enable": true,
Expand Down Expand Up @@ -353,5 +354,9 @@
],
"random_play": true,
"play_interval": 5000
},
"web_captions_printer": {
"enable": true,
"api_ip_port": "http://127.0.0.1:5500"
}
}
7 changes: 6 additions & 1 deletion config.json.bak
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"log": true,
"schedule": true,
"database": true,
"play_audio": true
"play_audio": true,
"web_captions_printer": true
},
"read_user_name": {
"enable": true,
Expand Down Expand Up @@ -350,5 +351,9 @@
],
"random_play": true,
"play_interval": 5000
},
"web_captions_printer": {
"enable": true,
"api_ip_port": "http://127.0.0.1:5500"
}
}
68 changes: 67 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1173,7 +1173,8 @@ def get_box_name_by_key(key):
"log": "日志",
"schedule": "定时任务",
"database": "数据库",
"play_audio": "播放音频"
"play_audio": "播放音频",
"web_captions_printer": "web字幕打印机"
# 可以继续添加其他键和值
}

Expand Down Expand Up @@ -1547,6 +1548,55 @@ def trends_copywriting_gui_create():

trends_copywriting_gui_create()

# web字幕打印机
def web_captions_printer_gui_create():
data_json = []

web_captions_printer_config = config.get("web_captions_printer")
tmp_json = {
"label_text": "启用",
"label_tip": "是否启用web字幕打印机功能(需要先启动web字幕打印机程序才能使用)",
"data": web_captions_printer_config["enable"],
"widget_text": "",
"click_func": "",
"main_obj_name": "web_captions_printer",
"index": 0
}
data_json.append(tmp_json)

tmp_json = {
"label_text": "API地址",
"label_tip": "web字幕打印机的API地址,只需要 http://ip:端口 即可",
"data": web_captions_printer_config["api_ip_port"],
"main_obj_name": "web_captions_printer",
"index": 1
}
data_json.append(tmp_json)

# tmp_json = {
# "label_text": "类型",
# "label_tip": "发送给web字幕打印机内容,可以自定义哪些内容发过去显示",
# "widget_type": "combo_box",
# "combo_data_list": ["弹幕", "回复", "复读", "弹幕+回复", "回复+复读", "弹幕+回复+复读"],
# "data": web_captions_printer_config["type"],
# "main_obj_name": "web_captions_printer",
# "index": 1
# }
# data_json.append(tmp_json)

widgets = self.create_widgets_from_json(data_json)

# 动态添加widget到对应的gridLayout
row = 0
# 分2列,左边就是label说明,右边就是输入框等
for i in range(0, len(widgets), 2):
self.ui.gridLayout_web_captions_printer.addWidget(widgets[i], row, 0)
self.ui.gridLayout_web_captions_printer.addWidget(widgets[i + 1], row, 1)
row += 1

web_captions_printer_gui_create()


# 显隐各板块
self.oncomboBox_chat_type_IndexChanged(chat_type_index)
self.oncomboBox_audio_synthesis_type_IndexChanged(audio_synthesis_type_index)
Expand Down Expand Up @@ -2225,7 +2275,23 @@ def reorganize_trends_copywriting_data(trends_copywriting_data):
# 写回json
config_data["trends_copywriting"]["copywriting"] = reorganize_trends_copywriting_data(trends_copywriting_data)

# web字幕打印机
def reorganize_web_captions_printer_data(web_captions_printer_data):
keys = list(web_captions_printer_data.keys())

tmp_json = {
"enable": web_captions_printer_data[keys[0]],
"api_ip_port": web_captions_printer_data[keys[1]]
# "type": web_captions_printer_data[keys[2]]
}

logging.debug(f"tmp_json={tmp_json}")

return tmp_json

web_captions_printer_data = self.update_data_from_gridLayout(self.ui.gridLayout_web_captions_printer)
# 写回json
config_data["web_captions_printer"] = reorganize_web_captions_printer_data(web_captions_printer_data)

# 获取自定义板块显隐的数据
show_box_data = self.update_data_from_gridLayout(self.ui.gridLayout_show_box, "show_box")
Expand Down
16 changes: 14 additions & 2 deletions ui/main.ui
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ background-color: rgba(255, 255, 255, 50);
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<y>-8830</y>
<width>984</width>
<height>9496</height>
<height>9572</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
Expand Down Expand Up @@ -2614,6 +2614,18 @@ background-color: rgba(255, 255, 255, 50);
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_web_captions_printer">
<property name="title">
<string>web字幕打印机</string>
</property>
<layout class="QGridLayout" name="gridLayout_69">
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout_web_captions_printer"/>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_database">
<property name="title">
Expand Down
4 changes: 4 additions & 0 deletions utils/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,10 @@ async def voice_change_and_put_to_queue(message, voice_tmp_path):
if self.config.get("play_audio", "enable"):
self.voice_tmp_path_queue.put(data_json)

# 判断是否发送web字幕打印机
if self.config.get("web_captions_printer", "enable"):
self.common.send_to_web_captions_printer(self.config.get("web_captions_printer", "api_ip_port"), data_json)

# 区分TTS类型
if message["tts_type"] == "vits":
try:
Expand Down
38 changes: 37 additions & 1 deletion utils/common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# 导入所需的库
import re, random
import re, random, requests, json
import time
import os
import logging
Expand Down Expand Up @@ -516,3 +516,39 @@ def random_search_a_audio_file(self, root_dir):
return random.choice(audio_files)
else:
return None


# 请求web字幕打印机
def send_to_web_captions_printer(self, api_ip_port, data):
"""请求web字幕打印机
Args:
api_ip_port (str): api请求地址
data (dict): 包含用户名,弹幕内容
Returns:
bool: True/False
"""

# user_name = data["username"]
content = data["content"]

# 记录数据库):
try:
response = requests.get(url=api_ip_port + f'/send_message?content={content}')
response.raise_for_status() # 检查响应的状态码

result = response.content
ret = json.loads(result)

logging.debug(ret)

if ret['code'] == 200:
logging.debug(ret['message'])
return True
else:
logging.error(ret['message'])
return False
except Exception as e:
logging.info(e)
return False
8 changes: 4 additions & 4 deletions utils/my_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ def local_qa_handle(self, data):
"user_name": user_name,
"content": resp_content
}

# 音频合成(edge-tts / vits_fast)并播放
My_handle.audio.audio_synthesis(message)

Expand Down Expand Up @@ -508,7 +508,7 @@ def choose_song_handle(self, data):
"user_name": user_name,
"content": resp_content
}

# 音频合成(edge-tts / vits_fast)并播放
My_handle.audio.audio_synthesis(message)

Expand All @@ -534,7 +534,7 @@ def choose_song_handle(self, data):
"user_name": user_name,
"content": resp_content
}

# 音频合成(edge-tts / vits_fast)并播放
My_handle.audio.audio_synthesis(message)

Expand Down Expand Up @@ -760,7 +760,6 @@ def reread_handle(self, data):
"content": content
}

# 音频合成(edge-tts / vits_fast)并播放
My_handle.audio.audio_synthesis(message)


Expand Down Expand Up @@ -817,6 +816,7 @@ def comment_handle(self, data):
# 用户弹幕违禁判断
if self.prohibitions_handle(content):
return


"""
根据聊天类型执行不同逻辑
Expand Down

0 comments on commit efbcae6

Please sign in to comment.