Upload 5 files
Browse files- app.py +12 -9
- utils.py +1 -1
- video_processing.py +58 -24
app.py
CHANGED
@@ -75,9 +75,6 @@ def extract_key_contents(script, num_contents=10):
|
|
75 |
except Exception as e:
|
76 |
print(f"Lỗi khi trích xuất nội dung: {str(e)}")
|
77 |
return []
|
78 |
-
|
79 |
-
# Định nghĩa danh sách giọng đọc
|
80 |
-
VOICES = ["alloy", "echo", "fable", "onyx", "nova", "shimmer"]
|
81 |
|
82 |
# Giao diện Gradio
|
83 |
def interface():
|
@@ -143,18 +140,24 @@ def interface():
|
|
143 |
confirm_button.click(lambda x: asyncio.run(confirm_content(x)),
|
144 |
inputs=[content_output],
|
145 |
outputs=[download_docx, download_audio, status_message])
|
|
|
|
|
|
|
146 |
|
147 |
with gr.Tab("Tạo Âm thanh"):
|
148 |
text_input = gr.Textbox(label="Nhập văn bản để chuyển đổi")
|
149 |
-
voice_select = gr.Dropdown(label="Chọn giọng đọc",
|
150 |
-
choices=VOICES) # Dropdown cho voice_select
|
151 |
audio_button = gr.Button("Tạo Âm thanh")
|
152 |
audio_output = gr.Audio(label="Âm thanh tạo ra")
|
153 |
download_audio = gr.File(label="Tải xuống file âm thanh", interactive=False)
|
154 |
|
155 |
def text_to_speech_func(text, voice):
|
156 |
-
|
157 |
-
|
|
|
|
|
|
|
|
|
158 |
|
159 |
audio_button.click(text_to_speech_func,
|
160 |
inputs=[text_input, voice_select],
|
@@ -175,6 +178,7 @@ def interface():
|
|
175 |
""" Tạo video từ các thông tin đầu vào. """
|
176 |
try:
|
177 |
status_message.update("Đang xử lý...") # Cập nhật trạng thái
|
|
|
178 |
# 1. Tính toán thời lượng video
|
179 |
audio_clip = AudioFileClip(audio_file)
|
180 |
video_duration = audio_clip.duration
|
@@ -207,8 +211,7 @@ def interface():
|
|
207 |
except Exception as e:
|
208 |
status_message.update(f"Lỗi khi tạo video: {e}") # Cập nhật trạng thái
|
209 |
return None
|
210 |
-
|
211 |
-
return app
|
212 |
|
213 |
# Khởi chạy ứng dụng
|
214 |
if __name__ == "__main__":
|
|
|
75 |
except Exception as e:
|
76 |
print(f"Lỗi khi trích xuất nội dung: {str(e)}")
|
77 |
return []
|
|
|
|
|
|
|
78 |
|
79 |
# Giao diện Gradio
|
80 |
def interface():
|
|
|
140 |
confirm_button.click(lambda x: asyncio.run(confirm_content(x)),
|
141 |
inputs=[content_output],
|
142 |
outputs=[download_docx, download_audio, status_message])
|
143 |
+
|
144 |
+
# Định nghĩa danh sách giọng đọc
|
145 |
+
VOICES = ["alloy", "echo", "fable", "onyx", "nova", "shimmer"]
|
146 |
|
147 |
with gr.Tab("Tạo Âm thanh"):
|
148 |
text_input = gr.Textbox(label="Nhập văn bản để chuyển đổi")
|
149 |
+
voice_select = gr.Dropdown(label="Chọn giọng đọc", choices=VOICES) # Dropdown cho voice_select
|
|
|
150 |
audio_button = gr.Button("Tạo Âm thanh")
|
151 |
audio_output = gr.Audio(label="Âm thanh tạo ra")
|
152 |
download_audio = gr.File(label="Tải xuống file âm thanh", interactive=False)
|
153 |
|
154 |
def text_to_speech_func(text, voice):
|
155 |
+
try:
|
156 |
+
audio_path = text_to_speech(text, voice, "Tiếng Việt")
|
157 |
+
return audio_path, audio_path
|
158 |
+
except Exception as e:
|
159 |
+
print(f"Lỗi khi chuyển đổi văn bản thành giọng nói: {e}")
|
160 |
+
return None, None
|
161 |
|
162 |
audio_button.click(text_to_speech_func,
|
163 |
inputs=[text_input, voice_select],
|
|
|
178 |
""" Tạo video từ các thông tin đầu vào. """
|
179 |
try:
|
180 |
status_message.update("Đang xử lý...") # Cập nhật trạng thái
|
181 |
+
|
182 |
# 1. Tính toán thời lượng video
|
183 |
audio_clip = AudioFileClip(audio_file)
|
184 |
video_duration = audio_clip.duration
|
|
|
211 |
except Exception as e:
|
212 |
status_message.update(f"Lỗi khi tạo video: {e}") # Cập nhật trạng thái
|
213 |
return None
|
214 |
+
return app
|
|
|
215 |
|
216 |
# Khởi chạy ứng dụng
|
217 |
if __name__ == "__main__":
|
utils.py
CHANGED
@@ -10,7 +10,7 @@ def check_file_exists(file_path):
|
|
10 |
raise FileNotFoundError(f"Tệp {file_path} không tồn tại.")
|
11 |
|
12 |
# Đường dẫn đầy đủ tới các file
|
13 |
-
bgm_file = "data/bg_music
|
14 |
output_path = "output/final_video.mp4"
|
15 |
|
16 |
check_file_exists(bgm_file)
|
|
|
10 |
raise FileNotFoundError(f"Tệp {file_path} không tồn tại.")
|
11 |
|
12 |
# Đường dẫn đầy đủ tới các file
|
13 |
+
bgm_file = "data/bg_music.mp3"
|
14 |
output_path = "output/final_video.mp4"
|
15 |
|
16 |
check_file_exists(bgm_file)
|
video_processing.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import os
|
2 |
import random
|
|
|
3 |
import shutil
|
4 |
import tempfile
|
5 |
from concurrent.futures import ThreadPoolExecutor
|
@@ -82,6 +83,35 @@ def process_images_parallel(image_patch, clip_duration):
|
|
82 |
|
83 |
return clips
|
84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
def process_script_for_video(script, dataset_path, use_dataset):
|
86 |
"""
|
87 |
Xử lý script để tạo video.
|
@@ -93,30 +123,34 @@ def create_video_func(script, audio_path, dataset_path, use_dataset):
|
|
93 |
"""
|
94 |
Hàm chính để tạo video.
|
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 |
def split_audio(audio_path, num_segments, output_dir):
|
122 |
"""
|
|
|
1 |
import os
|
2 |
import random
|
3 |
+
import glob
|
4 |
import shutil
|
5 |
import tempfile
|
6 |
from concurrent.futures import ThreadPoolExecutor
|
|
|
83 |
|
84 |
return clips
|
85 |
|
86 |
+
# Định nghĩa hàm extract_key_contents
|
87 |
+
def extract_key_contents(script: str) -> list[str]:
|
88 |
+
"""
|
89 |
+
Hàm này dùng để trích xuất các ý chính từ một đoạn script.
|
90 |
+
|
91 |
+
Tham số:
|
92 |
+
- script (str): Đoạn văn bản cần xử lý để trích xuất các ý chính.
|
93 |
+
|
94 |
+
Trả về:
|
95 |
+
- list[str]: Danh sách các câu được tách ra từ đoạn script.
|
96 |
+
|
97 |
+
Logic xử lý:
|
98 |
+
- Đầu tiên, đoạn script được tách thành các câu dựa trên dấu chấm ('.').
|
99 |
+
- Mỗi câu được xem như một ý chính và được thêm vào danh sách kết quả.
|
100 |
+
"""
|
101 |
+
# Kiểm tra nếu script là chuỗi rỗng
|
102 |
+
if not script:
|
103 |
+
return []
|
104 |
+
|
105 |
+
# Tách đoạn script thành các câu dựa trên dấu chấm
|
106 |
+
sentences = script.split('.')
|
107 |
+
|
108 |
+
# Loại bỏ các khoảng trắng thừa và các câu rỗng
|
109 |
+
sentences = [sentence.strip() for sentence in sentences if sentence.strip()]
|
110 |
+
|
111 |
+
# Trả về danh sách các câu
|
112 |
+
return sentences
|
113 |
+
|
114 |
+
|
115 |
def process_script_for_video(script, dataset_path, use_dataset):
|
116 |
"""
|
117 |
Xử lý script để tạo video.
|
|
|
123 |
"""
|
124 |
Hàm chính để tạo video.
|
125 |
"""
|
126 |
+
try:
|
127 |
+
sentences = process_script_for_video(script, dataset_path, use_dataset)
|
128 |
+
|
129 |
+
# Tạo thư mục tạm thời để lưu các file âm thanh tách biệt
|
130 |
+
temp_dir = tempfile.mkdtemp()
|
131 |
+
|
132 |
+
# Tách file âm thanh thành các đoạn nhỏ
|
133 |
+
audio_clips = split_audio(audio_path, len(sentences), temp_dir)
|
134 |
+
|
135 |
+
# Lấy đường dẫn của các video từ dataset
|
136 |
+
video_files = glob.glob(os.path.join(dataset_path, "*.mp4")) if use_dataset else []
|
137 |
+
|
138 |
+
# Đảm bảo số lượng câu, âm thanh và video là bằng nhau
|
139 |
+
min_length = min(len(sentences), len(audio_clips), len(video_files))
|
140 |
+
sentences = sentences[:min_length]
|
141 |
+
audio_clips = audio_clips[:min_length]
|
142 |
+
video_files = video_files[:min_length]
|
143 |
+
|
144 |
+
output_path = "output_video.mp4"
|
145 |
+
create_video(sentences, audio_clips, video_files, output_path)
|
146 |
+
|
147 |
+
return output_path
|
148 |
+
except Exception as e:
|
149 |
+
print(f"Lỗi khi tạo video: {e}")
|
150 |
+
return None
|
151 |
+
finally:
|
152 |
+
# Xóa thư mục tạm thời
|
153 |
+
shutil.rmtree(temp_dir)
|
154 |
|
155 |
def split_audio(audio_path, num_segments, output_dir):
|
156 |
"""
|