forked from jianchang512/pyvideotrans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sp.py
1345 lines (1222 loc) · 58.5 KB
/
sp.py
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
import copy
import datetime
import json
import shutil
import sys
import os
import threading
import webbrowser
import torch
from PyQt5 import QtWidgets
from PyQt5.QtGui import QTextCursor, QIcon, QDesktopServices
from PyQt5.QtCore import QSettings, QUrl, Qt, QSize
from PyQt5.QtWidgets import QMainWindow, QApplication, QMessageBox, QFileDialog, QLabel, QPushButton, QToolBar
import warnings
from videotrans.component.set_form import InfoForm
from videotrans.task.check_update import CheckUpdateWorker
from videotrans.task.logs_worker import LogsWorker
from videotrans.task.main_worker import Worker, Shiting
warnings.filterwarnings('ignore')
from videotrans import VERSION
from videotrans.component import DeepLForm, DeepLXForm, BaiduForm, TencentForm, ChatgptForm
from videotrans.component.controlobj import TextGetdir
from videotrans.configure.config import langlist, transobj, logger, homedir
from videotrans.configure.language import english_code_bygpt
from videotrans.util.tools import show_popup, set_proxy, set_process, get_edge_rolelist, is_vlc
from videotrans.configure import config
import pygame
if config.defaulelang == "zh":
from videotrans.ui.cn import Ui_MainWindow
else:
from videotrans.ui.en import Ui_MainWindow
# primary ui
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.cfg = config.video
self.task = None
self.toolboxobj = None
self.shitingobj = None
self.initUI()
self.setWindowIcon(QIcon("./icon.ico"))
self.rawtitle = f"{'视频翻译配音' if config.defaulelang != 'en' else ' Video Translate & Dubbing'} {VERSION}"
self.setWindowTitle(self.rawtitle)
def initUI(self):
self.settings = QSettings("Jameson", "VideoTranslate")
# 获取最后一次选择的目录
self.last_dir = self.settings.value("last_dir", ".", str)
# language code
self.languagename = list(langlist.keys())
self.app_mode = 'biaozhun'
# 上次缓存
self.get_setting()
self.splitter.setSizes([1000, 360])
# start
self.startbtn.clicked.connect(self.check_start)
# 隐藏倒计时
self.stop_djs.clicked.connect(self.reset_timeid)
self.stop_djs.hide()
# subtitle btn
self.continue_compos.hide()
self.continue_compos.clicked.connect(self.set_djs_timeout)
# select and save
self.btn_get_video.clicked.connect(self.get_mp4)
self.source_mp4.setAcceptDrops(True)
self.btn_save_dir.clicked.connect(self.get_save_dir)
self.target_dir.setAcceptDrops(True)
self.proxy.setText(config.proxy)
self.open_targetdir.clicked.connect(lambda: self.open_dir(self.target_dir.text()))
# language
self.source_language.addItems(self.languagename)
self.source_language.setCurrentIndex(2)
# 目标语言改变时,如果当前tts是 edgeTTS,则根据目标语言去修改显示的角色
self.target_language.addItems(["-"] + self.languagename)
# 目标语言改变
self.target_language.currentTextChanged.connect(self.set_voice_role)
self.voice_role.currentTextChanged.connect(self.show_listen_btn)
self.listen_btn.hide()
self.listen_btn.clicked.connect(self.listen_voice_fun)
# translation type
self.translate_type.addItems(["google", "baidu", "chatGPT", "tencent", "DeepL", "DeepLX", "baidu(noKey)"])
self.translate_type.setCurrentText(self.cfg['translate_type'])
self.translate_type.currentTextChanged.connect(self.set_translate_type)
# model
self.whisper_type.addItems([transobj['whisper_type_all'], transobj['whisper_type_split']])
self.whisper_type.currentIndexChanged.connect(self.check_whisper_type)
if self.cfg['whisper_type']:
self.whisper_type.setCurrentIndex(0 if self.cfg['whisper_type'] == 'all' else 1)
self.whisper_model.addItems(['base', 'small', 'medium', 'large', 'large-v3'])
self.whisper_model.setCurrentText(self.cfg['whisper_model'])
self.whisper_model.currentTextChanged.connect(self.check_whisper_model)
#
self.voice_rate.setText(self.cfg['voice_rate'])
self.voice_rate.textChanged.connect(self.voice_rate_changed)
self.voice_silence.setText(self.cfg['voice_silence'])
self.voice_autorate.stateChanged.connect(
lambda: self.autorate_changed(self.voice_autorate.isChecked(), "voice"))
self.video_autorate.stateChanged.connect(
lambda: self.autorate_changed(self.video_autorate.isChecked(), "video"))
# 设置角色类型,如果当前是OPENTTS或 coquiTTS则设置,如果是edgeTTS,则为No
if self.cfg['tts_type'] == 'edgeTTS':
self.voice_role.addItems(['No'])
elif self.cfg['tts_type'] == 'openaiTTS':
self.voice_role.addItems(['No'] + self.cfg['openaitts_role'].split(','))
elif self.cfg['tts_type'] == 'coquiTTS':
self.voice_role.addItems(['No'] + self.cfg['coquitts_role'].split(','))
# 设置 tts_type
self.tts_type.addItems(self.cfg['tts_type_list'])
self.tts_type.setCurrentText(self.cfg['tts_type'])
# tts_type 改变时,重设角色
self.tts_type.currentTextChanged.connect(self.tts_type_change)
self.enable_cuda.stateChanged.connect(self.check_cuda)
self.enable_cuda.setChecked(self.cfg['enable_cuda'])
# subtitle 0 no 1=embed subtitle 2=softsubtitle
self.subtitle_type.addItems([transobj['nosubtitle'], transobj['embedsubtitle'], transobj['softsubtitle']])
self.subtitle_type.setCurrentIndex(self.cfg['subtitle_type'])
# 字幕编辑
self.subtitle_area = TextGetdir(self)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
self.subtitle_area.setSizePolicy(sizePolicy)
self.subtitle_area.setMinimumSize(300, 0)
self.subtitle_area.setPlaceholderText(transobj['subtitle_tips'])
self.subtitle_layout.insertWidget(0, self.subtitle_area)
self.import_sub = QPushButton("导入字幕")
self.import_sub.setMinimumSize(80, 35)
self.import_sub.clicked.connect(self.import_sub_fun)
self.listen_peiyin = QPushButton("试听配音")
self.listen_peiyin.setMinimumSize(0, 35)
self.listen_peiyin.setDisabled(True)
self.listen_peiyin.setToolTip("先启动任务,待字幕翻译完成后可试听,配音速度、自动加速实时修改生效")
self.listen_peiyin.clicked.connect(self.shiting_peiyin)
self.layout_sub_bottom.insertWidget(0, self.import_sub)
self.layout_sub_bottom.insertWidget(1, self.listen_peiyin)
# menubar
self.actionbaidu_key.triggered.connect(self.set_baidu_key)
self.actiontencent_key.triggered.connect(self.set_tencent_key)
self.actionchatgpt_key.triggered.connect(self.set_chatgpt_key)
self.actiondeepL_key.triggered.connect(self.set_deepL_key)
self.actiondeepLX_address.triggered.connect(self.set_deepLX_address)
self.action_vlc.triggered.connect(lambda: self.open_url('vlc'))
self.action_ffmpeg.triggered.connect(lambda: self.open_url('ffmpeg'))
self.action_git.triggered.connect(lambda: self.open_url('git'))
self.action_discord.triggered.connect(lambda: self.open_url('discord'))
self.action_website.triggered.connect(lambda: self.open_url('website'))
self.action_issue.triggered.connect(lambda: self.open_url('issue'))
self.action_tool.triggered.connect(self.open_toolbox)
self.action_about.triggered.connect(self.about)
self.action_clone.triggered.connect(lambda: show_popup(transobj['yinsekaifazhong'], transobj['yinsekelong']))
# 设置QAction的大小
self.toolBar.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
# 设置QToolBar的大小,影响其中的QAction的大小
self.toolBar.setIconSize(QSize(100, 45)) # 设置图标大小
self.action_biaozhun.triggered.connect(self.set_biaozhun)
self.action_tiquzimu.triggered.connect(self.set_tiquzimu)
self.action_tiquzimu_no.triggered.connect(self.set_tiquzimu_no)
self.action_zimu_video.triggered.connect(self.set_zimu_video)
self.action_zimu_peiyin.triggered.connect(self.set_zimu_peiyin)
self.action_yuyinshibie.triggered.connect(lambda: self.open_toolbox(2))
self.action_yuyinhecheng.triggered.connect(lambda: self.open_toolbox(3))
self.action_yinshipinfenli.triggered.connect(lambda: self.open_toolbox(0))
self.action_yingyinhebing.triggered.connect(lambda: self.open_toolbox(1))
self.action_geshi.triggered.connect(lambda: self.open_toolbox(4))
self.action_hun.triggered.connect(lambda: self.open_toolbox(5))
# 底部状态栏
self.statusLabel = QLabel(transobj['modelpathis'] + " /models")
self.statusLabel.setStyleSheet("color:#00a67d")
self.statusBar.addWidget(self.statusLabel)
rightbottom = QPushButton(" 捐助该软件! ")
rightbottom.setToolTip("如果有你的捐助,软件将能得到持续维护,点击查看")
rightbottom.clicked.connect(self.about)
usetype = QPushButton(" 快速使用技巧 ")
usetype.setToolTip("查看常见使用技巧")
usetype.clicked.connect(self.usetype)
container = QToolBar()
container.addWidget(rightbottom)
container.addWidget(usetype)
self.statusBar.addPermanentWidget(container)
# 日志
self.task_logs = LogsWorker(self)
self.task_logs.post_logs.connect(self.update_data)
self.task_logs.start()
self.check_update = CheckUpdateWorker(self)
self.check_update.start()
def check_cuda(self, state):
res = state
# 选中如果无效,则取消
if state and not torch.cuda.is_available():
QMessageBox.critical(self, '你的设备不满足CUDA加速要求',
'请确认是NVIDIA显卡,并已安装 CUDA 11.8,如未安装,请去 developer.nvidia.com/cuda-downloads 安装匹配当前系统的 cuda 11.8,然后重启软件')
self.enable_cuda.setChecked(False)
self.enable_cuda.setDisabled(True)
res = False
config.cuda = res
if config.cuda:
os.environ['CUDA_OK'] = "yes"
elif os.environ.get('CUDA_OK'):
os.environ.pop('CUDA_OK')
# 配音速度改变时,更改全局
def voice_rate_changed(self, text):
text = int(str(text).replace('+', '').replace('%', ''))
text = f'+{text}%' if text >= 0 else f'-{text}%'
config.voice_rate = text
# 字幕下方试听配音
def shiting_peiyin(self):
if not self.task:
return
if self.voice_role.currentText() == 'No':
return QMessageBox.critical(self, "出错了", "未选择角色,不可试听")
if self.shitingobj:
self.shitingobj.stop = True
self.shitingobj = None
self.listen_peiyin.setText('重听中')
else:
self.listen_peiyin.setText('试听中/点击重听')
obj = {
"sub_name": self.task.video.targetdir_target_sub,
"noextname": self.task.video.noextname,
"cache_folder": self.task.video.cache_folder,
"source_wav": self.task.video.targetdir_source_sub,
"voice_role": config.voice_role,
"voice_autorate": config.voice_autorate,
"voice_rate": config.voice_rate,
"tts_type": self.task.video.obj['tts_type'],
}
txt=self.subtitle_area.toPlainText().strip()
if not txt:
return QMessageBox.critical(self,"出错了",'无字幕内容,不可试听')
with open(self.task.video.targetdir_target_sub,'w',encoding='utf-8') as f:
f.write(txt)
self.shitingobj = Shiting(obj, self)
self.shitingobj.start()
# 启用标准模式
def set_biaozhun(self):
self.app_mode = 'biaozhun'
self.show_tips.setText("")
self.startbtn.setText("开始处理")
self.action_tiquzimu_no.setChecked(False)
self.action_biaozhun.setChecked(True)
self.action_tiquzimu.setChecked(False)
self.action_zimu_video.setChecked(False)
self.action_zimu_peiyin.setChecked(False)
# 选择视频
self.hide_show_element(self.layout_source_mp4, True)
# 保存目标
self.hide_show_element(self.layout_target_dir, True)
self.open_targetdir.show()
# 翻译渠道
self.hide_show_element(self.layout_translate_type, True)
# 代理
self.hide_show_element(self.layout_proxy, True)
# 原始语言
self.hide_show_element(self.layout_source_language, True)
# 目标语言
self.hide_show_element(self.layout_target_language, True)
# tts类型
self.hide_show_element(self.layout_tts_type, True)
# 配音角色
self.hide_show_element(self.layout_voice_role, True)
# 试听按钮
self.hide_show_element(self.listen_layout, False)
# 语音模型
self.hide_show_element(self.layout_whisper_model, True)
# 字幕类型
self.hide_show_element(self.layout_subtitle_type, True)
# 配音语速
self.hide_show_element(self.layout_voice_rate, True)
# 静音片段
self.hide_show_element(self.layout_voice_silence, True)
# 配音自动加速
self.voice_autorate.show()
# 视频自动降速
self.video_autorate.show()
# cuda
self.enable_cuda.show()
# 视频提取字幕并翻译,无需配音
def set_tiquzimu(self):
self.app_mode = 'tiqu'
self.show_tips.setText("原始语言设为视频发音语言,目标语言设为想翻译为的语言")
self.startbtn.setText("开始提取和翻译")
self.action_tiquzimu_no.setChecked(False)
self.action_tiquzimu.setChecked(True)
self.action_biaozhun.setChecked(False)
self.action_zimu_video.setChecked(False)
self.action_zimu_peiyin.setChecked(False)
# 选择视频
self.hide_show_element(self.layout_source_mp4, True)
# 保存目标
self.hide_show_element(self.layout_target_dir, True)
self.open_targetdir.show()
# 翻译渠道
self.hide_show_element(self.layout_translate_type, True)
# 代理
self.hide_show_element(self.layout_proxy, True)
# 原始语言
self.hide_show_element(self.layout_source_language, True)
# 目标语言
self.hide_show_element(self.layout_target_language, True)
# tts类型
self.hide_show_element(self.layout_tts_type, False)
# 配音角色
self.hide_show_element(self.layout_voice_role, False)
# self.voice_role.setCurrentText('No')
# 试听按钮
self.hide_show_element(self.listen_layout, False)
# 语音模型
self.hide_show_element(self.layout_whisper_model, True)
# 字幕类型
self.hide_show_element(self.layout_subtitle_type, False)
# self.subtitle_type.setCurrentIndex(0)
# 配音语速
self.hide_show_element(self.layout_voice_rate, False)
# self.voice_rate.setText('+0%')
# 静音片段
self.hide_show_element(self.layout_voice_silence, False)
# self.voice_silence.setText('500')
# 配音自动加速
self.voice_autorate.hide()
self.voice_autorate.setChecked(False)
# 视频自动降速
self.video_autorate.hide()
self.video_autorate.setChecked(False)
# cuda
self.enable_cuda.show()
# 从视频提取字幕,不翻译
# 只显示 选择视频、保存目标、原始语言、语音模型,其他不需要
def set_tiquzimu_no(self):
self.app_mode = 'tiqu_no'
self.show_tips.setText("原始语言设为视频发音语言")
self.startbtn.setText("开始提取字幕")
self.action_tiquzimu.setChecked(False)
self.action_tiquzimu_no.setChecked(True)
self.action_biaozhun.setChecked(False)
self.action_zimu_video.setChecked(False)
self.action_zimu_peiyin.setChecked(False)
# 选择视频
self.hide_show_element(self.layout_source_mp4, True)
# 保存目标
self.hide_show_element(self.layout_target_dir, True)
self.open_targetdir.show()
# 翻译渠道
self.hide_show_element(self.layout_translate_type, False)
# 代理
self.hide_show_element(self.layout_proxy, False)
# self.proxy.setText('')
# 原始语言
self.hide_show_element(self.layout_source_language, True)
# 目标语言
self.hide_show_element(self.layout_target_language, False)
# self.target_language.setCurrentText('-')
# tts类型
self.hide_show_element(self.layout_tts_type, False)
# 配音角色
self.hide_show_element(self.layout_voice_role, False)
# self.voice_role.setCurrentText('No')
# 试听按钮
self.hide_show_element(self.listen_layout, False)
# 语音模型
self.hide_show_element(self.layout_whisper_model, True)
# 字幕类型
self.hide_show_element(self.layout_subtitle_type, False)
# self.subtitle_type.setCurrentIndex(0)
# 配音语速
self.hide_show_element(self.layout_voice_rate, False)
# self.voice_rate.setText('+0%')
# 静音片段
self.hide_show_element(self.layout_voice_silence, False)
# self.voice_silence.setText('500')
# 配音自动加速
self.voice_autorate.hide()
self.voice_autorate.setChecked(False)
# 视频自动降速
self.video_autorate.hide()
self.video_autorate.setChecked(False)
# cuda
self.enable_cuda.show()
# 启用字幕合并模式, 仅显示 选择视频、保存目录、字幕类型、自动视频降速 cuda
# 不配音、不识别,
def set_zimu_video(self):
self.app_mode = 'hebing'
self.show_tips.setText("选择要合并的视频,将字幕srt文件拖拽到右侧字幕区")
self.startbtn.setText("开始合并")
self.action_tiquzimu_no.setChecked(False)
self.action_biaozhun.setChecked(False)
self.action_tiquzimu.setChecked(False)
self.action_zimu_video.setChecked(True)
self.action_zimu_peiyin.setChecked(False)
# 选择视频
self.hide_show_element(self.layout_source_mp4, True)
# 保存目标
self.hide_show_element(self.layout_target_dir, True)
self.open_targetdir.show()
# 翻译渠道
self.hide_show_element(self.layout_translate_type, False)
# 代理
self.hide_show_element(self.layout_proxy, False)
# 原始语言
self.hide_show_element(self.layout_source_language, False)
# 目标语言
self.hide_show_element(self.layout_target_language, False)
# tts类型
self.hide_show_element(self.layout_tts_type, False)
# 配音角色
self.hide_show_element(self.layout_voice_role, False)
# 试听按钮
self.hide_show_element(self.listen_layout, False)
# 语音模型
self.hide_show_element(self.layout_whisper_model, False)
# 字幕类型
self.hide_show_element(self.layout_subtitle_type, True)
# 配音语速
self.hide_show_element(self.layout_voice_rate, False)
# 静音片段
self.hide_show_element(self.layout_voice_silence, False)
# 配音自动加速
self.voice_autorate.hide()
self.voice_autorate.setChecked(False)
# 视频自动降速
self.video_autorate.show()
self.video_autorate.setChecked(False)
# cuda
self.enable_cuda.show()
# 仅仅对已有字幕配音,显示目标语言、tts相关,自动加速相关,
# 不翻译不识别
def set_zimu_peiyin(self):
self.show_tips.setText("请将目标语言设为字幕所用语言,并选择配音角色")
self.startbtn.setText("开始配音")
self.action_tiquzimu_no.setChecked(False)
self.action_biaozhun.setChecked(False)
self.action_tiquzimu.setChecked(False)
self.action_zimu_video.setChecked(False)
self.action_zimu_peiyin.setChecked(True)
self.app_mode = 'peiyin'
# 选择视频
self.hide_show_element(self.layout_source_mp4, False)
# 保存目标
self.hide_show_element(self.layout_target_dir, True)
self.open_targetdir.show()
# 翻译渠道
self.hide_show_element(self.layout_translate_type, False)
# 代理 openaitts
self.hide_show_element(self.layout_proxy, True)
# 原始语言
self.hide_show_element(self.layout_source_language, False)
# 目标语言
self.hide_show_element(self.layout_target_language, True)
# tts类型
self.hide_show_element(self.layout_tts_type, True)
# 配音角色
self.hide_show_element(self.layout_voice_role, True)
# 试听按钮
self.hide_show_element(self.listen_layout, True)
# 语音模型
self.hide_show_element(self.layout_whisper_model, False)
# 字幕类型
self.hide_show_element(self.layout_subtitle_type, False)
# 配音语速
self.hide_show_element(self.layout_voice_rate, True)
# 静音片段
self.hide_show_element(self.layout_voice_silence, False)
# 配音自动加速
self.voice_autorate.show()
# 视频自动降速
self.video_autorate.hide()
self.video_autorate.setChecked(False)
# cuda
self.enable_cuda.show()
# 使用指南
def usetype(self):
string = """
【标准模式】
根据需要设置各个选项,自由配置组合,实现翻译和配音、合并等
【提取字幕不翻译】
选择视频文件,选择视频源语言,则从视频识别出文字并自动导出字幕文件到目标文件夹
【提取字幕并翻译】
选择视频文件,选择视频源语言,设置想翻译到的目标语言,则从视频识别出文字并翻译为目标语言,然后导出双语字幕文件到目标文件夹
【字幕和视频合并】
选择视频,然后将已有的字幕文件拖拽到右侧字幕区,将源语言和目标语言都设为字幕所用语言、然后选择配音类型和角色,开始执行
【为字幕创建配音】
将本地的字幕文件拖拽到右侧字幕编辑器,然后选择目标语言、配音类型和角色,将生成配音后的音频文件到目标文件夹
【音视频识别文字】
将视频或音频拖拽到识别窗口,将识别出文字并导出为srt字幕格式
【将文字合成语音】
将一段文字或者字幕,使用指定的配音角色生成配音
【从视频分离音频】
将视频文件分离为音频文件和无声视频
【音视频字幕合并】
音频文件、视频文件、字幕文件合并为一个视频文件
【音视频格式转换】
各种格式之间的相互转换
"""
QMessageBox.information(self, "常见使用方式", string)
# 关于页面
def about(self):
self.infofrom = InfoForm()
self.infofrom.show()
# voice_autorate video_autorate 变化
def autorate_changed(self, state, name):
if state:
if name == 'voice':
self.video_autorate.setChecked(False)
else:
self.voice_autorate.setChecked(False)
if name == 'voice':
config.voice_autorate = state
else:
config.video_autorate = state
def open_dir(self, dirname=None):
if not dirname:
return
if not os.path.isdir(dirname):
dirname = os.path.dirname(dirname)
QDesktopServices.openUrl(QUrl(f"file:{dirname}"))
# 隐藏布局及其元素
def hide_show_element(self, wrap_layout, show_status):
def hide_recursive(layout, show_status):
for i in range(layout.count()):
item = layout.itemAt(i)
if item.widget():
if not show_status:
item.widget().hide()
else:
item.widget().show()
elif item.layout():
hide_recursive(item.layout(), show_status)
hide_recursive(wrap_layout, show_status)
# 开启执行后,禁用按钮,停止或结束后,启用按钮
def disabled_widget(self, type):
self.import_sub.setDisabled(type)
self.btn_get_video.setDisabled(type)
self.source_mp4.setDisabled(type)
self.btn_save_dir.setDisabled(type)
self.target_dir.setDisabled(type)
self.translate_type.setDisabled(type)
self.proxy.setDisabled(type)
self.source_language.setDisabled(type)
self.target_language.setDisabled(type)
self.tts_type.setDisabled(type)
self.whisper_model.setDisabled(type)
self.whisper_type.setDisabled(type)
self.subtitle_type.setDisabled(type)
self.voice_silence.setDisabled(type)
self.video_autorate.setDisabled(type)
self.enable_cuda.setDisabled(type)
# self.voice_role.setDisabled(type)
# self.voice_rate.setDisabled(type)
# self.voice_autorate.setDisabled(type)
def closeEvent(self, event):
# 在关闭窗口前执行的操作
if self.toolboxobj is not None:
self.toolboxobj.close()
if config.current_status == 'ing':
config.current_status = 'end'
msg = QMessageBox()
msg.setWindowTitle(transobj['exit'])
msg.setWindowIcon(QIcon(config.rootdir + "/icon.ico"))
msg.setText(transobj['waitclear'])
msg.addButton(transobj['queding'], QMessageBox.AcceptRole)
msg.setIcon(QMessageBox.Information)
msg.exec_() # 显示消息框
event.accept()
else:
event.accept()
def get_setting(self):
# 从缓存获取默认配置
self.cfg['baidu_appid'] = self.settings.value("baidu_appid", "")
self.cfg['baidu_miyue'] = self.settings.value("baidu_miyue", "")
self.cfg['deepl_authkey'] = self.settings.value("deepl_authkey", "")
self.cfg['deeplx_address'] = self.settings.value("deeplx_address", "")
self.cfg['chatgpt_api'] = self.settings.value("chatgpt_api", "")
self.cfg['chatgpt_key'] = self.settings.value("chatgpt_key", "")
self.cfg['tencent_SecretId'] = self.settings.value("tencent_SecretId", "")
self.cfg['tencent_SecretKey'] = self.settings.value("tencent_SecretKey", "")
os.environ['OPENAI_API_KEY'] = self.cfg['chatgpt_key']
self.cfg['chatgpt_model'] = self.settings.value("chatgpt_model", self.cfg['chatgpt_model'])
self.cfg['chatgpt_template'] = self.cfg['chatgpt_template']
self.cfg['translate_type'] = self.settings.value("translate_type", self.cfg['translate_type'])
self.cfg['subtitle_type'] = self.settings.value("subtitle_type", self.cfg['subtitle_type'], int)
config.proxy = self.settings.value("proxy", "", str)
self.cfg['voice_rate'] = self.settings.value("voice_rate", self.cfg['voice_rate'], str)
self.cfg['voice_silence'] = self.settings.value("voice_silence", self.cfg['voice_silence'], str)
self.cfg['enable_cuda'] = False if not config.cuda else self.settings.value("enable_cuda", False, bool)
self.cfg['whisper_model'] = self.settings.value("whisper_model", self.cfg['whisper_model'], str)
self.cfg['whisper_type'] = self.settings.value("whisper_type", self.cfg['whisper_type'], str)
self.cfg['tts_type'] = self.settings.value("tts_type", self.cfg['tts_type'], str)
if not self.cfg['tts_type']:
self.cfg['tts_type'] = 'edgeTTS'
def open_url(self, title):
if title == 'vlc':
webbrowser.open_new_tab("https://www.videolan.org/vlc/")
elif title == 'ffmpeg':
webbrowser.open_new_tab("https://www.ffmpeg.org/download.html")
elif title == 'git':
webbrowser.open_new_tab("https://github.com/jianchang512/pyvideotrans")
elif title == 'issue':
webbrowser.open_new_tab("https://github.com/jianchang512/pyvideotrans/issues")
elif title == 'discord':
webbrowser.open_new_tab("https://discord.com/channels/1174626422044766258/1174626425702207562")
elif title == 'website':
webbrowser.open_new_tab("https://v.wonyes.org")
elif title == "about":
webbrowser.open_new_tab("https://github.com/jianchang512/pyvideotrans/blob/main/about.md")
# 工具箱
def open_toolbox(self, index=0):
try:
import box
if self.toolboxobj is None:
self.toolboxobj = box.MainWindow()
self.toolboxobj.show()
self.toolboxobj.tabWidget.setCurrentIndex(index)
self.toolboxobj.raise_()
except Exception as e:
self.toolboxobj = None
QMessageBox.critical(self, "出错了", "你可能需要先安装VLC解码器," + str(e))
logger.error("vlc" + str(e))
# 将倒计时设为立即超时
def set_djs_timeout(self):
config.task_countdown = 0
self.continue_compos.setText("继续执行中")
self.continue_compos.setDisabled(True)
self.stop_djs.hide()
self.process.clear()
if self.shitingobj:
self.shitingobj.stop = True
# 手动点击停止自动合并倒计时
def reset_timeid(self):
self.stop_djs.hide()
config.task_countdown = 86400
self.process.moveCursor(QTextCursor.End)
self.process.insertHtml("<br><strong>倒计时停止,修改后请手动点击“继续执行”按钮</strong><br>")
self.continue_compos.setDisabled(False)
self.continue_compos.setText("继续下一步")
# set deepl key
def set_deepL_key(self):
def save():
key = self.w.deepl_authkey.text()
self.settings.setValue("deepl_authkey", key)
self.cfg['deepl_authkey'] = key
self.w.close()
self.w = DeepLForm()
if self.cfgself.cfgself.cfgself.cfgself.cfg['deepl_authkey']:
self.w.deepl_authkey.setText(self.cfg['deepl_authkey'])
self.w.set_deepl.clicked.connect(save)
self.w.show()
def set_deepLX_address(self):
def save():
key = self.w.deeplx_address.text()
self.settings.setValue("deeplx_address", key)
self.cfgself.cfgself.cfgself.cfg['deeplx_address'] = key
self.w.close()
self.w = DeepLXForm()
if self.cfgself.cfgself.cfgself.cfg['deeplx_address']:
self.w.deeplx_address.setText(self.cfg['deeplx_address'])
self.w.set_deeplx.clicked.connect(save)
self.w.show()
# set baidu
def set_baidu_key(self):
def save_baidu():
appid = self.w.baidu_appid.text()
miyue = self.w.baidu_miyue.text()
self.settings.setValue("baidu_appid", appid)
self.settings.setValue("baidu_miyue", miyue)
self.cfgself.cfgself.cfg['baidu_appid'] = appid
self.cfgself.cfgself.cfg['baidu_miyue'] = miyue
self.w.close()
self.w = BaiduForm()
if self.cfgself.cfgself.cfg['baidu_appid']:
self.w.baidu_appid.setText(self.cfgself.cfgself.cfg['baidu_appid'])
if self.cfgself.cfgself.cfg['baidu_miyue']:
self.w.baidu_miyue.setText(self.cfg['baidu_miyue'])
self.w.set_badiu.clicked.connect(save_baidu)
self.w.show()
def set_tencent_key(self):
def save():
SecretId = self.w.tencent_SecretId.text()
SecretKey = self.w.tencent_SecretKey.text()
self.settings.setValue("tencent_SecretId", SecretId)
self.settings.setValue("tencent_SecretKey", SecretKey)
self.cfgself.cfg['tencent_SecretId'] = SecretId
self.cfgself.cfg['tencent_SecretKey'] = SecretKey
self.w.close()
self.w = TencentForm()
if self.cfgself.cfg['tencent_SecretId']:
self.w.tencent_SecretId.setText(self.cfgself.cfg['tencent_SecretId'])
if self.cfgself.cfg['tencent_SecretKey']:
self.w.tencent_SecretKey.setText(self.cfg['tencent_SecretKey'])
self.w.set_tencent.clicked.connect(save)
self.w.show()
# set chatgpt
def set_chatgpt_key(self):
def save_chatgpt():
key = self.w.chatgpt_key.text()
api = self.w.chatgpt_api.text()
model = self.w.chatgpt_model.currentText()
template = self.w.chatgpt_template.toPlainText()
self.settings.setValue("chatgpt_key", key)
self.settings.setValue("chatgpt_api", api)
self.settings.setValue("chatgpt_model", model)
self.settings.setValue("chatgpt_template", template)
os.environ['OPENAI_API_KEY'] = key
self.cfg['chatgpt_key'] = key
self.cfg['chatgpt_api'] = api
self.cfg['chatgpt_model'] = model
self.cfg['chatgpt_template'] = template
self.w.close()
self.w = ChatgptForm()
if self.cfg['chatgpt_key']:
self.w.chatgpt_key.setText(self.cfg['chatgpt_key'])
if self.cfg['chatgpt_api']:
self.w.chatgpt_api.setText(self.cfg['chatgpt_api'])
if self.cfg['chatgpt_model']:
self.w.chatgpt_model.setCurrentText(self.cfg['chatgpt_model'])
if self.cfg['chatgpt_template']:
self.w.chatgpt_template.setPlainText(self.cfg['chatgpt_template'])
self.w.set_chatgpt.clicked.connect(save_chatgpt)
self.w.show()
# 翻译渠道变化时,检测条件
def set_translate_type(self, name):
try:
if name == "baidu" and not self.cfg['baidu_appid']:
QMessageBox.critical(self, transobj['anerror'], transobj['baidukeymust'])
return
if name == "chatGPT" and not self.cfg["chatgpt_key"]:
QMessageBox.critical(self, transobj['anerror'], transobj['chatgptkeymust'])
return
if name == "DeepL" and not self.cfg["deepl_authkey"]:
QMessageBox.critical(self, transobj['anerror'], transobj['setdeepl_authkey'])
return
if name == "DeepLX" and not self.cfg["deeplx_address"]:
QMessageBox.critical(self, transobj['anerror'], transobj['setdeeplx_address'])
return
self.cfg['translate_type'] = name
except Exception as e:
QMessageBox.critical(self, transobj['anerror'], str(e))
def check_whisper_type(self, index):
if index == 0:
self.cfg['whisper_type'] = 'all'
else:
self.cfg['whisper_type'] = 'split'
# check model is exits
def check_whisper_model(self, name):
if not os.path.exists(config.rootdir + f"/models/{name}.pt"):
self.statusLabel.setText(transobj['downloadmodel'] + f" ./models/{name}.pt")
QMessageBox.critical(self, transobj['downloadmodel'], f"./models/{name}.pt")
else:
self.statusLabel.setText(transobj['modelpathis'] + f" ./models/{name}.pt")
# 更新执行状态
def update_status(self, type):
config.current_status = type
self.continue_compos.hide()
self.stop_djs.hide()
if type != 'ing':
# 结束或停止
self.startbtn.setText(transobj[type])
# 启用
self.disabled_widget(False)
if type == 'end':
# 清理字幕
self.subtitle_area.clear()
# 清理输入
self.statusLabel.setText("本次任务结束")
self.source_mp4.clear()
self.target_dir.clear()
if self.task:
self.task.requestInterruption()
self.task.quit()
else:
# 重设为开始状态
self.disabled_widget(True)
self.startbtn.setText(transobj['running'])
self.statusLabel.setText("开始处理...")
# tts类型改变
def tts_type_change(self, type):
self.cfg['tts_type'] = type
# 如果任务是在进行中,则更改
if type == "openaiTTS":
self.voice_role.clear()
self.voice_role.addItems(['No'] + self.cfg['openaitts_role'].split(','))
elif type == 'coquiTTS':
self.voice_role.addItems(['No'] + self.cfg['coquitts_role'].split(','))
elif type == 'edgeTTS':
self.set_voice_role(self.target_language.currentText())
# 试听配音
def listen_voice_fun(self):
currentlang = self.target_language.currentText()
if currentlang in ["English", "英语"]:
text = self.cfg['listen_text_en']
lang = "en"
elif currentlang in ["中文简", "中文繁", "Simplified_Chinese", "Traditional_Chinese"]:
text = self.cfg['listen_text_cn']
lang = "zh"
else:
return
role = self.voice_role.currentText()
if not role or role == 'No':
return QMessageBox.critical(self, transobj['anerror'], transobj['mustberole'])
voice_dir = os.environ.get('APPDATA') or os.environ.get('appdata')
if not voice_dir or not os.path.exists(voice_dir):
voice_dir = config.rootdir + "/tmp/voice_tmp"
else:
voice_dir = voice_dir.replace('\\', '/') + "/pyvideotrans"
if not os.path.exists(voice_dir):
os.makedirs(voice_dir)
voice_file = f"{voice_dir}/{self.cfg['tts_type']}-{lang}-{role}.mp3"
obj = {
"text": text,
"rate": "+0%",
"role": role,
"voice_file": voice_file,
"tts_type": self.cfg['tts_type'],
}
from videotrans.task.play_audio import PlayMp3
t = PlayMp3(obj, self)
t.start()
# 显示试听按钮
def show_listen_btn(self, role):
if config.current_status == 'ing' and config.voice_role != 'No' and role == 'No':
QMessageBox.critical(self, transobj['anerror'], '运行中,不可改为无配音角色')
self.voice_role.setCurrentText(config.voice_role)
return
config.voice_role = role
t = self.target_language.currentText()
if role != 'No' and t in ["中文简", "中文繁", "英语", "Simplified_Chinese", "Traditional_Chinese", "English"]:
self.listen_btn.show()
self.listen_btn.setDisabled(False)
else:
self.listen_btn.hide()
self.listen_btn.setDisabled(True)
# 目标语言改变时设置配音角色
def set_voice_role(self, t):
role = self.voice_role.currentText()
# 如果tts类型是 openaiTTS,则角色不变
# 是edgeTTS时需要改变
if self.cfg['tts_type'] != 'edgeTTS':
if role != 'No' and t in ["中文简", "中文繁", "英语", "Simplified_Chinese", "Traditional_Chinese", "English"]:
self.listen_btn.show()
self.listen_btn.setDisabled(False)
else:
self.listen_btn.hide()
self.listen_btn.setDisabled(True)
return
self.listen_btn.hide()
self.voice_role.clear()
if t == '-':
self.voice_role.addItems(['No'])
return
if not config.edgeTTS_rolelist:
self.target_language.setCurrentText('-')
QMessageBox.critical(self, transobj['anerror'], transobj['waitrole'])
return
try:
vt = langlist[t][0].split('-')[0]
if vt not in config.edgeTTS_rolelist:
self.voice_role.addItems(['No'])
return
if len(config.edgeTTS_rolelist[vt]) < 2:
self.target_language.setCurrentText('-')
QMessageBox.critical(self, transobj['anerror'], transobj['waitrole'])
return
self.voice_role.addItems(config.edgeTTS_rolelist[vt])
except:
self.voice_role.addItems([it for item in list(config.edgeTTS_rolelist.values()) for it in item])
# get video filter mp4
def get_mp4(self):
fnames, _ = QFileDialog.getOpenFileNames(self, transobj['selectmp4'], self.last_dir,
"Video files(*.mp4 *.avi *.mov *.mpg *.mkv)")
if len(fnames) < 1:
return
for (i, it) in enumerate(fnames):
fnames[i] = it.replace('\\', '/')
if len(fnames) > 0:
self.source_mp4.setText(fnames[0])
self.settings.setValue("last_dir", os.path.dirname(fnames[0]))
config.queue_mp4 = fnames
# 从本地导入字幕文件
def import_sub_fun(self):
fname, _ = QFileDialog.getOpenFileName(self, transobj['selectmp4'], self.last_dir,
"Srt files(*.srt *.txt)")
if fname:
with open(fname, 'r', encoding='utf-8') as f:
self.subtitle_area.insertPlainText(f.read().strip())
# 保存目录
def get_save_dir(self):
dirname = QFileDialog.getExistingDirectory(self, transobj['selectsavedir'], self.last_dir)
dirname = dirname.replace('\\', '/')
self.target_dir.setText(dirname)
# 检测开始状态并启动
def check_start(self):
if config.current_status == 'ing':
question = show_popup(transobj['exit'], transobj['confirmstop'])
if question == QMessageBox.AcceptRole:
self.update_status('stop')
return
# 清理日志
self.process.clear()