Skip to content

Commit

Permalink
Fix: subform error & line_role
Browse files Browse the repository at this point in the history
  • Loading branch information
jianchang512 committed Aug 14, 2024
1 parent 85c2141 commit 985215b
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 37 deletions.
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 = "v2.29"
VERSION_NUM = 1200229
VERSION = "v2.30"
VERSION_NUM = 1200230
2 changes: 1 addition & 1 deletion videotrans/configure/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def log_uncaught_exceptions(exc_type, exc_value, exc_traceback):
queue_novice = {}

# 倒计时
task_countdown = 30
task_countdown = 0
# 获取的视频信息 全局缓存
video_cache = {}

Expand Down
38 changes: 24 additions & 14 deletions videotrans/mainwin/secwin.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class SecWindow():
def __init__(self, main=None):
self.main = main
self.usetype = None
self.edit_subtitle_type=''

def is_separate_fun(self, state):
config.params['is_separate'] = True if state else False
Expand Down Expand Up @@ -607,16 +608,21 @@ def set_djs_timeout(self):
self.main.stop_djs.hide()
if self.main.shitingobj:
self.main.shitingobj.stop = True
self.update_subtitle()

# 手动点击停止自动合并倒计时
def reset_timeid(self):
self.main.stop_djs.hide()
config.task_countdown = 86400
self.main.continue_compos.setDisabled(False)
self.main.continue_compos.setText(config.transobj['nextstep'])
self.update_data('{"type":"allow_edit"}')


# 翻译渠道变化时,检测条件
def set_translate_type(self, name):
if not self.main.subform:
return
try:
rs = translator.is_allow_translate(translate_type=name, only_key=True)
if rs is not True:
Expand Down Expand Up @@ -660,6 +666,8 @@ def check_whisper_type(self, index):

# 设定模型类型
def model_type_change(self):
if not self.main.subform:
return
if self.main.model_type.currentIndex() == 1:
config.params['model_type'] = 'openai'
self.main.whisper_model.setDisabled(False)
Expand Down Expand Up @@ -753,6 +761,8 @@ def isMircosoft(self, type):

# tts类型改变
def tts_type_change(self, type):
if not self.main.subform:
return
self.hide_show_element(self.main.edge_volume_layout, self.isMircosoft(type))
if self.main.app_mode == 'peiyin' and type == 'clone-voice' and config.params['voice_role'] == 'clone':
QMessageBox.critical(self.main, config.transobj['anerror'], config.transobj[
Expand Down Expand Up @@ -1142,6 +1152,7 @@ def change_proxy(self, p):

# 检测开始状态并启动
def check_start(self):
self.edit_subtitle_type=''
if config.current_status == 'ing':
# 停止
question = tools.show_popup(config.transobj['exit'], config.transobj['confirmstop'])
Expand Down Expand Up @@ -1470,6 +1481,7 @@ def update_status(self, type):
self.main.source_mp4.setText(config.transobj["No select videos"] if len(
config.queue_mp4) < 1 else f'{len(config.queue_mp4)} videos')
# 清理输入
self.main.source_mp4.setText(config.transobj["No select videos"])
if self.main.task:
self.main.task.requestInterruption()
self.main.task.quit()
Expand Down Expand Up @@ -1522,14 +1534,15 @@ def update_data(self, json_data):
elif d['type'] == 'succeed':
# 本次任务结束
self.set_process_btn_text(d['text'], btnkey=d['btnkey'], type='succeed')
elif d['type'] == 'edit_subtitle':
elif d['type'] == 'edit_subtitle_source' or d['type']=='edit_subtitle_target':
# 显示出合成按钮,等待编辑字幕,允许修改字幕
self.main.subtitle_area.setReadOnly(False)
self.main.subtitle_area.setFocus()
self.main.continue_compos.show()
self.main.continue_compos.setDisabled(False)
self.main.continue_compos.setText(d['text'])
self.main.stop_djs.show()
self.edit_subtitle_type=d['type']
elif d['type'] == 'disabled_edit':
# 禁止修改字幕
self.main.subtitle_area.setReadOnly(True)
Expand All @@ -1538,6 +1551,7 @@ def update_data(self, json_data):
elif d['type'] == 'allow_edit':
# 允许修改字幕
self.main.subtitle_area.setReadOnly(False)
self.main.subtitle_area.setFocus()
self.main.export_sub.setDisabled(False)
self.main.set_line_role.setDisabled(False)
elif d['type'] == 'replace_subtitle':
Expand All @@ -1546,9 +1560,9 @@ def update_data(self, json_data):
self.main.subtitle_area.insertPlainText(d['text'])
elif d['type'] == 'timeout_djs':
self.main.stop_djs.hide()
self.update_subtitle(step=d['text'], btnkey=d['btnkey'])
self.main.continue_compos.setDisabled(True)
self.main.subtitle_area.setReadOnly(True)
self.update_subtitle()
elif d['type'] == 'show_djs':
self.set_process_btn_text(d['text'], btnkey=d['btnkey'])
elif d['type'] == 'check_soft_update':
Expand Down Expand Up @@ -1584,29 +1598,25 @@ def update_data(self, json_data):
self.main.sepw.set.setText(d['text'])

# update subtitle 手动 点解了 立即合成按钮,或者倒计时结束超时自动执行
def update_subtitle(self, step="translate_start", btnkey=""):
def update_subtitle(self):
self.main.stop_djs.hide()
self.main.continue_compos.setDisabled(True)
# 如果当前是等待翻译阶段,则更新原语言字幕,然后清空字幕区
txt = self.main.subtitle_area.toPlainText().strip()
txt = re.sub(r':\d+\.\d+', lambda m: m.group().replace('.', ','), txt, re.S | re.M)
config.task_countdown = 0
if not btnkey or not txt:
return
if btnkey == 'srt2wav':
srtfile = self.main.task.video.init['target_sub']
with open(srtfile, 'w', encoding='utf-8') as f:
f.write(txt)
if not txt:
return

if not self.main.task.is_batch and btnkey in self.main.task.tasklist:
if step == 'translate_start':
srtfile = self.main.task.tasklist[btnkey].init['source_sub']
if not self.main.task.is_batch and self.main.task.tasklist:
tk=list(self.main.task.tasklist.values())[0]
if self.edit_subtitle_type == 'edit_subtitle_source':
srtfile = tk.init['source_sub']
else:
srtfile = self.main.task.tasklist[btnkey].init['target_sub']
srtfile = tk.init['target_sub']
# 不是批量才允许更新字幕
with open(srtfile, 'w', encoding='utf-8') as f:
f.write(txt)
if step == 'translate_start':
if self.edit_subtitle_type == 'edit_subtitle_source':
self.main.subtitle_area.clear()
return True
20 changes: 15 additions & 5 deletions videotrans/mainwin/spwin.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ def __init__(self, parent=None, width=1200, height=700):
self.ai302fyw = None
self.ai302ttsw = None
self.setiniw = None
self.util = None#SecWindow(self)
self.subform = None

self.app_mode = "biaozhun" if not config.params['app_mode'] else config.params['app_mode']
self.processbtns = {}
Expand All @@ -80,8 +82,7 @@ def __init__(self, parent=None, width=1200, height=700):
self.rawtitle = f"{config.transobj['softname']}{VERSION} {'使用文档' if config.defaulelang == 'zh' else 'Documents'} pyvideotrans.com "
self.setWindowTitle(self.rawtitle)
# 检查窗口是否打开
self.util = None#SecWindow(self)
self.subform = None

self.setupUi(self)
self.initUI()
self.bind_action()
Expand Down Expand Up @@ -127,6 +128,8 @@ def start_subform(self):
self.actionsepar.triggered.connect(self.subform.open_separate)
self.actionsetini.triggered.connect(self.subform.open_setini)
self.action_hebingsrt.triggered.connect(self.subform.open_hebingsrt)
if config.params['tts_type'] and not config.params['tts_type'] in ['edgeTTS','AzureTTS']:
self.util.tts_type_change(config.params['tts_type'])

def initUI(self):
self.settings = QSettings("Jameson", "VideoTranslate")
Expand Down Expand Up @@ -272,6 +275,9 @@ def initUI(self):

self.set_line_role.setCursor(Qt.PointingHandCursor)
self.set_line_role.setToolTip(config.transobj['Set up separate dubbing roles for each subtitle to be used'])

if config.params['subtitle_type'] and int(config.params['subtitle_type'])>0:
self.subtitle_type.setCurrentIndex(int(config.params['subtitle_type']))

def bind_action(self):
from videotrans.mainwin.secwin import SecWindow
Expand All @@ -297,7 +303,10 @@ def bind_action(self):
if config.params['tts_type']:
if config.params['tts_type'] not in ['edgeTTS', 'AzureTTS']:
self.voice_role.addItems(['No'])
self.util.tts_type_change(config.params['tts_type'])

if config.params['is_separate']:
self.is_separate.setChecked(True)


# 设置 tts_type
self.tts_type.addItems(config.params['tts_type_list'])
Expand All @@ -308,10 +317,11 @@ def bind_action(self):
# 根据目标语言更新角色列表
self.util.set_voice_role(config.params['target_language'])
# 设置默认角色列表
if config.params['voice_role'] and config.params['voice_role'] != 'No' and self.current_rolelist and \
config.params['voice_role'] in self.current_rolelist:
print(config.params['voice_role'])
if config.params['voice_role'] and config.params['voice_role'] != 'No' and self.current_rolelist and config.params['voice_role'] in self.current_rolelist:
self.voice_role.setCurrentText(config.params['voice_role'])
self.util.show_listen_btn(config.params['voice_role'])
print('2222')

self.proxy.textChanged.connect(self.util.change_proxy)

Expand Down
6 changes: 6 additions & 0 deletions videotrans/mainwin/subform.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,22 @@ def test():

def save():
key = self.main.chatttsw.chattts_address.text().strip()
voice = self.main.chatttsw.chattts_voice.text().strip()
key = key.rstrip('/')
key = 'http://' + key.replace('http://', '').replace('/tts', '')
config.params["chattts_api"] = key
config.getset_params(config.params)
config.settings['chattts_voice']=voice
json.dump(config.settings, open(config.rootdir + "/videotrans/cfg.json", 'w', encoding='utf-8'),ensure_ascii=False)

self.main.chatttsw.close()

from videotrans.component import ChatttsForm
self.main.chatttsw = ChatttsForm()
if config.params["chattts_api"]:
self.main.chatttsw.chattts_address.setText(config.params["chattts_api"])
if config.settings["chattts_voice"]:
self.main.chatttsw.chattts_voice.setText(config.settings["chattts_voice"])
self.main.chatttsw.set_chattts.clicked.connect(save)
self.main.chatttsw.test.clicked.connect(test)
self.main.chatttsw.show()
Expand Down
36 changes: 22 additions & 14 deletions videotrans/task/step.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def recogn(self):
if self.config_params['app_mode'] == 'tiqu':
shutil.copy2(self.init['source_sub'], f"{self.obj['output']}/{self.obj['raw_noextname']}.srt")
self._unlink(self.init['shibie_audio'])
with open(self.init['source_sub'],'r',encoding='utf-8') as f:
tools.set_process(f.read().strip(), 'replace_subtitle', btnkey=self.init['btnkey'])
return True

# 分离未完成,需等待
Expand Down Expand Up @@ -112,15 +114,16 @@ def _srt_vail(self, file):

# 翻译字幕
def trans(self):
if config.current_status !='ing':
return
self.precent += 3
# 是否需要翻译,存在识别后字幕并且不存在目标语言字幕,并且原语言和目标语言不同,则需要翻译
if self.config_params['target_language'] == '-' or \
self.config_params['target_language'] == self.config_params[
'source_language'] or not tools.vail_file(self.init['source_sub']):
return True

config.task_countdown = 0 if self.config_params['app_mode'] == 'biaozhun_jd' else int(config.settings[
'countdown_sec'])
config.task_countdown = 0 if self.config_params['app_mode'] == 'biaozhun_jd' else int(config.settings['countdown_sec'])

# 如果存在目标语言字幕,前台直接使用该字幕替换
if self._srt_vail(self.init['target_sub']):
Expand All @@ -132,11 +135,11 @@ def trans(self):
shutil.copy2(self.init['target_sub'],
f"{self.obj['output']}/{Path(self.init['target_sub']).name}")
return True

# 批量不允许修改字幕
if not self.config_params['is_batch']:
# 等待编辑原字幕后翻译,允许修改字幕
tools.set_process(config.transobj["xiugaiyuanyuyan"], 'edit_subtitle', btnkey=self.init['btnkey'])
tools.set_process(config.transobj["xiugaiyuanyuyan"], 'edit_subtitle_source', btnkey=self.init['btnkey'])
while config.task_countdown > 0:
config.task_countdown -= 1
if config.task_countdown <= config.settings['countdown_sec']:
Expand All @@ -149,11 +152,11 @@ def trans(self):
time.sleep(2)

# 如果已存在目标语言字幕则跳过,比如使用已有字幕,无需翻译时
if self._srt_vail(self.init['target_sub']):
if self.obj and self.obj['output'] != self.obj['linshi_output'] and tools.vail_file(
self.init['target_sub']):
shutil.copy2(self.init['target_sub'], f"{self.obj['output']}/{Path(self.init['target_sub']).name}")
return True
#if self._srt_vail(self.init['target_sub']):
# if self.obj and self.obj['output'] != self.obj['linshi_output'] and tools.vail_file(
# self.init['target_sub']):
# shutil.copy2(self.init['target_sub'], f"{self.obj['output']}/{Path(self.init['target_sub']).name}")
# return True
tools.set_process(config.transobj['starttrans'], btnkey=self.init['btnkey'])
# 开始翻译,从目标文件夹读取原始字幕
rawsrt = tools.get_subtitle_from_srt(self.init['source_sub'], is_file=True)
Expand Down Expand Up @@ -185,11 +188,12 @@ def trans(self):

# 配音处理
def dubbing(self):
if config.current_status !='ing':
return
if self.config_params['app_mode'] in ['tiqu']:
return True
self.precent += 3
config.task_countdown = 0 if self.config_params['app_mode'] == 'biaozhun_jd' else int(config.settings[
'countdown_sec'])
config.task_countdown = 0 if self.config_params['app_mode'] == 'biaozhun_jd' else int(config.settings['countdown_sec'])

# 不需要配音
if self.config_params['voice_role'] == 'No' or \
Expand All @@ -202,7 +206,7 @@ def dubbing(self):
return True
# 允许修改字幕
if not self.config_params['is_batch']:
tools.set_process(config.transobj["xiugaipeiyinzimu"], "edit_subtitle", btnkey=self.init['btnkey'])
tools.set_process(config.transobj["xiugaipeiyinzimu"], "edit_subtitle_target", btnkey=self.init['btnkey'])
while config.task_countdown > 0:
# 其他情况,字幕处理完毕,未超时,等待1s,继续倒计时
time.sleep(1)
Expand Down Expand Up @@ -850,6 +854,8 @@ def _novoicemp4_add_time(self, duration_ms):

# 添加背景音乐
def _back_music(self):
if config.current_status !='ing':
return
if self.config_params['app_mode'] not in ["tiqu", "peiyin"] and self.config_params[
'voice_role'] != 'No' and tools.vail_file(self.init['target_wav']) and tools.vail_file(
self.init['background_music']):
Expand Down Expand Up @@ -890,6 +896,8 @@ def _back_music(self):
config.logger.error(f'添加背景音乐失败:{str(e)}')

def _separate(self):
if config.current_status !='ing':
return
if self.config_params['is_separate'] and tools.vail_file(self.init['target_wav']):
try:
self.parent.status_text = '重新嵌入背景音' if config.defaulelang == 'zh' else 'Re-embedded background sounds'
Expand All @@ -913,9 +921,9 @@ def _separate(self):

# 最终合成视频 source_mp4=原始mp4视频文件,noextname=无扩展名的视频文件名字
def _compos_video(self):
if self.config_params['app_mode'] in ['tiqu', 'peiyin']:
if self.config_params['app_mode'] in ['tiqu', 'peiyin'] or config.current_status !='ing':
return True

# 判断novoice_mp4是否完成
if not tools.is_novoice_mp4(self.init['novoice_mp4'], self.init['noextname']):
raise Exception(config.transobj['fenlinoviceerror'])
Expand Down
Loading

0 comments on commit 985215b

Please sign in to comment.