-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasr_gui.py
More file actions
3606 lines (3053 loc) · 142 KB
/
asr_gui.py
File metadata and controls
3606 lines (3053 loc) · 142 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
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
import logging
import os
from pathlib import Path
import platform
import subprocess
import sys
import webbrowser
plugin_path = os.path.join(sys.prefix, 'Lib', 'site-packages', 'PyQt5', 'Qt5', 'plugins')
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path
from PyQt5.QtCore import Qt, QRunnable, QThreadPool, QObject, pyqtSignal as Signal, pyqtSlot as Slot, QSize, QThread, QTimer, pyqtSignal
from PyQt5.QtGui import QCursor, QColor, QFont, QIcon
import requests
from datetime import datetime
import json
from PyQt5.QtWidgets import (QApplication, QWidget, QVBoxLayout, QHBoxLayout, QFileDialog,
QTableWidgetItem, QHeaderView, QSizePolicy)
from qfluentwidgets import (ComboBox, PushButton, LineEdit, TableWidget, FluentIcon as FIF,
Action, RoundMenu, InfoBar, InfoBarPosition,
FluentWindow, BodyLabel, MessageBox, TextEdit, Dialog, SegmentedWidget)
from bk_asr.BcutASR import BcutASR
from bk_asr.JianYingASR import JianYingASR
from bk_asr.KuaiShouASR import KuaiShouASR
# 设置日志配置
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
class WorkerSignals(QObject):
finished = Signal(str, str)
errno = Signal(str, str)
progress = Signal(int, str, str) # (行号, 状态, 消息) 用于显示每个任务的进度
class ASRWorker(QRunnable):
"""ASR处理工作线程"""
def __init__(self, file_path, asr_engine, export_format):
super().__init__()
self.file_path = file_path
self.asr_engine = asr_engine
self.export_format = export_format
self.signals = WorkerSignals()
self.audio_path = None
@Slot()
def run(self):
try:
use_cache = True
# 检查文件类型,如果不是音频则转换
logging.info("[+]正在进ffmpeg转换")
audio_exts = ['.mp3', '.wav']
if not any(self.file_path.lower().endswith(ext) for ext in audio_exts):
temp_audio = self.file_path.rsplit(".", 1)[0] + ".mp3"
if not video2audio(self.file_path, temp_audio):
raise Exception("音频转换失败,确保安装ffmpeg")
self.audio_path = temp_audio
else:
self.audio_path = self.file_path
# 根据选择的 ASR 引擎实例化相应的类
if self.asr_engine == 'B 接口':
asr = BcutASR(self.audio_path, use_cache=use_cache)
elif self.asr_engine == 'J 接口':
asr = JianYingASR(self.audio_path, use_cache=use_cache)
elif self.asr_engine == 'K 接口':
asr = KuaiShouASR(self.audio_path, use_cache=use_cache)
elif self.asr_engine == 'Whisper':
# from bk_asr.WhisperASR import WhisperASR
# asr = WhisperASR(self.file_path, use_cache=use_cache)
raise NotImplementedError("WhisperASR 暂未实现")
else:
raise ValueError(f"未知的 ASR 引擎: {self.asr_engine}")
logging.info(f"开始处理文件: {self.file_path} 使用引擎: {self.asr_engine}")
result = asr.run()
# 根据导出格式选择转换方法
save_ext = self.export_format.lower()
if save_ext == 'srt':
result_text = result.to_srt()
elif save_ext == 'ass':
result_text = result.to_ass()
elif save_ext == 'txt':
result_text = result.to_txt()
logging.info(f"完成处理文件: {self.file_path} 使用引擎: {self.asr_engine}")
save_path = self.file_path.rsplit(".", 1)[0] + "." + save_ext
with open(save_path, "w", encoding="utf-8") as f:
f.write(result_text)
self.signals.finished.emit(self.file_path, result_text)
except Exception as e:
logging.error(f"处理文件 {self.file_path} 时出错: {str(e)}")
self.signals.errno.emit(self.file_path, f"处理时出错: {str(e)}")
class UpdateCheckerThread(QThread):
msg = pyqtSignal(str, str, str) # 用于发送消息的信号
def __init__(self, parent=None):
super().__init__(parent)
def run(self):
try:
from check_update import check_update, check_internet_connection
# 检查互联网连接
if not check_internet_connection():
self.msg.emit("错误", "无法连接到互联网,请检查网络连接。", "")
return
# 检查更新
config = check_update(self)
if config:
if config['fource']:
self.msg.emit("更新", "检测到新版本,请下载最新版本。", config['update_download_url'])
else:
self.msg.emit("可更新", "检测到新版本,请下载最新版本。", config['update_download_url'])
except Exception:
pass
class ASRWidget(QWidget):
"""ASR处理界面"""
def __init__(self):
super().__init__()
self.init_ui()
self.max_threads = 3 # 设置最大线程数
self.thread_pool = QThreadPool()
self.thread_pool.setMaxThreadCount(self.max_threads)
self.processing_queue = []
self.workers = {} # 维护文件路径到worker的映射
def init_ui(self):
layout = QVBoxLayout(self)
# ASR引擎选择区域
engine_layout = QHBoxLayout()
engine_label = BodyLabel("选择接口:", self)
engine_label.setFixedWidth(70)
self.combo_box = ComboBox(self)
self.combo_box.addItems(['B 接口', 'J 接口', 'K 接口', 'Whisper'])
engine_layout.addWidget(engine_label)
engine_layout.addWidget(self.combo_box)
layout.addLayout(engine_layout)
# 导出格式选择区域
format_layout = QHBoxLayout()
format_label = BodyLabel("导出格式:", self)
format_label.setFixedWidth(70)
self.format_combo = ComboBox(self)
self.format_combo.addItems(['SRT', 'TXT', 'ASS'])
format_layout.addWidget(format_label)
format_layout.addWidget(self.format_combo)
layout.addLayout(format_layout)
# 文件选择区域
file_layout = QHBoxLayout()
self.file_input = LineEdit(self)
self.file_input.setPlaceholderText("拖拽文件或文件夹到这里")
self.file_input.setReadOnly(True)
self.file_button = PushButton("选择文件", self)
self.file_button.clicked.connect(self.select_file)
file_layout.addWidget(self.file_input)
file_layout.addWidget(self.file_button)
layout.addLayout(file_layout)
# 文件列表表格
self.table = TableWidget(self)
self.table.setColumnCount(2)
self.table.setHorizontalHeaderLabels(['文件名', '状态'])
self.table.setContextMenuPolicy(Qt.CustomContextMenu)
self.table.customContextMenuRequested.connect(self.show_context_menu)
layout.addWidget(self.table)
# 设置表格列的拉伸模式
header = self.table.horizontalHeader()
header.setSectionResizeMode(0, QHeaderView.Stretch)
header.setSectionResizeMode(1, QHeaderView.Fixed)
self.table.setColumnWidth(1, 100)
self.table.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
# 处理按钮
self.process_button = PushButton("开始处理", self)
self.process_button.clicked.connect(self.process_files)
self.process_button.setEnabled(False) # 初始禁用
layout.addWidget(self.process_button)
self.setAcceptDrops(True)
def select_file(self):
"""选择文件对话框"""
files, _ = QFileDialog.getOpenFileNames(self, "选择音频或视频文件", "",
"Media Files (*.mp3 *.wav *.ogg *.mp4 *.avi *.mov *.ts)")
for file in files:
self.add_file_to_table(file)
self.update_start_button_state()
def add_file_to_table(self, file_path):
"""将文件添加到表格中"""
if self.find_row_by_file_path(file_path) != -1:
InfoBar.warning(
title='文件已存在',
content=f"文件 {os.path.basename(file_path)} 已经添加到列表中。",
orient=Qt.Horizontal,
isClosable=True,
position=InfoBarPosition.TOP,
duration=2000,
parent=self
)
return
row_count = self.table.rowCount()
self.table.insertRow(row_count)
item_filename = self.create_non_editable_item(os.path.basename(file_path))
item_status = self.create_non_editable_item("未处理")
item_status.setForeground(QColor("gray"))
self.table.setItem(row_count, 0, item_filename)
self.table.setItem(row_count, 1, item_status)
item_filename.setData(Qt.UserRole, file_path)
def create_non_editable_item(self, text):
"""创建不可编辑的表格项"""
item = QTableWidgetItem(text)
item.setFlags(item.flags() & ~Qt.ItemIsEditable)
return item
def show_context_menu(self, pos):
"""显示右键菜单"""
current_row = self.table.rowAt(pos.y())
if current_row < 0:
return
self.table.selectRow(current_row)
menu = RoundMenu(parent=self)
reprocess_action = Action(FIF.SYNC, "重新处理")
delete_action = Action(FIF.DELETE, "删除任务")
open_dir_action = Action(FIF.FOLDER, "打开文件目录")
menu.addActions([reprocess_action, delete_action, open_dir_action])
delete_action.triggered.connect(self.delete_selected_row)
open_dir_action.triggered.connect(self.open_file_directory)
reprocess_action.triggered.connect(self.reprocess_selected_file)
menu.exec(QCursor.pos())
def delete_selected_row(self):
"""删除选中的行"""
current_row = self.table.currentRow()
if current_row >= 0:
file_path = self.table.item(current_row, 0).data(Qt.UserRole)
if file_path in self.workers:
worker = self.workers[file_path]
worker.signals.finished.disconnect(self.update_table)
worker.signals.errno.disconnect(self.handle_error)
# QThreadPool 不支持直接终止线程,通常需要设计任务可中断
# 这里仅移除引用
self.workers.pop(file_path, None)
self.table.removeRow(current_row)
self.update_start_button_state()
def open_file_directory(self):
"""打开文件所在目录"""
current_row = self.table.currentRow()
if current_row >= 0:
current_item = self.table.item(current_row, 0)
if current_item:
file_path = current_item.data(Qt.UserRole)
directory = os.path.dirname(file_path)
try:
if platform.system() == "Windows":
os.startfile(directory)
elif platform.system() == "Darwin":
subprocess.Popen(["open", directory])
else:
subprocess.Popen(["xdg-open", directory])
except Exception as e:
InfoBar.error(
title='无法打开目录',
content=str(e),
orient=Qt.Horizontal,
isClosable=True,
position=InfoBarPosition.TOP,
duration=3000,
parent=self
)
def reprocess_selected_file(self):
"""重新处理选中的文件"""
current_row = self.table.currentRow()
if current_row >= 0:
file_path = self.table.item(current_row, 0).data(Qt.UserRole)
status = self.table.item(current_row, 1).text()
if status == "处理中":
InfoBar.warning(
title='当前文件正在处理中',
content="请等待当前文件处理完成后再重新处理。",
orient=Qt.Horizontal,
isClosable=True,
position=InfoBarPosition.TOP,
duration=3000,
parent=self
)
return
self.add_to_queue(file_path)
def add_to_queue(self, file_path):
"""将文件添加到处理队列并更新状态"""
self.processing_queue.append(file_path)
self.process_next_in_queue()
def process_files(self):
"""处理所有未处理的文件"""
for row in range(self.table.rowCount()):
if self.table.item(row, 1).text() == "未处理":
file_path = self.table.item(row, 0).data(Qt.UserRole)
self.processing_queue.append(file_path)
self.process_next_in_queue()
def process_next_in_queue(self):
"""处理队列中的下一个文件"""
while self.thread_pool.activeThreadCount() < self.max_threads and self.processing_queue:
file_path = self.processing_queue.pop(0)
if file_path not in self.workers:
self.process_file(file_path)
def process_file(self, file_path):
"""处理单个文件"""
selected_engine = self.combo_box.currentText()
selected_format = self.format_combo.currentText()
worker = ASRWorker(file_path, selected_engine, selected_format)
worker.signals.finished.connect(self.update_table)
worker.signals.errno.connect(self.handle_error)
self.thread_pool.start(worker)
self.workers[file_path] = worker
row = self.find_row_by_file_path(file_path)
if row != -1:
status_item = self.create_non_editable_item("处理中")
status_item.setForeground(QColor("orange"))
self.table.setItem(row, 1, status_item)
self.update_start_button_state()
def update_table(self, file_path, result):
"""更新表格中文件的处理状态"""
row = self.find_row_by_file_path(file_path)
if row != -1:
item_status = self.create_non_editable_item("已处理")
item_status.setForeground(QColor("green"))
self.table.setItem(row, 1, item_status)
InfoBar.success(
title='处理完成',
content=f"文件 {self.table.item(row, 0).text()} 已处理完成",
orient=Qt.Horizontal,
isClosable=True,
position=InfoBarPosition.TOP,
duration=1500,
parent=self
)
self.workers.pop(file_path, None)
self.process_next_in_queue()
self.update_start_button_state()
def handle_error(self, file_path, error_message):
"""处理错误信息"""
row = self.find_row_by_file_path(file_path)
if row != -1:
item_status = self.create_non_editable_item("错误")
item_status.setForeground(QColor("red"))
self.table.setItem(row, 1, item_status)
InfoBar.error(
title='处理出错',
content=error_message,
orient=Qt.Horizontal,
isClosable=True,
position=InfoBarPosition.TOP,
duration=3000,
parent=self
)
self.workers.pop(file_path, None)
self.process_next_in_queue()
self.update_start_button_state()
def find_row_by_file_path(self, file_path):
"""根据文件路径查找表格中的行号"""
for row in range(self.table.rowCount()):
item = self.table.item(row, 0)
if item.data(Qt.UserRole) == file_path:
return row
return -1
def update_start_button_state(self):
"""根据文件列表更新开始处理按钮的状态"""
has_unprocessed = any(
self.table.item(row, 1).text() == "未处理"
for row in range(self.table.rowCount())
)
self.process_button.setEnabled(has_unprocessed)
def dragEnterEvent(self, event):
"""拖拽进入事件"""
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
def dropEvent(self, event):
"""拖拽释放事件"""
supported_formats = ('.mp3', '.wav', '.ogg', '.flac', '.aac', '.m4a', '.wma', # 音频格式
'.mp4', '.avi', '.mov', '.ts', '.mkv', '.wmv', '.flv', '.webm', '.rmvb') # 视频格式
files = [u.toLocalFile() for u in event.mimeData().urls()]
for file in files:
if os.path.isdir(file):
for root, dirs, files_in_dir in os.walk(file):
for f in files_in_dir:
if f.lower().endswith(supported_formats):
self.add_file_to_table(os.path.join(root, f))
elif file.lower().endswith(supported_formats):
self.add_file_to_table(file)
self.update_start_button_state()
class SrtOptimizerWorker(QRunnable):
"""SRT优化工作线程"""
def __init__(self, srt_path, save_path):
super().__init__()
self.srt_path = srt_path
self.save_path = save_path
self.signals = WorkerSignals()
@Slot()
def run(self):
try:
logging.info(f"开始优化SRT文件: {self.srt_path}")
# 使用 sys.executable 确保我们用的是当前环境的 python
command = [
sys.executable, 'main.py',
'--srt_path', self.srt_path,
'--save_path', self.save_path
]
# 在Windows上,隐藏命令行窗口
startupinfo = None
if platform.system() == "Windows":
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
result = subprocess.run(command, capture_output=True, text=True, check=True, encoding='utf-8', errors='replace', startupinfo=startupinfo)
logging.info(f"SRT文件优化完成: {self.save_path}")
self.signals.finished.emit(self.srt_path, f"优化完成, 已保存到 {self.save_path}")
except subprocess.CalledProcessError as e:
error_output = e.stderr or e.stdout
logging.error(f"优化SRT文件 {self.srt_path} 时出错: {error_output}")
self.signals.errno.emit(self.srt_path, f"优化时出错: {error_output}")
except Exception as e:
logging.error(f"优化SRT文件 {self.srt_path} 时出错: {str(e)}")
self.signals.errno.emit(self.srt_path, f"优化时出错: {str(e)}")
class VideoFrameWorker(QRunnable):
"""视频帧提取工作线程"""
def __init__(self, video_path, output_dir, frame_type='first', timestamp='00:00:00'):
super().__init__()
self.video_path = video_path
self.output_dir = output_dir
self.frame_type = frame_type # 'first', 'last', 'custom'
self.timestamp = timestamp # 具体时间戳,如 '00:00:10'
self.signals = WorkerSignals()
@Slot()
def run(self):
try:
import os
import subprocess
from pathlib import Path
# 确保输出目录存在
Path(self.output_dir).mkdir(parents=True, exist_ok=True)
# 生成输出文件名和提取时间
video_name = os.path.splitext(os.path.basename(self.video_path))[0]
if self.frame_type == 'first':
suffix = "_首帧"
seek_time = '00:00:00'
elif self.frame_type == 'last':
suffix = "_尾帧"
# 获取视频总时长
cmd_duration = ['ffprobe', '-v', 'error', '-show_entries', 'format=duration',
'-of', 'default=noprint_wrappers=1:nokey=1', self.video_path]
result = subprocess.run(cmd_duration, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
if result.returncode == 0:
total_seconds = float(result.stdout.strip())
# 减去1秒作为尾帧时间(保持为数值类型)
seek_seconds = max(0, total_seconds - 1)
# 转换为 HH:MM:SS 格式
hours = int(seek_seconds // 3600)
minutes = int((seek_seconds % 3600) // 60)
seconds = int(seek_seconds % 60)
seek_time = f"{hours:02d}:{minutes:02d}:{seconds:02d}"
else:
seek_time = '00:00:00'
else: # custom
suffix = f"_{self.timestamp}帧"
seek_time = self.timestamp
output_path = os.path.join(self.output_dir, f"{video_name}{suffix}.jpg")
# ffmpeg命令提取指定帧
cmd = [
'ffmpeg',
'-i', self.video_path,
'-vframes', '1',
'-q:v', '2',
'-y',
'-ss', seek_time,
output_path
]
result = subprocess.run(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
encoding='utf-8'
)
if result.returncode == 0:
self.signals.finished.emit(self.video_path, f"成功提取: {os.path.basename(self.video_path)} -> {os.path.basename(output_path)}")
else:
self.signals.errno.emit(self.video_path, f"提取失败: {result.stderr}")
except Exception as e:
self.signals.errno.emit(self.video_path, f"处理出错: {str(e)}")
class VideoResizeWorker(QRunnable):
"""视频尺寸转换工作线程"""
def __init__(self, video_path, output_dir, width=None, height=None, maintain_aspect=True):
super().__init__()
self.video_path = video_path
self.output_dir = output_dir
self.width = width
self.height = height
self.maintain_aspect = maintain_aspect
self.signals = WorkerSignals()
@Slot()
def run(self):
try:
import os
import subprocess
from pathlib import Path
# 确保输出目录存在
Path(self.output_dir).mkdir(parents=True, exist_ok=True)
# 生成输出文件名
video_name = os.path.splitext(os.path.basename(self.video_path))[0]
output_path = os.path.join(self.output_dir, f"{video_name}_resized.mp4")
# 构建ffmpeg命令
cmd = ['ffmpeg', '-i', self.video_path]
# 设置视频尺寸参数
if self.maintain_aspect:
if self.width:
# 只设置宽度,高度自动等比例
cmd.extend(['-vf', f'scale={self.width}:-2'])
elif self.height:
# 只设置高度,宽度自动等比例
cmd.extend(['-vf', f'scale=-2:{self.height}'])
else:
# 设置具体的宽度和高度
if self.width and self.height:
cmd.extend(['-vf', f'scale={self.width}:{self.height}'])
# 添加其他参数
cmd.extend([
'-c:v', 'libx264', # 使用H.264编码
'-preset', 'medium', # 编码速度与质量平衡
'-crf', '23', # 质量参数(越小质量越高)
'-c:a', 'copy', # 音频直接复制
'-y', # 覆盖输出文件
output_path
])
result = subprocess.run(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
encoding='utf-8'
)
if result.returncode == 0:
size_info = ""
if self.width:
size_info += f"宽度:{self.width}"
if self.height:
if size_info:
size_info += ", "
size_info += f"高度:{self.height}"
self.signals.finished.emit(self.video_path, f"成功转换: {os.path.basename(self.video_path)} -> {os.path.basename(output_path)} ({size_info})")
else:
self.signals.errno.emit(self.video_path, f"转换失败: {result.stderr}")
except Exception as e:
self.signals.errno.emit(self.video_path, f"处理出错: {str(e)}")
class VideoToAudioWorker(QRunnable):
"""视频转音频工作线程"""
def __init__(self, video_path, output_dir, audio_format='mp3', audio_quality=2):
super().__init__()
self.video_path = video_path
self.output_dir = output_dir
self.audio_format = audio_format.lower()
self.audio_quality = audio_quality # 音频质量参数
self.signals = WorkerSignals()
@Slot()
def run(self):
try:
import os
import subprocess
from pathlib import Path
# 确保输出目录存在
Path(self.output_dir).mkdir(parents=True, exist_ok=True)
# 生成输出文件名
video_name = os.path.splitext(os.path.basename(self.video_path))[0]
output_path = os.path.join(self.output_dir, f"{video_name}.{self.audio_format}")
# 构建ffmpeg命令
cmd = [
'ffmpeg',
'-i', self.video_path,
'-vn', # 不要视频流
'-acodec', self._get_audio_codec(), # 音频编码器
]
# 根据格式添加质量参数
if self.audio_format == 'mp3':
cmd.extend(['-q:a', str(self.audio_quality)]) # MP3质量 (0-9, 0最好)
elif self.audio_format == 'wav':
# WAV是无损格式,不需要质量参数
pass
elif self.audio_format == 'aac':
cmd.extend(['-b:a', '192k']) # AAC比特率
elif self.audio_format == 'flac':
# FLAC是无损格式
pass
cmd.extend(['-y', output_path])
result = subprocess.run(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
encoding='utf-8'
)
if result.returncode == 0:
self.signals.finished.emit(self.video_path, f"成功转换: {os.path.basename(self.video_path)} -> {os.path.basename(output_path)}")
else:
self.signals.errno.emit(self.video_path, f"转换失败: {result.stderr}")
except Exception as e:
self.signals.errno.emit(self.video_path, f"处理出错: {str(e)}")
def _get_audio_codec(self):
"""根据格式获取对应的音频编码器"""
codec_map = {
'mp3': 'libmp3lame',
'wav': 'pcm_s16le',
'aac': 'aac',
'flac': 'flac',
'm4a': 'aac',
'ogg': 'libvorbis'
}
return codec_map.get(self.audio_format, 'libmp3lame')
class TTSWorker(QRunnable):
"""TTS处理工作线程"""
def __init__(self, text, ref_audio_path, prompt_text, prompt_lang):
super().__init__()
self.text = text
self.ref_audio_path = ref_audio_path
self.prompt_text = prompt_text
self.prompt_lang = prompt_lang
self.signals = WorkerSignals()
@Slot()
def run(self):
try:
output_dir = Path("output")
output_dir.mkdir(exist_ok=True)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"{timestamp}.wav"
save_path = output_dir / filename
url = "http://127.0.0.1:9880/tts"
params = {
"text": self.text,
"text_lang": "zh",
"ref_audio_path": self.ref_audio_path,
"prompt_text": self.prompt_text,
"prompt_lang": self.prompt_lang,
}
logging.info(f"[+]正在请求TTS API: {url} with params: {params}")
response = requests.get(url, params=params, stream=True)
if response.status_code == 200:
with open(save_path, 'wb') as f:
for chunk in response.iter_content(chunk_size=1024):
f.write(chunk)
logging.info(f"[+]音频文件已保存到: {save_path}")
self.signals.finished.emit(str(save_path), self.text)
else:
error_msg = f"API请求失败,状态码: {response.status_code}, 内容: {response.text}"
logging.error(error_msg)
self.signals.errno.emit("API_ERROR", error_msg)
except requests.exceptions.RequestException as e:
error_msg = f"调用TTS API时网络错误: {e}"
logging.error(error_msg)
self.signals.errno.emit("NETWORK_ERROR", error_msg)
except Exception as e:
error_msg = f"处理TTS时发生未知错误: {e}"
logging.error(error_msg)
self.signals.errno.emit("UNKNOWN_ERROR", error_msg)
class VideoFrameWidget(QWidget):
"""视频第一帧提取界面"""
def __init__(self):
super().__init__()
self.frame_type = 'first' # 默认提取首帧
self.timestamp = '00:00:00' # 默认时间戳
self.init_ui()
self.thread_pool = QThreadPool()
self.thread_pool.setMaxThreadCount(3) # 同时最多处理3个视频
self.processing_files = set()
# 初始化按钮样式
self.set_frame_type('first')
def init_ui(self):
layout = QVBoxLayout(self)
# 源文件选择区域
source_layout = QHBoxLayout()
source_label = BodyLabel("源文件:", self)
source_label.setFixedWidth(70)
self.source_input = LineEdit(self)
self.source_input.setPlaceholderText("拖拽视频文件或文件夹到这里")
self.source_input.setReadOnly(True)
self.source_button = PushButton("选择文件", self)
self.source_button.clicked.connect(self.select_source)
source_layout.addWidget(source_label)
source_layout.addWidget(self.source_input)
source_layout.addWidget(self.source_button)
layout.addLayout(source_layout)
# 输出目录选择区域
output_layout = QHBoxLayout()
output_label = BodyLabel("输出目录:", self)
output_label.setFixedWidth(70)
self.output_input = LineEdit(self)
self.output_input.setPlaceholderText("默认保存到 output 目录")
self.output_input.setText(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'output'))
self.output_input.setReadOnly(True)
self.output_button = PushButton("选择目录", self)
self.output_button.clicked.connect(self.select_output_dir)
output_layout.addWidget(output_label)
output_layout.addWidget(self.output_input)
output_layout.addWidget(self.output_button)
layout.addLayout(output_layout)
# 帧选择区域
frame_label = BodyLabel("帧选择:", self)
layout.addWidget(frame_label)
frame_layout = QHBoxLayout()
# 首帧按钮
self.first_frame_btn = PushButton("首帧", self)
self.first_frame_btn.setCheckable(True)
self.first_frame_btn.clicked.connect(lambda: self.set_frame_type('first'))
frame_layout.addWidget(self.first_frame_btn)
# 尾帧按钮
self.last_frame_btn = PushButton("尾帧", self)
self.last_frame_btn.setCheckable(True)
self.last_frame_btn.clicked.connect(lambda: self.set_frame_type('last'))
frame_layout.addWidget(self.last_frame_btn)
# 具体时间帧按钮
self.custom_frame_btn = PushButton("具体时间帧", self)
self.custom_frame_btn.setCheckable(True)
self.custom_frame_btn.clicked.connect(lambda: self.set_frame_type('custom'))
frame_layout.addWidget(self.custom_frame_btn)
# 时间输入框
self.time_input = LineEdit(self)
self.time_input.setPlaceholderText("00:00:10")
self.time_input.setMaximumWidth(100)
self.time_input.setEnabled(False) # 默认禁用
self.time_input.editingFinished.connect(self.on_time_changed)
frame_layout.addWidget(self.time_input)
frame_layout.addStretch()
layout.addLayout(frame_layout)
# 文件列表表格
table_label = BodyLabel("文件列表:", self)
layout.addWidget(table_label)
self.table = TableWidget(self)
self.table.setColumnCount(2)
self.table.setHorizontalHeaderLabels(['文件名', '状态'])
self.table.setContextMenuPolicy(Qt.CustomContextMenu)
self.table.customContextMenuRequested.connect(self.show_context_menu)
layout.addWidget(self.table)
# 设置表格列的拉伸模式
header = self.table.horizontalHeader()
header.setSectionResizeMode(0, QHeaderView.Stretch)
header.setSectionResizeMode(1, QHeaderView.Fixed)
self.table.setColumnWidth(1, 120)
# 日志显示区域
log_label = BodyLabel("处理日志:", self)
layout.addWidget(log_label)
self.log_text = TextEdit(self)
self.log_text.setFixedHeight(150)
self.log_text.setReadOnly(True)
layout.addWidget(self.log_text)
# 处理按钮
self.process_button = PushButton("开始提取", self)
self.process_button.clicked.connect(self.process_videos)
self.process_button.setEnabled(False)
layout.addWidget(self.process_button)
self.setAcceptDrops(True)
def select_source(self):
"""选择源文件或文件夹"""
# 创建一个选择对话框让用户选择文件或文件夹
dialog = Dialog("选择源文件类型", "请选择要添加的类型:", self)
dialog.yesButton.setText("选择文件")
dialog.cancelButton.setText("选择文件夹")
if dialog.exec():
# 选择文件
files, _ = QFileDialog.getOpenFileNames(
self, "选择视频文件", "",
"视频文件 (*.mp4 *.avi *.mov *.mkv *.wmv *.flv *.webm *.ts)"
)
for file in files:
self.add_file_to_table(file)
else:
# 选择文件夹
folder = QFileDialog.getExistingDirectory(self, "选择包含视频的文件夹")
if folder:
self.add_videos_from_folder(folder)
self.update_process_button_state()
def select_output_dir(self):
"""选择输出目录"""
folder = QFileDialog.getExistingDirectory(self, "选择图片保存目录")
if folder:
self.output_input.setText(folder)
self.update_process_button_state()
def set_frame_type(self, frame_type):
"""设置帧类型"""
self.frame_type = frame_type
# 定义按钮样式
active_style = """
PushButton {
background-color: #0078d4;
color: white;
border: none;
border-radius: 5px;
padding: 8px 16px;
min-width: 80px;
}
PushButton:hover {
background-color: #106ebe;
}
"""
inactive_style = """
PushButton {
background-color: #f0f0f0;
color: #333333;
border: 1px solid #d0d0d0;
border-radius: 5px;
padding: 8px 16px;
min-width: 80px;
}
PushButton:hover {
background-color: #e0e0e0;
border: 1px solid #b0b0b0;
}
"""
# 更新按钮状态和样式
self.first_frame_btn.setChecked(frame_type == 'first')
self.first_frame_btn.setStyleSheet(active_style if frame_type == 'first' else inactive_style)
self.last_frame_btn.setChecked(frame_type == 'last')
self.last_frame_btn.setStyleSheet(active_style if frame_type == 'last' else inactive_style)
self.custom_frame_btn.setChecked(frame_type == 'custom')
self.custom_frame_btn.setStyleSheet(active_style if frame_type == 'custom' else inactive_style)
# 启用或禁用时间输入框
self.time_input.setEnabled(frame_type == 'custom')
# 如果是自定义时间,更新时间戳
if frame_type == 'custom':
time_text = self.time_input.text()
if time_text:
self.timestamp = time_text
else:
self.timestamp = '00:00:00'
def on_time_changed(self):
"""时间输入框编辑完成事件"""
text = self.time_input.text().strip()
if self.frame_type == 'custom' and text:
# 如果输入是纯数字(秒数),自动转换为 HH:MM:SS 格式
if ':' not in text and text.isdigit():
total_seconds = int(text)
hours = total_seconds // 3600
minutes = (total_seconds % 3600) // 60
seconds = total_seconds % 60
formatted_time = f"{hours:02d}:{minutes:02d}:{seconds:02d}"
self.time_input.setText(formatted_time)
self.timestamp = formatted_time
# 如果已经是 HH:MM:SS 格式,验证后使用
elif len(text) == 8 and text.count(':') == 2:
parts = text.split(':')
try:
h, m, s = int(parts[0]), int(parts[1]), int(parts[2])
if 0 <= h <= 23 and 0 <= m <= 59 and 0 <= s <= 59:
self.timestamp = text
except ValueError:
pass
def add_videos_from_folder(self, folder):
"""从文件夹添加所有视频文件"""
import os
video_extensions = ('.mp4', '.avi', '.mov', '.mkv', '.wmv', '.flv', '.webm', '.ts')
for root, _, files in os.walk(folder):
for file in files:
if file.lower().endswith(video_extensions):
full_path = os.path.join(root, file)
self.add_file_to_table(full_path)
def add_file_to_table(self, file_path):
"""将文件添加到表格中"""
if self.find_row_by_file_path(file_path) != -1:
InfoBar.warning(
title='文件已存在',
content=f"文件 {os.path.basename(file_path)} 已经添加到列表中。",
orient=Qt.Horizontal,
isClosable=True,
position=InfoBarPosition.TOP,
duration=2000,
parent=self
)
return
row_count = self.table.rowCount()
self.table.insertRow(row_count)