-
Notifications
You must be signed in to change notification settings - Fork 509
/
main.py
430 lines (381 loc) · 19.8 KB
/
main.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
# Copyright © [2024] 程序那些事
#
# All rights reserved. This software and associated documentation files (the "Software") are provided for personal and educational use only. Commercial use of the Software is strictly prohibited unless explicit permission is obtained from the author.
#
# Permission is hereby granted to any person to use, copy, and modify the Software for non-commercial purposes, provided that the following conditions are met:
#
# 1. The original copyright notice and this permission notice must be included in all copies or substantial portions of the Software.
# 2. Modifications, if any, must retain the original copyright information and must not imply that the modified version is an official version of the Software.
# 3. Any distribution of the Software or its modifications must retain the original copyright notice and include this permission notice.
#
# For commercial use, including but not limited to selling, distributing, or using the Software as part of any commercial product or service, you must obtain explicit authorization from the author.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# Author: 程序那些事
# email: [email protected]
# Website: [www.flydean.com](http://www.flydean.com)
# GitHub: [https://github.com/ddean2009/MoneyPrinterPlus](https://github.com/ddean2009/MoneyPrinterPlus)
#
# All rights reserved.
#
#
import os
import streamlit as st
from config.config import my_config, audio_voices_azure, audio_voices_ali, audio_voices_tencent
from services.audio.alitts_service import AliAudioService
from services.audio.azure_service import AzureAudioService
from services.audio.chattts_service import ChatTTSAudioService
from services.audio.gptsovits_service import GPTSoVITSAudioService
from services.audio.tencent_tts_service import TencentAudioService
from services.captioning.captioning_service import generate_caption, add_subtitles
from services.hunjian.hunjian_service import concat_audio_list, get_audio_and_video_list, get_audio_and_video_list_local
from services.llm.azure_service import MyAzureService
from services.llm.baichuan_service import MyBaichuanService
from services.llm.baidu_qianfan_service import BaiduQianfanService
from services.llm.deepseek_service import MyDeepSeekService
from services.llm.kimi_service import MyKimiService
from services.llm.llm_provider import get_llm_provider
from services.llm.ollama_service import OllamaService
from services.llm.openai_service import MyOpenAIService
from services.llm.tongyi_service import MyTongyiService
from services.resource.pexels_service import PexelsService
from services.resource.pixabay_service import PixabayService
from services.sd.sd_service import SDService
from services.video.merge_service import merge_get_video_list, VideoMergeService, merge_generate_subtitle
from services.video.video_service import get_audio_duration, VideoService, VideoMixService
from tools.tr_utils import tr
from tools.utils import random_with_system_time, get_must_session_option, extent_audio
# 获取当前脚本的绝对路径
script_path = os.path.abspath(__file__)
# print("当前脚本的绝对路径是:", script_path)
# 脚本所在的目录
script_dir = os.path.dirname(script_path)
# 音频输出目录
audio_output_dir = os.path.join(script_dir, "./work")
audio_output_dir = os.path.abspath(audio_output_dir)
def get_audio_voices():
selected_audio_provider = my_config['audio']['provider']
if selected_audio_provider == 'Azure':
return audio_voices_azure
if selected_audio_provider == 'Ali':
return audio_voices_ali
if selected_audio_provider == 'Tencent':
return audio_voices_tencent
def get_resource_provider():
resource_provider = my_config['resource']['provider']
print("resource_provider:", resource_provider)
if resource_provider == "pexels":
return PexelsService()
if resource_provider == "pixabay":
return PixabayService()
if resource_provider == "stableDiffusion":
return SDService()
def get_audio_service():
selected_audio_provider = my_config['audio']['provider']
if selected_audio_provider == "Azure":
return AzureAudioService()
if selected_audio_provider == "Ali":
return AliAudioService()
if selected_audio_provider == "Tencent":
return TencentAudioService()
def main_generate_video_content():
print("main_generate_video_content begin")
topic = get_must_session_option('video_subject', "请输入要生成的主题")
if topic is None:
return
video_language = st.session_state.get('video_language')
video_length = st.session_state.get('video_length')
llm_provider = my_config['llm']['provider']
print("llm_provider:", llm_provider)
llm_service = get_llm_provider(llm_provider)
st.session_state["video_content"] = llm_service.generate_content(topic,
llm_service.topic_prompt_template,
video_language,
video_length)
st.session_state["video_keyword"] = llm_service.generate_content(st.session_state["video_content"],
prompt_template=llm_service.keyword_prompt_template)
print("keyword:", st.session_state.get("video_keyword"))
print("main_generate_video_content end")
def main_try_test_local_audio():
print("main_try_test_local_audio begin")
selected_local_audio_tts_provider = my_config['audio'].get('local_tts', {}).get('provider', '')
video_content = "你好,我是程序那些事"
if selected_local_audio_tts_provider == "chatTTS":
audio_service = ChatTTSAudioService()
if selected_local_audio_tts_provider == "GPTSoVITS":
audio_service = GPTSoVITSAudioService()
audio_service.read_with_content(video_content)
def main_try_test_audio():
print("main_try_test_audio begin")
audio_service = get_audio_service()
audio_rate = get_audio_rate()
audio_language = st.session_state.get("audio_language")
if audio_language == "en-US":
video_content = "hello,this is flydean"
else:
video_content = "你好,我是程序那些事"
audio_voice = get_must_session_option("audio_voice", "请先设置配音语音")
if audio_voice is None:
return
audio_service.read_with_ssml(video_content,
audio_voice,
audio_rate)
def main_generate_video_dubbing():
print("main_generate_video_dubbing begin")
video_content = get_must_session_option("video_content", "请先设置视频主题")
if video_content is None:
return
temp_file_name = random_with_system_time()
audio_output_file = os.path.join(audio_output_dir, str(temp_file_name) + ".wav")
st.session_state["audio_output_file"] = audio_output_file
if st.session_state.get("audio_type") == "remote":
print("use remote audio")
audio_service = get_audio_service()
audio_rate = get_audio_rate()
audio_voice = get_must_session_option("audio_voice", "请先设置配音语音")
if audio_voice is None:
return
audio_service.save_with_ssml(video_content,
audio_output_file,
audio_voice,
audio_rate)
else:
print("use local audio")
selected_local_audio_tts_provider = my_config['audio'].get('local_tts', {}).get('provider', '')
audio_service = None
if selected_local_audio_tts_provider == "chatTTS":
audio_service = ChatTTSAudioService()
if selected_local_audio_tts_provider == "GPTSoVITS":
audio_service = GPTSoVITSAudioService()
audio_service.chat_with_content(video_content, audio_output_file)
# 语音扩展2秒钟,防止突然结束很突兀
extent_audio(audio_output_file, 2)
print("main_generate_video_dubbing end")
def main_generate_video_dubbing_for_mix():
print("main_generate_video_dubbing_for_mix begin")
if st.session_state.get("audio_type") == "remote":
print("use remote audio")
audio_service = get_audio_service()
audio_rate = get_audio_rate()
audio_output_file_list, video_dir_list = get_audio_and_video_list(audio_service, audio_rate)
else:
print("use local audio")
selected_local_audio_tts_provider = my_config['audio'].get('local_tts', {}).get('provider', '')
audio_service = None
if selected_local_audio_tts_provider == "chatTTS":
audio_service = ChatTTSAudioService()
if selected_local_audio_tts_provider == "GPTSoVITS":
audio_service = GPTSoVITSAudioService()
audio_output_file_list, video_dir_list = get_audio_and_video_list_local(audio_service)
st.session_state["audio_output_file_list"] = audio_output_file_list
st.session_state["video_dir_list"] = video_dir_list
print("main_generate_video_dubbing_for_mix end")
def get_audio_rate():
audio_provider = my_config['audio']['provider']
if audio_provider == "Azure":
audio_speed = st.session_state.get("audio_speed")
if audio_speed == "normal":
audio_rate = "0.00"
if audio_speed == "fast":
audio_rate = "10.00"
if audio_speed == "slow":
audio_rate = "-10.00"
if audio_speed == "faster":
audio_rate = "20.00"
if audio_speed == "slower":
audio_rate = "-20.00"
if audio_speed == "fastest":
audio_rate = "30.00"
if audio_speed == "slowest":
audio_rate = "-30.00"
return audio_rate
if audio_provider == "Ali":
audio_speed = st.session_state.get("audio_speed")
if audio_speed == "normal":
audio_rate = "0"
if audio_speed == "fast":
audio_rate = "150"
if audio_speed == "slow":
audio_rate = "-150"
if audio_speed == "faster":
audio_rate = "250"
if audio_speed == "slower":
audio_rate = "-250"
if audio_speed == "fastest":
audio_rate = "400"
if audio_speed == "slowest":
audio_rate = "-400"
return audio_rate
if audio_provider == "Tencent":
audio_speed = st.session_state.get("audio_speed")
if audio_speed == "normal":
audio_rate = "0"
if audio_speed == "fast":
audio_rate = "1"
if audio_speed == "slow":
audio_rate = "-1"
if audio_speed == "faster":
audio_rate = "1.5"
if audio_speed == "slower":
audio_rate = "-1.5"
if audio_speed == "fastest":
audio_rate = "2"
if audio_speed == "slowest":
audio_rate = "-2"
return audio_rate
def main_get_video_resource():
print("main_get_video_resource begin")
resource_service = get_resource_provider()
query = get_must_session_option("video_keyword", "请先设置视频关键字")
if query is None:
return
audio_file = get_must_session_option("audio_output_file", "请先生成配音文件")
if audio_file is None:
return
audio_length = get_audio_duration(audio_file)
print("audio_length:", audio_length)
return_videos, total_length = resource_service.handle_video_resource(query, audio_length, 50, False)
st.session_state["return_videos"] = return_videos
return return_videos, audio_file
def main_generate_subtitle():
print("main_generate_subtitle begin:")
enable_subtitles = st.session_state.get("enable_subtitles")
if enable_subtitles:
# 设置输出字幕
random_name = random_with_system_time()
captioning_output = os.path.join(audio_output_dir, f"{random_name}.srt")
st.session_state["captioning_output"] = captioning_output
audio_output_file = get_must_session_option("audio_output_file", "请先生成视频对应的语音文件")
generate_caption()
def main_generate_ai_video(video_generator):
print("main_generate_ai_video begin:")
with video_generator:
st_area = st.status(tr("Generate Video in process..."), expanded=True)
with st_area as status:
st.write(tr("Generate Video Dubbing..."))
main_generate_video_dubbing()
st.write(tr("Generate Video subtitles..."))
main_generate_subtitle()
st.write(tr("Get Video Resource..."))
main_get_video_resource()
st.write(tr("Video normalize..."))
audio_file = get_must_session_option("audio_output_file", "请先生成配音文件")
if audio_file is None:
return
video_list = get_must_session_option("return_videos", "请先生成视频资源文件")
if video_list is None:
return
video_service = VideoService(video_list, audio_file)
print("normalize video")
video_service.normalize_video()
st.write(tr("Generate Video..."))
video_file = video_service.generate_video_with_audio()
print("final file without subtitle:", video_file)
enable_subtitles = st.session_state.get("enable_subtitles")
if enable_subtitles:
st.write(tr("Add Subtitles..."))
subtitle_file = get_must_session_option('captioning_output', "请先生成字幕文件")
if subtitle_file is None:
return
font_name = st.session_state.get('subtitle_font')
font_size = st.session_state.get('subtitle_font_size')
primary_colour = st.session_state.get('subtitle_color')
outline_colour = st.session_state.get('subtitle_border_color')
outline = st.session_state.get('subtitle_border_width')
alignment = st.session_state.get('subtitle_position')
add_subtitles(video_file, subtitle_file,
font_name=font_name,
font_size=font_size,
primary_colour=primary_colour,
outline_colour=outline_colour,
outline=outline,
alignment=alignment)
print("final file with subtitle:", video_file)
st.session_state["result_video_file"] = video_file
status.update(label=tr("Generate Video completed!"), state="complete", expanded=False)
def main_generate_ai_video_for_mix(video_generator):
print("main_generate_ai_video_for_mix begin:")
with video_generator:
st_area = st.status(tr("Generate Video in process..."), expanded=True)
with st_area as status:
st.write(tr("Generate Video Dubbing..."))
main_generate_video_dubbing_for_mix()
st.write(tr("Video normalize..."))
video_dir_list = get_must_session_option("video_dir_list", "请选择视频目录路径")
audio_file_list = get_must_session_option("audio_output_file_list", "请先生成配音文件列表")
video_mix_servie = VideoMixService()
# 使用 zip() 函数遍历两个列表并获得配对
i = 0
audio_output_file_list = []
final_video_file_list = []
for video_dir, audio_file in zip(video_dir_list, audio_file_list):
print(f"Video Directory: {video_dir}, Audio File: {audio_file}")
if i == 0:
matching_videos, total_length = video_mix_servie.match_videos_from_dir(video_dir,
audio_file, True)
else:
matching_videos, total_length = video_mix_servie.match_videos_from_dir(video_dir,
audio_file, False)
i = i + 1
audio_output_file_list.append(audio_file)
final_video_file_list.extend(matching_videos)
final_audio_output_file = concat_audio_list(audio_output_file_list)
st.session_state['audio_output_file'] = final_audio_output_file
st.write(tr("Generate Video subtitles..."))
main_generate_subtitle()
video_service = VideoService(final_video_file_list, final_audio_output_file)
print("normalize video")
video_service.normalize_video()
st.write(tr("Generate Video..."))
video_file = video_service.generate_video_with_audio()
print("final file without subtitle:", video_file)
enable_subtitles = st.session_state.get("enable_subtitles")
if enable_subtitles:
st.write(tr("Add Subtitles..."))
subtitle_file = get_must_session_option('captioning_output', "请先生成字幕文件")
if subtitle_file is None:
return
font_name = st.session_state.get('subtitle_font')
font_size = st.session_state.get('subtitle_font_size')
primary_colour = st.session_state.get('subtitle_color')
outline_colour = st.session_state.get('subtitle_border_color')
outline = st.session_state.get('subtitle_border_width')
alignment = st.session_state.get('subtitle_position')
add_subtitles(video_file, subtitle_file,
font_name=font_name,
font_size=font_size,
primary_colour=primary_colour,
outline_colour=outline_colour,
outline=outline,
alignment=alignment)
print("final file with subtitle:", video_file)
st.session_state["result_video_file"] = video_file
status.update(label=tr("Generate Video completed!"), state="complete", expanded=False)
def main_generate_ai_video_from_img(video_generator):
print("main_generate_ai_video_from_img begin:")
with video_generator:
st_area = st.status(tr("Generate Video in process..."), expanded=True)
with st_area as status:
sd_service = SDService()
video_content = st.session_state.get('video_content')
video_list, audio_list, text_list = sd_service.sd_get_video_list(video_content)
pass
pass
def main_generate_ai_video_for_merge(video_generator):
print("main_generate_ai_video_for_merge begin:")
with video_generator:
st_area = st.status(tr("Generate Video in process..."), expanded=True)
with st_area as status:
video_scene_video_list, video_scene_text_list = merge_get_video_list()
st.write(tr("Video normalize..."))
video_service = VideoMergeService(video_scene_video_list)
print("normalize video")
video_scene_video_list = video_service.normalize_video()
st.write(tr("Generate Video subtitles..."))
merge_generate_subtitle(video_scene_video_list, video_scene_text_list)
st.write(tr("Generate Video..."))
video_file = video_service.generate_video_with_bg_music()
print("final file:", video_file)
st.session_state["result_video_file"] = video_file
status.update(label=tr("Generate Video completed!"), state="complete", expanded=False)