-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathdeffer.py
688 lines (492 loc) · 19.2 KB
/
deffer.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
from datetime import datetime
import math
import os
import re
import shutil
import subprocess
import requests
import json
import base64
import random
import string
import time
from gtts import gTTS
from moviepy.editor import *
from pydub import AudioSegment
from PIL import Image
import edge_tts
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import hashlib
from nltk import *
import subprocess
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
# nltk.download('punkt')
# nltk.download('stopwords')
# nltk.download('averaged_perceptron_tagger')
from moviepy.editor import ImageClip, VideoFileClip
from config import *
# 分割内容
# 保存到文件
# 文本
#
def spliter(novel_text):
# 创建一个名为"txt文件夹"的文件夹(如果不存在)
if not os.path.exists("split_txt"):
os.makedirs("split_txt")
novel_text=clip_to_whole(novel_text)
# 按句子分割文本
sentences = re.split(r'(?<=[.。!?])', novel_text)
# 合并相邻的句子
merged_sentences = []
current_sentence = ""
for sentence in sentences:
current_sentence += sentence
if len(current_sentence) > 50: # 合并后的句子长度超过50个字符时分割
merged_sentences.append(current_sentence)
current_sentence = ""
if current_sentence:
merged_sentences.append(current_sentence)
# 使用TF-IDF计算句子相似度
tfidf_vectorizer = TfidfVectorizer()
tfidf_matrix = tfidf_vectorizer.fit_transform(merged_sentences)
similarities = cosine_similarity(tfidf_matrix)
similarity_threshold=0.7
# 将相似句子组合成场景
scenes = []
current_scene = []
for i in range(len(merged_sentences)):
if not current_scene:
current_scene.append(i)
else:
if similarities[current_scene[0]][i] > similarity_threshold:
current_scene.append(i)
else:
scenes.append(current_scene)
current_scene = [i]
# 保存每个段落到文件
for i, scene in enumerate(scenes):
scene_sentences = [merged_sentences[i] for i in scene]
scene_text = "".join(scene_sentences)
filename = f"split_txt/{i+1}.txt"
with open(filename, 'w', encoding='utf-8') as scene_file:
scene_file.write(scene_text)
print(scene_text)
# 处理最后一段文本,否则会有丢失的情况
if current_scene:
last_scene_text = "".join([merged_sentences[i] for i in current_scene])
last_filename = f"split_txt/{len(scenes)+1}.txt"
with open(last_filename, 'w', encoding='utf-8') as last_scene_file:
last_scene_file.write(last_scene_text)
print(last_scene_text)
# add
def clip_to_whole(content):
res_content = content.replace('"', "'").strip().replace("\n", "")
return res_content
# 只是去掉了换行,二维
def text_clip_to_whole(contents):
res_contents = []
for content in contents:
res_content=clip_to_whole(content)
res_contents.append(res_content)
return res_contents
# 返回的是二维数组
def text_to_voice(contents):
try:
shutil.rmtree('./tts')
os.mkdir('./tts')
except:
pass
voice = []
first=1
for lines in contents:
filenames = []
second=1
for line in lines:
# 执行相应的处理操作,例如:
# line = line.replace('"', "'").strip().replace("\n", "")
# filename = gtts(line)
filename = _edge_tts(first, second, line)
savefile(line,'./tts/'+str(first)+'.'+str(second)+'.txt')
filenames.append(filename)
second+=1
first += 1
voice.append(filenames)
# 获取音频文件名
# 0
# 二维
# def loadvoice():
# result = []
# current_dir = []
# base_path = "./tts"
# with os.scandir(base_path) as entries:
# for entry in entries:
# if entry.is_file() and entry.name.endswith(".mp3"):
# current_dir.append(os.path.join("tts", entry.name))
# current_dir.sort()
# result.append(current_dir)
# return result
#add
# 转换为音频
def _edge_tts(first,second,content):
# voice = 'zh-CN-YunxiNeural'
# rate = '-4%'
# volume = '+0%'
# filename = './tts/%s.mp3' % content
# tts = edge_tts.Communicate(text=content, voice=voice, rate=rate, volume=volume)
# tts.save(filename)
filename = 'tts/'+str(first)+"."+str(second)+'.mp3'
cmd = 'edge-tts --voice zh-CN-YunxiNeural --text "' + content + '" --write-media %s' % filename
res = subprocess.call(cmd, shell=True)
sound = AudioSegment.from_mp3(filename)
duration = int(sound.duration_seconds)
update_name = '%s.%s_duration_%f.mp3' % (filename.split('.')[0],filename.split('.')[1], duration)
os.rename(filename, update_name)
return update_name
# 文本生成图片
# 一维数组
#
def text_to_pic(contents):
i=1
for content in contents:
getTxt2Image(','.join(content),ADDER_PROMOTE,"./",i)
i=i+1
#add
def getTxt2Image(text_prompt,add_prompt,localPythonExePath,num):#文生图核心方法#localPythonExePath是本地存储路径
serverName = "server001"
txt2img_url = Stable_diff_api+'/sdapi/v1/txt2img'
print("提示词:"+text_prompt)
data = {'prompt': text_prompt+add_prompt+'(masterpiece:1,2), best quality, highres, original, extremely detailed wallpaper, perfect lighting,(extremely detailed CG:1.2),',
'brach_size': 1,
# "sampler_name": "DPM++ 2m Karras",#可自行选择
"steps": 20,
"cfg_scale": 8, #提示词相关性
"width" : 740,
"height": 580,
"restore_faces": "true",
"negative_prompt": "NSFW, (worst quality:2), (low quality:2), (normal quality:2), lowres, normal quality, ((monochrome)), ((grayscale)), skin spots, acnes, skin blemishes, age spot, (ugly:1.331), (duplicate:1.331), (morbid:1.21), (mutilated:1.21), (tranny:1.331), mutated hands, (poorly drawn hands:1.5), blurry, (bad anatomy:1.21), (bad proportions:1.331), extra limbs, (disfigured:1.331), (missing arms:1.331), (extra legs:1.331), (fused fingers:1.61051), (too many fingers:1.61051), (unclear eyes:1.331), lowers, bad hands, missing fingers, extra digit,bad hands, missing fingers, (((extra arms and legs))),"
}
data.update(Stable_diff_conf)
response = submit_post(txt2img_url, data)
save_encoded_image(response.json()['images'][0], ".\pictures\image"+str(num)+".png")
print("图片已经生成,并保存在pictures目录中")
#add
def save_encoded_image(b64_image: str, output_path: str):
with open(output_path, "wb") as image_file:
image_file.write(base64.b64decode(b64_image))
#add
def submit_post(url: str, data: dict):#发出Post请求
return requests.post(url, data=json.dumps(data))
# 生成视频
# 传入两个二维数组
#
def handle_movie(voice,txts):
final_duration_clips = []
txts=txts
voice=voice
print(len(voice))
print(len(txts))
for item in voice:
duration_clips = []
for line in item:
filename = str(line.split('/')[-1])
tmp = filename.split('_')
# 获取语音文件名中的时长
duration_clips.append(float(tmp[-1].replace('.mp3', '')))
final_duration_clips.append(duration_clips)
final_clip_all=None
for i in range(len(txts)):
# final_duration_clips[i]
# txts[i]
# voice[i]
# 解说 + 字幕
final_clip=None
for j in range(len(txts[i])):
# 计算当前行的起始时间和结束时间
print("="*50)
print("i, j ---> " + str(i) + ", " + str(j))
print(voice[i][j])
print(txts[i][j])
print(final_duration_clips[i][j])
print(str(i+1)+".png")
start = 0
end = start + final_duration_clips[i][j]
print("strat:"+str(start))
print("strat:"+str(end))
print("="*50)
# 添加语音剪辑
voice_clip = AudioFileClip("./"+voice[i][j]).set_start(start).volumex(1.2)
# 添加字幕剪辑
screensize = (700,200)
# txt_clip = TextClip(txts[i][j], fontsize=30, color='black', font='MaoKenWangXingYuan.ttf', stroke_color='black',stroke_width=0.3,size = screensize, method='caption')
txt_clip = TextClip(txts[i][j], fontsize=30, color='white', font='MaoKenWangXingYuan.ttf',size = screensize, method='caption')
txt_clip = txt_clip.set_pos('bottom').set_duration(final_duration_clips[i][j])
txt_clip = txt_clip.set_start(start).set_end(end)
# 计算视频中每张图片的剪辑长度
# 定义镜头焦点效果
def resize_func(t):
# return 1 + 0.001*t
return 1
# 添加每张图片的剪辑,并设置位置、时长、帧率等属性
image_clip = ImageClip("./pictures/image"+str(i+1)+".png").set_duration(final_duration_clips[i][j]).resize(resize_func).set_position(('center', 'center')).set_fps(25)
# image_clip = image_clip.crossfadein(1).crossfadeout(1)
# 将图像、字幕和音频剪辑合并为一个剪辑,并添加淡入淡出效果
video_clip = CompositeVideoClip([image_clip,txt_clip])
video_clip = video_clip.set_audio(voice_clip)
if final_clip is None: # 添加此行代码
final_clip = video_clip # 添加此行代码
else:
final_clip = concatenate_videoclips([final_clip,video_clip])
if final_clip_all is None: # 添加此行代码
final_clip_all = final_clip # 添加此行代码
# 这一行使后来加的
else:
final_clip_all = concatenate_videoclips([final_clip_all,final_clip])
# 导出视频剪辑
# final_clip.write_videofile("video/"+str(i+1)+".mp4", fps=25)
current_time = datetime.now()
time_string = current_time.strftime("%Y_%m_%d_%H_%M_%S")
print(time_string)
# 这一行使后来加的
final_clip_all.write_videofile("video/"+time_string+".mp4", fps=25)
print("*"*50)
print("完成!")
# 内容翻译
# 一维数组
# 一维数组
def translate(fileContents):
resultered=[]
for fileContent in fileContents:
textTranslatedtoEnglish=json.loads(translater(fileContent))
resulter=textTranslatedtoEnglish['trans_result']
resulter=handleResult(resulter)
resultered.append(resulter)
return resultered
#
#
#
def translater(query):
# Set your own appid/appkey.
appid = '20230925001829106'
appkey = 'GibMVhrk0dwgvV6Lt_iD'
# For list of language codes, please refer to `https://api.fanyi.baidu.com/doc/21`
from_lang = 'zh'
to_lang = 'en'
endpoint = 'http://api.fanyi.baidu.com'
path = '/api/trans/vip/translate'
url = endpoint + path
# Generate salt and sign
def make_md5(s, encoding='utf-8'):
return hashlib.md5(s.encode(encoding)).hexdigest()
salt = random.randint(32768, 65536)
sign = make_md5(appid + query + str(salt) + appkey)
# Build request
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
payload = {'appid': appid, 'q': query, 'from': from_lang, 'to': to_lang, 'salt': salt, 'sign': sign}
# Send request
r = requests.post(url, params=payload, headers=headers)
result = r.json()
time.sleep(0.2)
# Show response
return (json.dumps(result, indent=4, ensure_ascii=False))
# add
def handleResult(resulter):
returned = "" # 初始化一个空字符串用于存储结果
for i in resulter:
# resulter中的每个元素(假设是一个字典)中的dst值取出来,用逗号连接起来,最终返回一个字符串。
returned += i['dst']+"," # 修正这里的索引
return returned
# 提取关键词
# 一维数组
# 二位数组
def use_extract_keywords(texts):
re_texts=[]
for text in texts:
re_text=extract_keywords(text)
re_texts.append(re_text)
return re_texts
#add
def extract_keywords(text):
# 将文本拆分成单词
words = text.split()
# 创建一个用于存储短语的列表
phrases = []
# 创建一个集合用于跟踪已经出现过的单词
seen_words = set()
n=3
# 生成n-gram短语
for i in range(len(words) - n + 1):
phrase = ' '.join(words[i:i + n])
# 检查短语中是否有已经出现的单词
if not any(word in seen_words for word in phrase.split()):
phrases.append(phrase)
# 将短语中的单词添加到已经出现过的单词集合中
seen_words.update(phrase.split())
return phrases
# 将每个段落拆分成句子,一维变二维
# 一维数组
# 二维
def dozen_txt_double(contents):
doublelist=[]
for content in contents:
innerlist = []
lines=split_sentences(content)
for line in lines:
innerlist.append(line)
doublelist.append(innerlist)
return doublelist
# add
# 将每个段落拆分成句子
def split_sentences(text):
# 去除文本中的引号
text = text.replace('"', '').replace('“', '').replace('”', '').replace('‘', '').replace('’', '')
# 将文本按照换行符进行切割
lines = text.split('\n')
# 去除每行开头和结尾的空格,并去除空行
lines = [line.strip() for line in lines if line.strip()]
# 将每个段落拆分成句子
sentences = []
for line in lines:
# 按照句号、问号、感叹号等标点符号进行拆分
sentences.extend(re.split(r'[.!?。?!]+', line))
# 去除句子开头和结尾的空格,并去除空句子
sentences = [sentence.strip() for sentence in sentences if sentence.strip()]
# 返回结果
return sentences
def delete_files_in_folder(folder_path):
try:
# 获取文件夹中的所有文件列表
file_list = os.listdir(folder_path)
# 遍历文件列表并删除每个文件
for filename in file_list:
file_path = os.path.join(folder_path, filename)
if os.path.isfile(file_path):
os.remove(file_path)
print("所有文件已成功删除。")
except Exception as e:
print("发生错误:{str(e)}")
def delete_file(file_name):
if os.path.exists(file_name):
os.remove(file_name)
print("成功删除文件"+file_name)
else:
print("文件\""+file_name+"\"不存在,已经忽略")
def get_all_pic_files(path):
files = []
for i in range(1, get_cont()+1):
filename = f"{i}.png"
file_path = os.path.join(path, filename)
if os.path.exists(file_path):
files.append(file_path)
return files
def savefile(content,name):
# 打开文件,如果文件不存在将会创建新文件
file = open(name, "w",encoding='utf-8')
# 写入内容到文件
file.write(content)
# 关闭文件
file.close()
# def load_voice():
# # 指定文件夹路径
# folder_path = './tts'
# # 获取文件夹中所有以.mp3结尾的文件
# mp3_files = [filename for filename in os.listdir(folder_path) if filename.endswith('.mp3')]
# # 定义一个自定义排序函数,按照文件名中的数字部分进行排序
# def custom_sort(filename):
# # 提取文件名中的数字部分作为排序关键字
# match = re.search(r'\d+', filename)
# if match:
# return int(match.group())
# else:
# return 0 # 如果没有找到数字部分,默认为0
# # 对文件名进行排序
# sorted_files = sorted(mp3_files, key=custom_sort)
# return sorted_files
def load_voice():
# 指定文件夹路径
folder_path = './tts'
# 获取文件夹中所有以.mp3结尾的文件
mp3_files = [filename for filename in os.listdir(folder_path) if filename.endswith('.mp3')]
# 定义一个自定义排序函数,按照文件名中的数字部分进行排序
def custom_sort(filename):
# 提取文件名中的数字部分作为排序关键字
match = re.search(r'\d+\.\d+', filename)
if match:
return tuple(map(int, match.group().split('.')))
else:
return (0, 0) # 如果没有找到数字部分,默认为(0,0)
# 对文件名进行排序
sorted_files = sorted(mp3_files, key=custom_sort)
# 将文件名按照行号分组,存储在二维列表中
voice = []
for filename in sorted_files:
match = re.search(r'(\d+)\.\d+', filename)
if match:
line_num = int(match.group(1))
while line_num > len(voice):
voice.append([])
voice[-1].append(folder_path+'/'+filename)
return voice
# 获取文本
# 0
# 一维
def loadtxt():
contents=[]
for i in range(1,get_cont()+1):
helloFile=open("split_txt/"+str(i)+".txt",encoding='utf-8')
fileContent=helloFile.read()
contents.append(fileContent)
helloFile.close()
return contents
# 获取章节数
def get_cont():
cont=0
with os.scandir('split_txt') as entries:
for entry in entries:
if entry.is_file():
cont=cont+1
return cont
# 读取小说文本
# 文件名
# 文本
def get_whole_file(filename):
with open(filename, 'r', encoding='utf-8') as file:
novel_text = file.read()
return novel_text
# def get_cont2():
# cont=0
# with os.scandir('video') as entries:
# for entry in entries:
# if entry.is_file():
# cont=cont+1
# return cont
# 二维list
def double_txt(contents):
doublelist=[]
for content in contents:
lines=split_sentences(content)
innerlist = []
for line in lines:
innerlist.append(line)
doublelist.append(innerlist)
return doublelist
# 重构图片
# 序号 附加的关键词
# 0
def re_create_pic(num,prom):
delete_file('image'+str(num)+".png")
getTxt2Image(translater(get_txt_by_num(num)),prom,"./",num)
# getTxt2Image(translater(clip_to_whole(get_txt_by_num(num))),prom,"./",num)
#add
def get_txt_by_num(num):
helloFile=open("split_txt/"+str(num)+".txt",encoding='utf-8')
fileContent=helloFile.read()
helloFile.close()
return fileContent
def open_file_content():
os.startfile('content.txt')
def open_floder_video():
os.system(f'start explorer "video"')
def open_floder_pic():
os.system(f'start explorer "pictures"')