-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathwork_space.py
1026 lines (896 loc) · 44.2 KB
/
work_space.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
from tools.audio_remove import audio_remove
from tools.warning_file import WarningFile
import os
import copy
import json
from pytubefix import YouTube
from pytubefix.cli import on_progress
from faster_whisper import WhisperModel
import srt
import re
from pygtrans import Translate
import requests
from tqdm import tqdm
from pydub import AudioSegment
import chardet
import asyncio
import edge_tts
import datetime
from moviepy.editor import VideoFileClip, AudioFileClip, CompositeAudioClip
from moviepy.video.tools.subtitles import SubtitlesClip
import sys
import traceback
import deepl
import wave
import math
import struct
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
from tools.trans_llm import TranslatorClass
import tenacity
from tools.merge_subtitle import SubtitleMerger
import subprocess
PROXY = "127.0.0.1:7890"
proxies = None
TTS_MAX_TRY_TIMES = 16
CHATGPT_URL = "https://api.openai.com/v1/"
GHATGPT_TERMS_FILE = "tools/terms.json"
paramDictTemplate = {
"proxy": "127.0.0.1:7890", # 代理地址,留空则不使用代理
"video Id": "eMlx5fFNoYc", # 油管视频ID
"work path": "conver\\cheak_valve", # 工作目录
"download video": True, # [工作流程开关]下载视频
"download fhd video": True, # [工作流程开关]下载1080p视频
"extract audio": True, # [工作流程开关]提取音频
"audio remove": True, # [工作流程开关]去除音乐
"audio remove model path": "models\\baseline.pth", # 去音乐模型路径
"audio transcribe": True, # [工作流程开关]语音转文字
"audio transcribe model": "base.en", # [工作流程开关]英文语音转文字模型名称
"srt merge": True, # [工作流程开关]字幕合并
"srt merge en to text": True, # [工作流程开关]英文字幕转文字
"srt merge translate": True, # [工作流程开关]字幕翻译
"srt merge translate tool": "google", # 翻译工具,目前支持google和deepl
"srt merge translate key": "", # 翻译工具的key
"srt merge zh to text": True, # [工作流程开关]中文字幕转文字
"srt to voice srouce": True, # [工作流程开关]字幕转语音
"TTS": "edge", # [工作流程开关]合成语音,目前支持edge和GPT-SoVITS
"TTS param": "", # TTS参数,GPT-SoVITS为地址,edge为角色。edge模式下可以不填,建议不要用GPT-SoVITS。
"voice connect": True, # [工作流程开关]语音合并
"audio zh transcribe": True, # [工作流程开关]合成后的语音转文字
"audio zh transcribe model": "medium", # 中文语音转文字模型名称
"video zh preview": True # [工作流程开关]视频预览
}
diagnosisLog = None
executeLog = None
# 默认utf-8编码
os.environ['PYTHONIOENCODING'] = 'utf-8'
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE" # 强制GPU版本cuda
def create_param_template(path):
with open(path, "w", encoding="utf-8") as file:
json.dump(paramDictTemplate, file, indent=4)
def load_param(path):
with open(path, "r", encoding="utf-8") as file:
paramDict = json.load(file)
return paramDict
def download_youtube_video(video_id, fileNameAndPath):
from pytubefix import YouTube
YouTube(f'https://youtu.be/{video_id}', proxies=proxies).streams.first().download(filename=fileNameAndPath)
def transcribeAudioEn(path, modelName="base.en", language="en",srtFilePathAndName="VIDEO_FILENAME.srt"):
# 非静音检测阈值,单位为分贝,越小越严格
NOT_SILENCE_THRESHOLD_DB = -30
END_INTERPUNCTION = ["…", ".", "!", "?", ";"]
NUMBER_CHARACTERS = "0123456789"
# 确保简体中文
initial_prompt=None
if language=="zh":
initial_prompt="简体"
model = WhisperModel(modelName, device="cuda", compute_type="float16", download_root="faster-whisper_models", local_files_only=False)
print("Whisper model loaded.")
# faster-whisper
segments, _ = model.transcribe(audio=path, language=language, word_timestamps=True, initial_prompt=initial_prompt)
# 转换为srt的Subtitle对象
index = 1
subs = []
subtitle = None
for segment in segments:
for word in segment.words:
if subtitle is None:
subtitle = srt.Subtitle(index, datetime.timedelta(seconds=word.start), datetime.timedelta(seconds=word.end), "")
finalWord = word.word.strip()
subtitle.end = datetime.timedelta(seconds=word.end)
# 避免ascii编码错误,不知道怎么写,以后再说吧
# bytes_s = bytes(finalWord, 'latin-1') # Convert the string to bytes using latin-1 encoding
# finalWord = bytes_s.decode('latin-1') # Decode the bytes to a string using utf-8 encoding
# finalWord = finalWord.encode('utf-8')
# 一句结束。但是要特别排除小数点被误认为是一句结尾的情况。
if (finalWord[-1] in END_INTERPUNCTION) and not (len(finalWord)>1 and finalWord[-2] in NUMBER_CHARACTERS):
pushWord = " " +finalWord
subtitle.content += pushWord
subs.append(subtitle)
index += 1
subtitle = None
else:
if subtitle.content == "":
subtitle.content = finalWord
# 如果上一个字符是".",则要考虑小数的可能性
elif finalWord[0] == ".":
subtitle.content = subtitle.content + finalWord
elif len(subtitle.content) > 0 and subtitle.content[-1] == "." and finalWord[0] in NUMBER_CHARACTERS:
subtitle.content = subtitle.content + finalWord
else:
subtitle.content = subtitle.content + " " + finalWord
# 补充最后一个字幕
if subtitle is not None:
subs.append(subtitle)
index += 1
print("Transcription complete.")
# 重新校准字幕开头,以字幕开始时间后声音大于阈值的第一帧为准
audio = wave.open(path, 'rb')
frameRate = audio.getframerate()
notSilenceThreshold = math.pow(10, NOT_SILENCE_THRESHOLD_DB / 20)
for sub in subs:
startTime = sub.start.total_seconds()
startFrame = int(startTime * frameRate)
endTime = sub.end.total_seconds()
endFrame = int(endTime * frameRate)
newStartTime = startTime
audio.setpos(startFrame)
readFrames = endFrame - startFrame
for i in range(readFrames):
frame = audio.readframes(1)
if not frame :
break
samples = struct.iter_unpack("<h", frame)
sampleVolumes = [] # 用于存储每个样本的音量值
for sample_tuple in samples:
# sample是一个样本值
# 调用calculate_volume函数计算样本的音量值,并将结果添加到sampleVolumes列表中
sample = sample_tuple[0]
sample_volume = abs(sample) / 32768
sampleVolumes.append(sample_volume) # 将音量值添加到列表中
# 找出所有样本的音量值中的最大值
maxVolume = max(sampleVolumes)
if maxVolume > notSilenceThreshold:
newStartTime = startTime + i / frameRate
break
sub.start = datetime.timedelta(seconds=newStartTime)
content = srt.compose(subs)
with open(srtFilePathAndName, "w", encoding="utf-8") as file:
file.write(content)
print("SRT file created.")
print("Output file: " + srtFilePathAndName)
return True
def transcribeAudioZh(path, modelName="base.en", language="en",srtFilePathAndName="VIDEO_FILENAME.srt"):
END_INTERPUNCTION = ["。", "!", "?", "…", ";", ",", "、", ",", ".", "!", "?", ";"]
ENGLISH_AND_NUMBER_CHARACTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
model = WhisperModel(modelName, device="cuda", compute_type="float16", download_root="faster-whisper_models", local_files_only=False)
segments, _ = model.transcribe(audio=path, language="zh", word_timestamps=True, initial_prompt="简体")
index = 1
subs = []
for segment in segments:
subtitle = None
for word in segment.words:
if subtitle is None:
subtitle = srt.Subtitle(index, datetime.timedelta(seconds=word.start), datetime.timedelta(seconds=word.end), "")
finalWord = word.word.strip()
subtitle.end = datetime.timedelta(seconds=word.end)
# 排除英文字母+. 情况
if (finalWord[-1] in END_INTERPUNCTION and not(finalWord[-1] == "." and len(finalWord)>1 and finalWord[-2] in ENGLISH_AND_NUMBER_CHARACTERS)) \
or (subtitle is not None and len(subtitle.content) > 20) :
if not ((finalWord[-1] == "." and len(finalWord)>1 and finalWord[-2] in ENGLISH_AND_NUMBER_CHARACTERS) or (subtitle is not None and len(subtitle.content) > 20) ):
pushWord = finalWord[:-1]
else:
pushWord = finalWord
subtitle.content += pushWord
subs.append(subtitle)
index += 1
subtitle = None
else:
subtitle.content += finalWord
if subtitle is not None:
subs.append(subtitle)
index += 1
content = srt.compose(subs)
with open(srtFilePathAndName, "w", encoding="utf-8") as file:
file.write(content)
def srtSentanceMerge(sourceSrtFilePathAndName, OutputSrtFilePathAndName):
srtContent = open(sourceSrtFilePathAndName, "r", encoding="utf-8").read()
subGenerator = srt.parse(srtContent)
subList = list(subGenerator)
if len(subList) == 0:
print("No subtitle found.")
return False
diagnosisLog.write("\n<Sentence Merge Section>", False)
subPorcessingIndex = 1
subItemList = []
subItemProcessing = None
for subItem in subList:
dotIndex = subItem.content.rfind('.')
exclamationIndex = subItem.content.rfind('!')
questionIndex = subItem.content.rfind('?')
endSentenceIndex = max(dotIndex, exclamationIndex, questionIndex)
# 异常情况,句号居然在中间
if endSentenceIndex != -1 and endSentenceIndex != len(subItem.content) - 1:
logString = f"Warning: Sentence (index:{endSentenceIndex}) not end at the end of the subtitle.\n"
logString += f"Content: {subItem.content}"
diagnosisLog.write(logString)
# 以后一个字幕,直接拼接送入就可以了
if subItem == subList[-1]:
if subItemProcessing is None:
subItemProcessing = copy.copy(subItem)
subItemList.append(subItemProcessing)
break
else:
subItemProcessing.end = subItem.end
subItemProcessing.content += subItem.content
subItemList.append(subItemProcessing)
break
# 新处理一串字符,则拷贝
if subItemProcessing is None:
subItemProcessing = copy.copy(subItem)
subItemProcessing.content = '' # 清空内容是为了延续后面拼接的逻辑
subItemProcessing.index = subPorcessingIndex
subItemProcessing.end = subItem.end
subItemProcessing.content += subItem.content
# 如果一句话结束了,就把这一句话送入处理
if endSentenceIndex == len(subItem.content) - 1:
subItemList.append(subItemProcessing)
subItemProcessing = None
subPorcessingIndex += 1
srtContent = srt.compose(subItemList)
# 如果打开错误则返回false
with open(OutputSrtFilePathAndName, "w", encoding="utf-8") as file:
file.write(srtContent)
def srt_to_text(srt_path):
with open(srt_path, "r", encoding="utf-8") as file:
lines = [line.rstrip() for line in file.readlines()]
text = ""
for line in lines:
line = line.replace('\r', '')
if line.isdigit():
continue
if line == "\n":
continue
if line == "":
continue
if re.search('\\d{2}:\\d{2}:\\d{2},\\d{3} --> \\d{2}:\\d{2}:\\d{2},\\d{3}', line):
continue
text += line + '\n'
return text
def googleTrans(texts):
if PROXY == "":
client = Translate()
else:
client = Translate(proxies={'https': proxies['https']})
textsResponse = client.translate(texts, target='zh')
textsTranslated = []
for txtResponse in textsResponse:
textsTranslated.append(txtResponse.translatedText)
return textsTranslated
def deeplTranslate(texts, key):
translator = deepl.Translator(key)
# list to string
textEn = ""
for oneLine in texts:
textEn += oneLine + "\n"
textZh = translator.translate_text(textEn, target_lang="zh")
textZh = str(textZh)
textsZh = textZh.split("\n")
return textsZh
def srtFileGoogleTran(sourceFileNameAndPath, outputFileNameAndPath):
srtContent = open(sourceFileNameAndPath, "r", encoding="utf-8").read()
subGenerator = srt.parse(srtContent)
subTitleList = list(subGenerator)
contentList = []
for subTitle in subTitleList:
contentList.append(subTitle.content)
contentList = googleTrans(contentList)
for i in range(len(subTitleList)):
subTitleList[i].content = contentList[i]
srtContent = srt.compose(subTitleList)
with open(outputFileNameAndPath, "w", encoding="utf-8") as file:
file.write(srtContent)
def srtFileDeeplTran(sourceFileNameAndPath, outputFileNameAndPath, key):
srtContent = open(sourceFileNameAndPath, "r", encoding="utf-8").read()
subGenerator = srt.parse(srtContent)
subTitleList = list(subGenerator)
contentList = []
for subTitle in subTitleList:
contentList.append(subTitle.content)
contentList = deeplTranslate(contentList, key)
for i in range(len(subTitleList)):
subTitleList[i].content = contentList[i]
srtContent = srt.compose(subTitleList)
with open(outputFileNameAndPath, "w", encoding="utf-8") as file:
file.write(srtContent)
def GPTTranslate(texts, key, model, proxies):
translator = TranslatorClass(api_key=key,
base_url=CHATGPT_URL,
model_name=model,
proxies=proxies)
# 加载术语文件
translator.load_terms(GHATGPT_TERMS_FILE)
# list to string
textEn = ""
for oneLine in texts:
textEn += oneLine + "\n"
batch_text = textEn.split("\n")
print("Start to translate by GPT with Batch mode.")
results = translator.translate_batch(batch_text, max_tokens=1200)
textsZh = []
for i, result in enumerate(results, 1):
print(f"Translated text {i}:", result['text_result'])
print(f"Process time {i}:", result['time'])
textsZh.append(result['text_result'])
return textsZh
def srtFileGPTTran(model, proxies, sourceFileNameAndPath, outputFileNameAndPath, key):
srtContent = open(sourceFileNameAndPath, "r", encoding="utf-8").read()
subGenerator = srt.parse(srtContent)
subTitleList = list(subGenerator)
contentList = []
for subTitle in subTitleList:
contentList.append(subTitle.content)
contentList = GPTTranslate(contentList, key, model, proxies)
for i in range(len(subTitleList)):
subTitleList[i].content = contentList[i]
srtContent = srt.compose(subTitleList)
with open(outputFileNameAndPath, "w", encoding="utf-8") as file:
file.write(srtContent)
def stringToVoice(url, string, outputFile):
data = {
"text": string,
"text_language": "zh"
}
response = requests.post(url, json=data)
if response.status_code != 200:
return False
with open(outputFile, "wb") as f:
f.write(response.content)
return True
def srtToVoice(url, srtFileNameAndPath, outputDir):
# create output directory if not exists
if not os.path.exists(outputDir):
os.makedirs(outputDir)
srtContent = open(srtFileNameAndPath, "r", encoding="utf-8").read()
subGenerator = srt.parse(srtContent)
subTitleList = list(subGenerator)
index = 1
fileNames = []
print("Start to convert srt to voice")
with tqdm(total=len(subTitleList)) as pbar:
for subTitle in subTitleList:
string = subTitle.content
fileName = str(index) + ".wav"
outputNameAndPath = os.path.join(outputDir, fileName)
fileNames.append(fileName)
tryTimes = 0
while tryTimes < TTS_MAX_TRY_TIMES:
if not stringToVoice(url, string, outputNameAndPath):
return False
# 获取outputNameAndPath的时间长度
audio = AudioSegment.from_wav(outputNameAndPath)
duration = len(audio)
# 获取最大音量
maxVolume = audio.max_dBFS
# 如果音频长度小于500ms,则重试,应该是数据有问题了
if duration > 600 and maxVolume > -15:
break
tryTimes += 1
if tryTimes >= TTS_MAX_TRY_TIMES:
print(f"Warning Failed to convert {fileName} to voice.")
print(f"Convert {fileName} duration: {duration}ms, max volume: {maxVolume}dB")
index += 1
pbar.update(1) # update progress bar
voiceMapSrt = copy.deepcopy(subTitleList)
for i in range(len(voiceMapSrt)):
voiceMapSrt[i].content = fileNames[i]
voiceMapSrtContent = srt.compose(voiceMapSrt)
voiceMapSrtFileAndPath = os.path.join(outputDir, "voiceMap.srt")
with open(voiceMapSrtFileAndPath, "w", encoding="utf-8") as f:
f.write(voiceMapSrtContent)
srtAtitionalFile = os.path.join(outputDir, "zh.srt")
with open(srtAtitionalFile, "w", encoding="utf-8") as f:
f.write(srtContent)
print("Convert srt to voice successfully")
return True
@tenacity.retry(wait=tenacity.wait_exponential(multiplier=1, min=4, max=10),
stop=tenacity.stop_after_attempt(5),
reraise=True)
def srtToVoiceEdge(srtFileNameAndPath, outputDir, charactor = "zh-CN-XiaoyiNeural"):
# create output directory if not exists
if not os.path.exists(outputDir):
os.makedirs(outputDir)
srtContent = open(srtFileNameAndPath, "r", encoding="utf-8").read()
subGenerator = srt.parse(srtContent)
subTitleList = list(subGenerator)
index = 1
fileNames = []
fileMp3Names = []
async def convertSrtToVoiceEdge(text, path):
print(f"Start to convert srt to voice into {path}, text: {text}")
communicate = edge_tts.Communicate(text, charactor)
await communicate.save(path)
coroutines = []
for subTitle in subTitleList:
fileMp3Name = str(index) + ".mp3"
fileName = str(index) + ".wav"
outputMp3NameAndPath = os.path.join(outputDir, fileMp3Name)
fileMp3Names.append(fileMp3Name)
fileNames.append(fileName)
coroutines.append(convertSrtToVoiceEdge(subTitle.content, outputMp3NameAndPath))
index += 1
# wait for all coroutines to finish
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.gather(*coroutines))
print("\nConvert srt to mp3 voice successfully")
# convert mp3 to wav
for i in range(len(fileMp3Names)):
mp3FileName = fileMp3Names[i]
wavFileName = fileNames[i]
mp3FileAndPath = os.path.join(outputDir, mp3FileName)
wavFileAndPath = os.path.join(outputDir, wavFileName)
sound = AudioSegment.from_mp3(mp3FileAndPath)
sound.export(wavFileAndPath, format="wav")
os.remove(mp3FileAndPath)
voiceMapSrt = copy.deepcopy(subTitleList)
for i in range(len(voiceMapSrt)):
voiceMapSrt[i].content = fileNames[i]
voiceMapSrtContent = srt.compose(voiceMapSrt)
voiceMapSrtFileAndPath = os.path.join(outputDir, "voiceMap.srt")
with open(voiceMapSrtFileAndPath, "w", encoding="utf-8") as f:
f.write(voiceMapSrtContent)
srtAtitionalFile = os.path.join(outputDir, "sub.srt")
with open(srtAtitionalFile, "w", encoding="utf-8") as f:
f.write(srtContent)
print("Convert srt to wav voice successfully")
return True
def zhVideoPreview(videoFileNameAndPath, voiceFileNameAndPath, insturmentFileNameAndPath, srtFileNameAndPath, outputFileNameAndPath):
"""
预览视频
参数:
videoFileNameAndPath (str): 视频文件的路径和文件名
voiceFileNameAndPath (str): 音频文件的路径和文件名
insturmentFileNameAndPath (str): 乐器音频文件的路径和文件名
srtFileNameAndPath (str): 字幕文件的路径和文件名
outputFileNameAndPath (str): 输出文件的路径和文件名
返回:
bool: 如果成功生成预览视频,则返回True,否则返回False
"""
# 从moviepy.editor导入VideoFileClip的创建音-视频剪辑
video_clip = VideoFileClip(videoFileNameAndPath)
# 加载音频
voice_clip = None
if (voiceFileNameAndPath is not None) and os.path.exists(voiceFileNameAndPath):
voice_clip = AudioFileClip(voiceFileNameAndPath)
insturment_clip = None
if (insturmentFileNameAndPath is not None) and os.path.exists(insturmentFileNameAndPath):
insturment_clip = AudioFileClip(insturmentFileNameAndPath)
# 组合音频剪辑
final_audio = None
if voiceFileNameAndPath is not None and insturmentFileNameAndPath is not None:
final_audio = CompositeAudioClip([voice_clip, insturment_clip])
elif voiceFileNameAndPath is not None:
final_audio = voice_clip
elif insturmentFileNameAndPath is not None:
final_audio = insturment_clip
video_clip = video_clip.set_audio(final_audio)
video_clip.write_videofile(outputFileNameAndPath, codec='libx264', audio_codec='aac', remove_temp=True)
return True
def voiceConnect(sourceDir, outputAndPath):
MAX_SPEED_UP = 1.2 # 最大音频加速
MIN_SPEED_UP = 1.05 # 最小音频加速
MIN_GAP_DURATION = 0.1 # 最小间隔时间,单位秒。低于这个间隔时间就认为音频重叠了
if not os.path.exists(sourceDir):
return False
srtMapFileName = "voiceMap.srt"
srtMapFileAndPath = os.path.join(sourceDir, srtMapFileName)
if not os.path.exists(srtMapFileAndPath):
return False
voiceMapSrtContent = ""
with open(srtMapFileAndPath, "r", encoding="utf-8") as f:
voiceMapSrtContent = f.read()
# 确定音频长度
voiceMapSrt = list(srt.parse(voiceMapSrtContent))
duration = voiceMapSrt[-1].end.total_seconds() * 1000
finalAudioFileAndPath = os.path.join(sourceDir, voiceMapSrt[-1].content)
finalAudioEnd = voiceMapSrt[-1].start.total_seconds() * 1000
finalAudioEnd += AudioSegment.from_wav(finalAudioFileAndPath).duration_seconds * 1000
duration = max(duration, finalAudioEnd)
diagnosisLog.write("\n<Voice connect section>", False)
# 初始化一个空的音频段
combined = AudioSegment.silent(duration=duration)
for i in range(len(voiceMapSrt)):
audioFileAndPath = os.path.join(sourceDir, voiceMapSrt[i].content)
audio = AudioSegment.from_wav(audioFileAndPath)
audio = audio.strip_silence(silence_thresh=-40, silence_len=100) # 去除头尾的静音
audioPosition = voiceMapSrt[i].start.total_seconds() * 1000
if i != len(voiceMapSrt) - 1:
# 检查上这一句的结尾到下一句的开头之间是否有静音,如果没有则需要缩小音频
audioEndPosition = audioPosition + audio.duration_seconds * 1000 + MIN_GAP_DURATION *1000
audioNextPosition = voiceMapSrt[i+1].start.total_seconds() * 1000
if audioNextPosition < audioEndPosition:
speedUp = (audio.duration_seconds * 1000 + MIN_GAP_DURATION *1000) / (audioNextPosition - audioPosition)
seconds = audioPosition / 1000.0
timeStr = str(datetime.timedelta(seconds=seconds))
if speedUp > MAX_SPEED_UP:
# 转换为 HH:MM:SS 格式
logStr = f"Warning: The audio {i+1} , at {timeStr} , is too short, speed up is {speedUp}."
diagnosisLog.write(logStr)
# 音频如果提速一个略大于1,则speedup函数可能会出现一个错误的音频,所以这里确定最小的speedup为1.01
if speedUp < MIN_SPEED_UP:
logStr = f"Warning: The audio {i+1} , at {timeStr} , speed up {speedUp} is too near to 1.0. Set to {MIN_SPEED_UP} forcibly."
diagnosisLog.write(logStr)
speedUp = MIN_SPEED_UP
audio = audio.speedup(playback_speed=speedUp)
combined = combined.overlay(audio, position=audioPosition)
combined.export(outputAndPath, format="wav")
return True
def envCheck():
# 检查环境变量中是否包含 ffmpeg
# 尝试调用ffmpeg命令来检查其是否安装
try:
subprocess.run(["ffmpeg", "-version"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
ffmpeg_found = True
except subprocess.CalledProcessError:
# ffmpeg命令存在但执行出错(不太可能发生,除非ffmpeg损坏)
ffmpeg_found = False
except FileNotFoundError:
# ffmpeg命令不存在
ffmpeg_found = False
waringMessage = ""
if not ffmpeg_found:
waringMessage += "未安装ffmpeg,请安装ffmpeg并将其所在目录添加到环境变量PATH中。\n"
if waringMessage:
root = tk.Tk()
root.deiconify() # 隐藏主窗口
messagebox.showwarning("环境依赖警告", waringMessage)
root.destroy() # 销毁主窗口
return False
else:
return True
if __name__ == "__main__":
if not envCheck():
exit(-1)
print("Please input the path and name of the parameter file (json format): ")
root = tk.Tk()
root.deiconify() # 打开主窗口
paramDirPathAndName = filedialog.askopenfilename() # 打开文件选择对话框
root.destroy() # 关闭主窗口
# 检查paramDirPathAndName是否存在,是否为json文件
if not os.path.exists(paramDirPathAndName) or not os.path.isfile(paramDirPathAndName) or not paramDirPathAndName.endswith(".json"):
print("Please select a valid parameter file.")
exit(-1)
# paramDirPathAndName = input("Please input the path and name of the parameter file: ")
if not os.path.exists(paramDirPathAndName):
create_param_template(paramDirPathAndName)
print(f"Parameter file created at {paramDirPathAndName}.")
print("Please edit the file and run the script again.")
exit(0)
paramDict = load_param(paramDirPathAndName)
workPath = paramDict["work path"]
videoId = paramDict["video Id"]
PROXY = paramDict["proxy"]
audioRemoveModelNameAndPath = paramDict["audio remove model path"]
proxies = None if not PROXY else {
'http': f"{PROXY}",
'https': f"{PROXY}",
'socks5': f"{PROXY}"
}
# create the working directory if it does not exist
if not os.path.exists(workPath):
os.makedirs(workPath)
print(f"Directory {workPath} created.")
# 日志
logFileName = "diagnosis.log"
diagnosisLog = WarningFile(os.path.join(workPath, logFileName))
# 执行日志文件的格式为excute_yyyyMMdd_HHmmss.log
logFileName = "execute_" + datetime.datetime.now().strftime("%Y%m%d_%H%M%S") + ".log"
executeLog = WarningFile(os.path.join(workPath, logFileName))
nowString = str(datetime.datetime.now())
executeLog.write(f"Start at: {nowString}")
executeLog.write("Params\n" + json.dumps(paramDict, indent=4) + "\n")
# 下载视频
voiceFileName = f"{videoId}.mp4"
viedoFileNameAndPath = os.path.join(workPath, voiceFileName)
if paramDict["download video"]:
print(f"Downloading video {videoId} to {viedoFileNameAndPath}")
try:
# 如果已经有了,就不下载了
if os.path.exists(viedoFileNameAndPath):
print(f"Video {videoId} already exists.")
executeLog.write(f"[WORK -] Skip downloading video.")
print("Now at: " + str(datetime.datetime.now()))
else:
yt = YouTube(f'https://www.youtube.com/watch?v={videoId}', proxies=proxies, on_progress_callback=on_progress)
video = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').asc().first()
video.download(output_path=workPath, filename=voiceFileName)
# go back to the script directory
executeLog.write(f"[WORK o] Download video {videoId} to {viedoFileNameAndPath} whith {video.resolution}.")
except Exception as e:
logStr = f"[WORK x] Error: Program blocked while downloading video {videoId} to {viedoFileNameAndPath}."
executeLog.write(logStr)
error_str = traceback.format_exception_only(type(e), e)[-1].strip()
executeLog.write(error_str)
sys.exit(-1)
else:
logStr = "[WORK -] Skip downloading video."
executeLog.write(logStr)
# try download more high-definition video
# 需要单独下载最高分辨率视频,因为pytube下载的1080p视频没音频
voiceFhdFileName = f"{videoId}_fhd.mp4"
voiceFhdFileNameAndPath = os.path.join(workPath, voiceFhdFileName)
if paramDict["download fhd video"]:
try:
# 如果已经有了,就不下载了
if os.path.exists(voiceFhdFileNameAndPath):
print(f"Video {videoId} already exists.")
executeLog.write(f"[WORK -] Skip downloading video.")
print("Now at: " + str(datetime.datetime.now()))
else:
print(f"Try to downloading more high-definition video {videoId} to {voiceFhdFileNameAndPath}")
yt = YouTube(f'https://www.youtube.com/watch?v={videoId}', proxies=proxies, on_progress_callback=on_progress)
video = yt.streams.filter(progressive=False, file_extension='mp4').order_by('resolution').desc().first()
video.download(output_path=workPath, filename=voiceFhdFileName)
executeLog.write(f"[WORK o] Download 1080p high-definition {videoId} to {voiceFhdFileNameAndPath} whith {video.resolution}.")
except:
logStr = f"[WORK x] Error: Program blocked while downloading high-definition video {videoId} to {voiceFhdFileNameAndPath} whith {video.resolution}."
executeLog.write(logStr)
logStr = f"Program will not exit for that the error is not critical."
executeLog.write(logStr)
else:
logStr = "[WORK -] Skip downloading high-definition video."
executeLog.write(logStr)
# 打印当前系统时间
print("Now at: " + str(datetime.datetime.now()))
# 视频转声音提取
audioFileName = f"{videoId}.wav"
audioFileNameAndPath = os.path.join(workPath, audioFileName)
if paramDict["extract audio"]:
# remove the audio file if it exists
print(f"Extracting audio from {viedoFileNameAndPath} to {audioFileNameAndPath}")
try:
video = VideoFileClip(viedoFileNameAndPath)
audio = video.audio
audio.write_audiofile(audioFileNameAndPath)
executeLog.write(f"[WORK o] Extract audio from {viedoFileNameAndPath} to {audioFileNameAndPath} successfully.")
except Exception as e:
logStr = f"[WORK x] Error: Program blocked while extracting audio from {viedoFileNameAndPath} to {audioFileNameAndPath}."
executeLog.write(logStr)
error_str = traceback.format_exception_only(type(e), e)[-1].strip()
executeLog.write(error_str)
sys.exit(-1)
else:
logStr = "[WORK -] Skip extracting audio."
executeLog.write(logStr)
# 去除音频中的音乐
voiceName = videoId + "_voice.wav"
voiceNameAndPath = os.path.join(workPath, voiceName)
insturmentName = videoId + "_insturment.wav"
insturmentNameAndPath = os.path.join(workPath, insturmentName)
if paramDict["audio remove"]:
print(f"Removing music from {audioFileNameAndPath} to {voiceNameAndPath} and {insturmentNameAndPath}")
try:
audio_remove(audioFileNameAndPath, voiceNameAndPath, insturmentNameAndPath, audioRemoveModelNameAndPath)
executeLog.write(f"[WORK o] Remove music from {audioFileNameAndPath} to {voiceNameAndPath} and {insturmentNameAndPath} successfully.")
except Exception as e:
logStr = f"[WORK x] Error: Program blocked while removing music from {audioFileNameAndPath} to {voiceNameAndPath} and {insturmentNameAndPath}."
executeLog.write(logStr)
error_str = traceback.format_exception_only(type(e), e)[-1].strip()
executeLog.write(error_str)
sys.exit(-1)
else:
logStr = "[WORK -] Skip removing music."
executeLog.write(logStr)
# 语音转文字
srtEnFileName = videoId + "_en.srt"
srtEnFileNameAndPath = os.path.join(workPath, srtEnFileName)
if paramDict["audio transcribe"]:
try:
print(f"Transcribing audio from {voiceNameAndPath} to {srtEnFileNameAndPath}")
transcribeAudioEn(voiceNameAndPath, paramDict["audio transcribe model"], "en", srtEnFileNameAndPath)
executeLog.write(f"[WORK o] Transcribe audio from {voiceNameAndPath} to {srtEnFileNameAndPath} successfully.")
except Exception as e:
logStr = f"[WORK x] Error: Program blocked while transcribing audio from {voiceNameAndPath} to {srtEnFileNameAndPath}."
executeLog.write(logStr)
error_str = traceback.format_exception_only(type(e), e)[-1].strip()
executeLog.write(error_str)
sys.exit(-1)
else:
logStr = "[WORK -] Skip transcription."
executeLog.write(logStr)
# 字幕语句合并
srtEnFileNameMerge = videoId + "_en_merge.srt"
srtEnFileNameMergeAndPath = os.path.join(workPath, srtEnFileNameMerge)
if paramDict["srt merge"]:
try:
print(f"Merging sentences in {srtEnFileNameAndPath} to {srtEnFileNameMergeAndPath}")
srtSentanceMerge(srtEnFileNameAndPath, srtEnFileNameMergeAndPath)
executeLog.write(f"[WORK o] Merge sentences in {srtEnFileNameAndPath} to {srtEnFileNameMergeAndPath} successfully.")
except Exception as e:
logStr = f"[WORK x] Error: Program blocked while merging sentences in {srtEnFileNameAndPath} to {srtEnFileNameMergeAndPath}."
executeLog.write(logStr)
error_str = traceback.format_exception_only(type(e), e)[-1].strip()
executeLog.write(error_str)
sys.exit(-1)
else:
logStr = "[WORK -] Skip sentence merge."
executeLog.write(logStr)
# 英文字幕转文字
tetEnFileName = videoId + "_en_merge.txt"
tetEnFileNameAndPath = os.path.join(workPath, tetEnFileName)
if paramDict["srt merge en to text"]:
try:
enText = srt_to_text(srtEnFileNameMergeAndPath)
print(f"Writing EN text to {tetEnFileNameAndPath}")
with open(tetEnFileNameAndPath, "w") as file:
file.write(enText)
executeLog.write(f"[WORK o] Write EN text to {tetEnFileNameAndPath} successfully.")
except Exception as e:
logStr = f"[WORK x] Error: Writing EN text to {tetEnFileNameAndPath} failed."
executeLog.write(logStr)
error_str = traceback.format_exception_only(type(e), e)[-1].strip()
executeLog.write(error_str)
# 这不是关键步骤,所以不退出程序
logStr = f"Program will not exit for that the error is not critical."
executeLog.write(logStr)
else:
logStr = "[WORK -] Skip writing EN text."
executeLog.write(logStr)
# 字幕翻译
srtZhFileName = videoId + "_zh_merge.srt"
srtZhFileNameAndPath = os.path.join(workPath, srtZhFileName)
if paramDict["srt merge translate"]:
try:
print(f"Translating subtitle from {srtEnFileNameMergeAndPath} to {srtZhFileNameAndPath}")
if paramDict["srt merge translate tool"] == "deepl":
if paramDict["srt merge translate key"] == "":
logStr = "[WORK x] Error: DeepL API key is not provided. Please provide it in the parameter file."
executeLog.write(logStr)
sys.exit(-1)
srtFileDeeplTran(srtEnFileNameMergeAndPath, srtZhFileNameAndPath, paramDict["srt merge translate key"])
elif 'gpt' in paramDict["srt merge translate tool"]:
if paramDict['srt merge translate key'] == '':
logStr = "[WORK x] Error: GPT API key is not provided. Please provide it in the parameter file."
executeLog.write(logStr)
sys.exit(-1)
srtFileGPTTran(paramDict['srt merge translate tool'],
proxies,
srtEnFileNameMergeAndPath,
srtZhFileNameAndPath,
paramDict['srt merge translate key'])
else:
srtFileGoogleTran(srtEnFileNameMergeAndPath, srtZhFileNameAndPath)
executeLog.write(f"[WORK o] Translate subtitle from {srtEnFileNameMergeAndPath} to {srtZhFileNameAndPath} successfully.")
except Exception as e:
logStr = f"[WORK x] Error: Program blocked while translating subtitle from {srtEnFileNameMergeAndPath} to {srtZhFileNameAndPath}."
executeLog.write(logStr)
error_str = traceback.format_exception_only(type(e), e)[-1].strip()
executeLog.write(error_str)
sys.exit(-1)
else:
logStr = "[WORK -] Skip subtitle translation."
executeLog.write(logStr)
# 中文字幕转文字
textZhFileName = videoId + "_zh_merge.txt"
textZhFileNameAndPath = os.path.join(workPath, textZhFileName)
if paramDict["srt merge zh to text"]:
try:
zhText = srt_to_text(srtZhFileNameAndPath)
print(f"Writing ZH text to {textZhFileNameAndPath}")
with open(textZhFileNameAndPath, "w", encoding="utf-8") as file:
file.write(zhText)
executeLog.write(f"[WORK o] Write ZH text to {textZhFileNameAndPath} successfully.")
except Exception as e:
logStr = f"[WORK x] Error: Writing ZH text to {textZhFileNameAndPath} failed."
executeLog.write(logStr)
error_str = traceback.format_exception_only(type(e), e)[-1].strip()
executeLog.write(error_str)
# 这不是关键步骤,所以不退出程序
logStr = f"Program will not exit for that the error is not critical."
executeLog.write(logStr)
else:
logStr = "[WORK -] Skip writing ZH text."
executeLog.write(logStr)
# 字幕转语音
ttsSelect = paramDict["TTS"]
voiceDir = os.path.join(workPath, videoId + "_zh_source")
voiceSrcSrtName = "zh.srt"
voiceSrcSrtNameAndPath = os.path.join(voiceDir, voiceSrcSrtName)
voiceSrcMapName = "voiceMap.srt"
voiceSrcMapNameAndPath = os.path.join(voiceDir, voiceSrcMapName)
if paramDict["srt to voice srouce"]:
try:
if ttsSelect == "GPT-SoVITS":
print(f"Converting subtitle to voice by GPT-SoVITS in {srtZhFileNameAndPath} to {voiceDir}")
voiceUrl = paramDict["TTS param"]
srtToVoice(voiceUrl, srtZhFileNameAndPath, voiceDir)
else:
charator = paramDict["TTS param"]
if charator == "":
srtToVoiceEdge(srtZhFileNameAndPath, voiceDir)
else:
srtToVoiceEdge(srtZhFileNameAndPath, voiceDir, charator)
print(f"Converting subtitle to voice by EdgeTTS in {srtZhFileNameAndPath} to {voiceDir}")
executeLog.write(f"[WORK o] Convert subtitle to voice in {srtZhFileNameAndPath} to {voiceDir} successfully.")
except Exception as e:
logStr = f"[WORK x] Error: Program blocked while converting subtitle to voice in {srtZhFileNameAndPath} to {voiceDir}."
executeLog.write(logStr)
error_str = traceback.format_exception_only(type(e), e)[-1].strip()
executeLog.write(error_str)
sys.exit(-1)
else:
logStr = "[WORK -] Skip voice conversion."
executeLog.write(logStr)
# 语音合并
voiceConnectedName = videoId + "_zh.wav"
voiceConnectedNameAndPath = os.path.join(workPath, voiceConnectedName)
if paramDict["voice connect"]:
try:
print(f"Connecting voice in {voiceDir} to {voiceConnectedNameAndPath}")
ret = voiceConnect(voiceDir, voiceConnectedNameAndPath)
if ret == True:
executeLog.write(f"[WORK o] Connect voice in {voiceDir} to {voiceConnectedNameAndPath} successfully.")
else:
executeLog.write(f"[WORK x] Connect voice in {voiceDir} to {voiceConnectedNameAndPath} failed.")
sys.exit(-1)
except Exception as e:
logStr = f"[WORK x] Error: Program blocked while connecting voice in {voiceDir} to {voiceConnectedNameAndPath}."
executeLog.write(logStr)
error_str = traceback.format_exception_only(type(e), e)[-1].strip()
executeLog.write(error_str)
sys.exit(-1)
else:
logStr = "[WORK -] Skip voice connection."
executeLog.write(logStr)
# 合成后的语音转文字
srtVoiceFileName = videoId + "_zh.srt"
srtVoiceFileNameAndPath = os.path.join(workPath, srtVoiceFileName)
if paramDict["audio zh transcribe"]:
try:
if os.path.exists(srtVoiceFileNameAndPath):
print("srtVoiceFileNameAndPath exists.")
else:
print(f"Transcribing audio from {voiceConnectedNameAndPath} to {srtVoiceFileNameAndPath}")
transcribeAudioZh(voiceConnectedNameAndPath, paramDict["audio zh transcribe model"] ,"zh", srtVoiceFileNameAndPath)
executeLog.write(f"[WORK o] Transcribe audio from {voiceConnectedNameAndPath} to {srtVoiceFileNameAndPath} successfully.")
except Exception as e:
logStr = f"[WORK x] Error: Program blocked while transcribing audio from {voiceConnectedNameAndPath} to {srtVoiceFileNameAndPath}."
executeLog.write(logStr)
error_str = traceback.format_exception_only(type(e), e)[-1].strip()
executeLog.write(error_str)
sys.exit(-1)
else:
logStr = "[WORK -] Skip transcription."
executeLog.write(logStr)
# 合成预览视频
previewVideoName = videoId + "_preview.mp4"
previewVideoNameAndPath = os.path.join(workPath, previewVideoName)
if paramDict["video zh preview"]:
try:
sourceVideoNameAndPath = ""
if os.path.exists(voiceFhdFileNameAndPath):
sourceVideoNameAndPath = voiceFhdFileNameAndPath