Skip to content

Commit

Permalink
Feat: add glossary when translate use AI
Browse files Browse the repository at this point in the history
  • Loading branch information
jianchang512 committed Jan 10, 2025
1 parent d472822 commit a8a8d48
Show file tree
Hide file tree
Showing 15 changed files with 358 additions and 150 deletions.
2 changes: 1 addition & 1 deletion sp.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
License: GPL-V3
# 代码是一坨屎,但又不是不能跑O(∩_∩)O~
# 代码越写越是坨屎,好烦
#
"""
import multiprocessing
Expand Down
4 changes: 2 additions & 2 deletions videotrans/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- coding: utf-8 -*-

VERSION = "v3.43"
VERSION_NUM = 120343
VERSION = "v3.44"
VERSION_NUM = 120344
3 changes: 3 additions & 0 deletions videotrans/glossary.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
美国弹道导弹防御系统=NBMD
中国导弹防御系统=CNMD
开普特感冒药=OTC
89 changes: 0 additions & 89 deletions videotrans/mainwin/_actions_sub.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,93 +96,6 @@ def check_cuda(self, state):
res = False
self.cfg['cuda'] = res

# 简单新手模式
def set_xinshoujandann(self):

self.main.splitter.setSizes([self.main.width, 0])
self.main.action_xinshoujandan.setChecked(True)
self.main.app_mode = 'biaozhun_jd'
self.main.show_tips.setText(config.transobj['xinshoumoshitips'])
self.main.startbtn.setText(config.transobj['kaishichuli'])
self.main.action_biaozhun.setChecked(False)
self.main.action_tiquzimu.setChecked(False)

# 仅保存视频行
self.main.only_video.setChecked(False)
self.main.only_video.hide()
self.main.copysrt_rawvideo.hide()

# 翻译
self.main.translate_type.setCurrentIndex(1)
self.main.label_9.hide()
self.main.translate_type.hide()
self.main.label_2.show()
self.main.source_language.show()
self.main.label_3.show()
self.main.target_language.show()
self.main.label.hide()
self.main.proxy.hide()

# 配音角色
self.main.tts_text.show()
self.main.tts_type.setCurrentIndex(0)
self.main.tts_type.setDisabled(True)
self.main.tts_type.show()
self.main.label_4.show()
self.main.voice_role.show()
self.main.listen_btn.show()
self.main.volume_rate.setDisabled(True)
self.main.volume_rate.show()
self.main.volume_label.show()
self.main.pitch_label.show()
self.main.pitch_rate.setDisabled(True)
self.main.pitch_rate.show()


# 语音识别行
self.main.split_type.setCurrentIndex(0)
self.main.model_name.setCurrentIndex(0)
self.main.reglabel.hide()
self.main.recogn_type.setCurrentIndex(0)
self.main.recogn_type.hide()
self.main.model_name_help.hide()
self.main.model_name.hide()
self.main.split_label.hide()
self.main.split_type.hide()
self.main.subtitle_type.setCurrentIndex(1)
self.main.subtitle_type.hide()
self.main.rephrase.setChecked(False)
self.main.rephrase.hide()
self.main.remove_noise.setChecked(False)
self.main.remove_noise.hide()




# 字幕对齐行
self.main.align_btn.hide()
self.main.label_6.hide()
self.main.voice_rate.hide()
self.main.append_video.setChecked(True)
self.main.append_video.hide()
self.main.voice_autorate.setChecked(True)
self.main.voice_autorate.hide()
self.main.video_autorate.setChecked(True)
self.main.video_autorate.hide()
self.main.is_separate.setChecked(False)
self.main.is_separate.hide()
self.main.enable_cuda.setChecked(False)
self.main.enable_cuda.hide()
self.main.label_cjklinenums.hide()
self.main.cjklinenums.hide()
self.main.label_othlinenums.hide()
self.main.othlinenums.hide()
# 添加背景行
self.main.addbackbtn.hide()
self.main.back_audio.hide()
self.main.is_loop_bgm.hide()
self.main.bgmvolume_label.hide()
self.main.bgmvolume.hide()


# 启用标准模式
Expand All @@ -192,7 +105,6 @@ def set_biaozhun(self):
self.main.app_mode = 'biaozhun'
self.main.show_tips.setText("自定义各项配置,批量进行视频翻译。选择单个视频时,处理过程中可暂停编辑字幕" if config.defaulelang=='zh' else 'Customize each configuration to batch video translation. When selecting a single video, you can pause to edit subtitles during processing.')
self.main.startbtn.setText(config.transobj['kaishichuli'])
self.main.action_xinshoujandan.setChecked(False)
self.main.action_tiquzimu.setChecked(False)

# 仅保存视频行
Expand Down Expand Up @@ -274,7 +186,6 @@ def set_tiquzimu(self):
self.main.app_mode = 'tiqu'
self.main.show_tips.setText(config.transobj['tiquzimu'])
self.main.startbtn.setText(config.transobj['kaishitiquhefanyi'])
self.main.action_xinshoujandan.setChecked(False)
self.main.action_biaozhun.setChecked(False)

# 仅保存视频行
Expand Down
3 changes: 1 addition & 2 deletions videotrans/mainwin/_main_win.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ def initUI(self):
self.model_name.setDisabled(False)

self.moshis = {
"biaozhun_jd": self.action_xinshoujandan,
"biaozhun": self.action_biaozhun,
"tiqu": self.action_tiquzimu
}
Expand Down Expand Up @@ -264,6 +263,7 @@ def _set_cache_set(self):
self.hfaster_help.clicked.connect(lambda :tools.open_url(url='https://pyvideotrans.com/vad'))
self.split_label.clicked.connect(lambda: tools.open_url(url='https://pyvideotrans.com/splitmode'))
self.align_btn.clicked.connect(lambda: tools.open_url(url='https://pyvideotrans.com/align'))
self.glossary.clicked.connect(lambda:tools.show_glossary_editor(self))


def _start_subform(self):
Expand All @@ -281,7 +281,6 @@ def _start_subform(self):

from videotrans import winform

self.action_xinshoujandan.triggered.connect(self.win_action.set_xinshoujandann)
self.action_biaozhun.triggered.connect(self.win_action.set_biaozhun)
self.action_tiquzimu.triggered.connect(self.win_action.set_tiquzimu)

Expand Down
2 changes: 0 additions & 2 deletions videotrans/task/trans_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,8 +734,6 @@ def _novoicemp4_add_time(self, duration_ms):
default_codec = f"libx{config.settings['video_codec']}"
cmd = [
'-y',
"-threads",
f'{os.cpu_count()}',
'-i',
self.cfg['novoice_mp4'],
'-vf',
Expand Down
18 changes: 15 additions & 3 deletions videotrans/translator/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,21 @@ def runsrt(self):


def _refine3_prompt(self):
zh_prompt=Path(config.ROOT_DIR+'/videotrans/prompts/srt/fansi3.txt').read_text(encoding='utf-8')
en_prompt=Path(config.ROOT_DIR+'/videotrans/prompts/srt/fansi3-en.txt').read_text(encoding='utf-8')
return zh_prompt if config.defaulelang=='zh' else en_prompt
glossary=''
if Path(config.ROOT_DIR+'/videotrans/glossary.txt').exists():
glossary=Path(config.ROOT_DIR+'/videotrans/glossary.txt').read_text(encoding='utf-8').strip()
if config.defaulelang=='zh':
prompt=Path(config.ROOT_DIR+'/videotrans/prompts/srt/fansi3.txt').read_text(encoding='utf-8')
glossary_prompt="""## 术语表\n严格按照以下术语表进行翻译,如果句子中出现术语,必须使用对应的翻译,而不能自由翻译:\n| 术语 | 翻译 |\n| --------- | ----- |\n"""
else:
prompt=Path(config.ROOT_DIR+'/videotrans/prompts/srt/fansi3-en.txt').read_text(encoding='utf-8')
glossary_prompt="""## Glossary of terms\nTranslations are made strictly according to the following glossary. If a term appears in a sentence, the corresponding translation must be used, not a free translation:\n| Glossary | Translation |\n| --------- | ----- |\n"""

if glossary:
glossary="\n".join(["|"+it.replace("=",'|')+"|" for it in glossary.split('\n')])
prompt=prompt.replace('<INPUT></INPUT>',f"""{glossary_prompt}{glossary}\n\n<INPUT></INPUT>""")

return prompt

def _set_cache(self, it, res_str):
if not res_str.strip():
Expand Down
2 changes: 2 additions & 0 deletions videotrans/tts/_edgetts.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ def process():
await communicate.stream()
except Exception as e:
config.logger.exception(e, exc_info=True)
if str(e).find('Invalid response status'):
raise Exception('可能被edge限流,请尝试使用或切换代理节点')
print(f"异步合成出错: {e}")
raise
finally:
Expand Down
30 changes: 16 additions & 14 deletions videotrans/ui/en.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,15 @@ def setupUi(self, MainWindow):
self.horizontalLayout_5.addWidget(self.label_3)
self.horizontalLayout_5.addWidget(self.target_language)


self.glossary = QtWidgets.QPushButton(self.layoutWidget)
self.glossary.setMinimumSize(QtCore.QSize(0, 30))
self.glossary.setObjectName("glossary")
self.glossary.setText("glossary" if config.defaulelang!='zh' else '术语表')
self.glossary.setStyleSheet("""background-color:transparent""")
self.glossary.setCursor(Qt.PointingHandCursor)
self.glossary.setToolTip('点击设置和修改术语表' if config.defaulelang=='zh' else 'Click to set up and modify the glossary')

self.label = QtWidgets.QPushButton(self.layoutWidget)
self.label.setMinimumSize(QtCore.QSize(0, 30))
self.label.setObjectName("label")
Expand All @@ -153,6 +162,10 @@ def setupUi(self, MainWindow):
self.proxy.setMinimumSize(QtCore.QSize(0, 30))
self.proxy.setObjectName("proxy")




self.horizontalLayout_5.addWidget(self.glossary)
self.horizontalLayout_5.addWidget(self.label)
self.horizontalLayout_5.addWidget(self.proxy)

Expand Down Expand Up @@ -737,10 +750,7 @@ def setupUi(self, MainWindow):
self.action_biaozhun.setChecked(True)
self.action_biaozhun.setObjectName("action_biaozhun")

self.action_xinshoujandan = QtGui.QAction(MainWindow)
self.action_xinshoujandan.setCheckable(True)
self.action_xinshoujandan.setChecked(False)
self.action_xinshoujandan.setObjectName("action_xinshoujandan")


self.action_yuyinshibie = QtGui.QAction(MainWindow)

Expand Down Expand Up @@ -925,20 +935,14 @@ def setupUi(self, MainWindow):
self.menuBar.addAction(self.menu.menuAction())
self.menuBar.addAction(self.menu_H.menuAction())

self.toolBar.addAction(self.action_xinshoujandan)
self.toolBar.addAction(self.action_biaozhun)
self.toolBar.addAction(self.action_tiquzimu)

self.toolBar.addAction(self.action_yuyinshibie)
self.toolBar.addAction(self.action_fanyi)
self.toolBar.addAction(self.action_yuyinhecheng)
self.toolBar.addAction(self.action_yingyinhebing)
if config.defaulelang=='zh':
self.toolBar.addAction(self.actionvideoandaudio)
self.toolBar.addAction(self.actionvideoandsrt)
self.toolBar.addAction(self.actionsubtitlescover)
self.toolBar.addAction(self.actionformatcover)
self.toolBar.addAction(self.action_subtitleediter)

# 200ms后渲染文字
QTimer.singleShot(50, self.retranslateUi)

Expand Down Expand Up @@ -1050,9 +1054,7 @@ def retranslateUi(self):
self.action_biaozhun.setToolTip(
'批量进行视频翻译,并可按照需求自定义所有配置选项' if config.defaulelang == 'zh' else 'Batch video translation with all configuration options customizable on demand')

self.action_xinshoujandan.setText(config.uilanglist.get("action_xinshoujandan"))
self.action_xinshoujandan.setToolTip(
'按照默认设置,一键将视频从一种语言翻译为另一种语言并嵌入字幕和配音' if config.defaulelang == 'zh' else 'Translate videos from one language to another and embed subtitles and voiceovers in one click.')


self.action_yuyinshibie.setText(config.uilanglist.get("Speech Recognition Text"))
self.action_yuyinshibie.setToolTip(
Expand Down
61 changes: 43 additions & 18 deletions videotrans/ui/fanyi.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,6 @@ def setupUi(self, fanyisrt):
self.fanyi_translate_type.setMinimumSize(QtCore.QSize(100, 30))
self.fanyi_translate_type.setObjectName("fanyi_translate_type")

self.aisendsrt=QtWidgets.QCheckBox()
self.aisendsrt.setText('发送完整字幕' if config.defaulelang=='zh' else 'Send full subtitles')
self.aisendsrt.setToolTip('当使用AI或Google翻译渠道时,可选以完整srt字幕格式发送请求,但可能出现较多空行' if config.defaulelang=='zh' else 'When using AI or Google translation channel, you can translate in srt format, but there may be more empty lines')
self.aisendsrt.setChecked(config.settings.get('aisendsrt'))

self.refine3=QtWidgets.QCheckBox()
self.refine3.setText('三步反思法翻译' if config.defaulelang=='zh' else 'Three Steps to Reflection Translation')
self.refine3.setToolTip('当使用AI翻译渠道,并选中以完整srt字幕格式发送时,可启用三步反思翻译法' if config.defaulelang=='zh' else 'When using the AI translation channel and checking the box to send in full srt subtitle format, the three-step reflective translation method can be enabled')
self.refine3.setChecked(config.settings.get('refine3'))

self.fanyi_model_list = QtWidgets.QComboBox()
self.fanyi_model_list.setMinimumSize(QtCore.QSize(100, 30))
Expand All @@ -60,8 +51,6 @@ def setupUi(self, fanyisrt):


self.horizontalLayout_18.addWidget(self.fanyi_translate_type)
self.horizontalLayout_18.addWidget(self.aisendsrt)
self.horizontalLayout_18.addWidget(self.refine3)
self.horizontalLayout_18.addWidget(self.fanyi_model_list)

self.label_source = QtWidgets.QLabel()
Expand Down Expand Up @@ -92,6 +81,15 @@ def setupUi(self, fanyisrt):
self.fanyi_target.setObjectName("fanyi_target")
self.horizontalLayout_18.addWidget(self.fanyi_target)


self.glossary = QtWidgets.QPushButton()
self.glossary.setMinimumSize(QtCore.QSize(100, 25))
self.glossary.setObjectName("glossary")
self.glossary.setText("glossary" if config.defaulelang!='zh' else '术语表')
self.glossary.setToolTip('点击设置和修改术语表' if config.defaulelang=='zh' else 'Click to set up and modify the glossary')
##self.glossary.setStyleSheet("""background-color:transparent""")
self.glossary.setCursor(Qt.PointingHandCursor)

self.out_format = QtWidgets.QComboBox()

self.out_format.addItems([
Expand All @@ -106,6 +104,34 @@ def setupUi(self, fanyisrt):
label_out.setText('输出' if config.defaulelang == 'zh' else 'Output')
self.horizontalLayout_18.addWidget(label_out)
self.horizontalLayout_18.addWidget(self.out_format)
self.horizontalLayout_18.addWidget(self.glossary)
self.horizontalLayout_18.addStretch()





self.verticalLayout_13.addLayout(self.horizontalLayout_18)





self.aisendsrt=QtWidgets.QCheckBox()
self.aisendsrt.setText('发送完整字幕' if config.defaulelang=='zh' else 'Send full subtitles')
self.aisendsrt.setToolTip('当使用AI或Google翻译渠道时,可选以完整srt字幕格式发送请求,但可能出现较多空行' if config.defaulelang=='zh' else 'When using AI or Google translation channel, you can translate in srt format, but there may be more empty lines')
self.aisendsrt.setChecked(config.settings.get('aisendsrt'))

self.refine3=QtWidgets.QCheckBox()
self.refine3.setText('三步反思法翻译' if config.defaulelang=='zh' else 'Three Steps to Reflection Translation')
self.refine3.setToolTip('当使用AI翻译渠道,并选中以完整srt字幕格式发送时,可启用三步反思翻译法' if config.defaulelang=='zh' else 'When using the AI translation channel and checking the box to send in full srt subtitle format, the three-step reflective translation method can be enabled')
self.refine3.setChecked(config.settings.get('refine3'))


self.fanyi_proxy = QtWidgets.QLineEdit()
self.fanyi_proxy.setMinimumSize(QtCore.QSize(0, 30))
self.fanyi_proxy.setObjectName("fanyi_proxy")


self.label_614 = QtWidgets.QLabel()
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed)
Expand All @@ -115,14 +141,13 @@ def setupUi(self, fanyisrt):
self.label_614.setSizePolicy(sizePolicy)
self.label_614.setMinimumSize(QtCore.QSize(0, 30))
self.label_614.setObjectName("label_614")
self.horizontalLayout_18.addWidget(self.label_614)

self.fanyi_proxy = QtWidgets.QLineEdit()
self.fanyi_proxy.setMinimumSize(QtCore.QSize(0, 30))
self.fanyi_proxy.setObjectName("fanyi_proxy")
self.horizontalLayout_18.addWidget(self.fanyi_proxy)

self.verticalLayout_13.addLayout(self.horizontalLayout_18)
self.horizontalLayout_new = QtWidgets.QHBoxLayout()
self.horizontalLayout_new.addWidget(self.aisendsrt)
self.horizontalLayout_new.addWidget(self.refine3)
self.horizontalLayout_new.addWidget(self.label_614)
self.horizontalLayout_new.addWidget(self.fanyi_proxy)
self.verticalLayout_13.addLayout(self.horizontalLayout_new)

self.loglabel = QtWidgets.QPushButton()
self.loglabel.setStyleSheet('''color:#148cd2;background-color:transparent''')
Expand Down
4 changes: 2 additions & 2 deletions videotrans/ui/subtitle_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,12 +725,12 @@ def save_ass(self, file_path,out_format=-1):
bgcolor = self.qcolor_to_ass_color(self.selected_backgroundcolor, type='bg')
bdcolor = self.qcolor_to_ass_color(self.selected_bordercolor, type='bd')
fontcolor = self.qcolor_to_ass_color(self.selected_color, type='fc')
self.qcolor_to_ass_color(self.selected_color)

file.write(
f'Style: Default,{self.selected_font.family()},{self.font_size_edit.text() if self.font_size_edit.text() else "20"},{fontcolor},{fontcolor},{bdcolor},{bgcolor},{int(self.selected_font.bold())},{int(self.selected_font.italic())},0,0,100,100,0,0,1,1,0,2,{left},{right},{vbottom},1\n')
file.write("\n[Events]\n")
file.write("Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n")
self.selected_font.bold()

index = 1

for i in range(self.content_layout.count()):
Expand Down
Loading

0 comments on commit a8a8d48

Please sign in to comment.