Update app.py
Browse files
app.py
CHANGED
@@ -55,21 +55,15 @@ async def convert_text_to_speech(text, language='vi', output_file="output.wav"):
|
|
55 |
except Exception as e:
|
56 |
raise Exception(f"Lỗi khi chuyển đổi văn bản thành giọng nói: {str(e)}")
|
57 |
|
58 |
-
def create_video(audio_file,
|
59 |
"""
|
60 |
-
Tạo video với
|
61 |
"""
|
62 |
-
# Chọn file video nền
|
63 |
-
|
64 |
-
"Ms. Leah": "mc1_background.mp4",
|
65 |
-
"Ms. Jenny": "mc2_background.mp4",
|
66 |
-
"Ms. Soo-jin": "mc3_background.mp4",
|
67 |
-
"Ms. Daisy": "mc4_background.mp4"
|
68 |
-
}
|
69 |
-
background_video = background_videos.get(mc_choice, "mc1_background.mp4") # Mặc định là MC1 nếu không tìm thấy
|
70 |
|
71 |
# Định nghĩa văn bản trên video
|
72 |
-
text_on_video =
|
73 |
|
74 |
# Định nghĩa màu sắc chữ
|
75 |
color_map = {
|
@@ -85,7 +79,7 @@ def create_video(audio_file, mc_choice, text_color, output_video="output.mp4"):
|
|
85 |
command = [
|
86 |
"ffmpeg",
|
87 |
"-stream_loop", "-1", # Lặp lại video nền vô hạn
|
88 |
-
"-i", background_video, # Video nền
|
89 |
"-i", audio_file, # File audio đầu vào
|
90 |
"-vf", f"drawtext=text='{text_on_video}':fontcolor={font_color}:fontsize=50:fontfile=/LHanoienne.otf:x=10:y=10:box=1:[email protected]:boxborderw=5", # Văn bản trên video
|
91 |
"-t", "45.7", # Thời lượng video
|
@@ -117,7 +111,7 @@ def interface():
|
|
117 |
use_generated_text = gr.Checkbox(label="Sử dụng văn bản được tạo bởi LLM", value=True)
|
118 |
manual_text_input = gr.Textbox(label="Nhập văn bản thủ công", visible=False)
|
119 |
language = gr.Dropdown(label="Chọn ngôn ngữ", choices=["vi", "en"], value="vi")
|
120 |
-
|
121 |
text_color = gr.Dropdown(label="Chọn màu chữ", choices=["Trắng", "Đỏ", "Xanh dương", "Xanh lá", "Vàng"], value="Trắng")
|
122 |
confirm_button = gr.Button("Xác nhận nội dung")
|
123 |
download_docx = gr.File(label="Tải xuống file DOCX", interactive=False)
|
@@ -154,7 +148,7 @@ def interface():
|
|
154 |
status = f"Đã xảy ra lỗi: {str(e)}"
|
155 |
return "", None, True, None, status
|
156 |
|
157 |
-
async def confirm_content(content_output, use_generated_text, manual_text_input, language,
|
158 |
try:
|
159 |
if use_generated_text:
|
160 |
text_to_convert = content_output
|
@@ -170,7 +164,7 @@ def interface():
|
|
170 |
audio_path = await convert_text_to_speech(text_to_convert, language)
|
171 |
status_message.value = "Đã chuyển đổi văn bản thành giọng nói thành công!"
|
172 |
status_message.value = "Đang tạo video..."
|
173 |
-
video_path = create_video(audio_path,
|
174 |
status_message.value = "Đã tạo video thành công!"
|
175 |
return docx_path, audio_path, video_path, status_message.value
|
176 |
except Exception as e:
|
@@ -187,7 +181,7 @@ def interface():
|
|
187 |
|
188 |
# Liên kết sự kiện click của nút Xác nhận nội dung
|
189 |
confirm_button.click(confirm_content,
|
190 |
-
inputs=[content_output, use_generated_text, manual_text_input, language,
|
191 |
outputs=[download_docx, download_audio, download_video, status_message])
|
192 |
|
193 |
return app
|
|
|
55 |
except Exception as e:
|
56 |
raise Exception(f"Lỗi khi chuyển đổi văn bản thành giọng nói: {str(e)}")
|
57 |
|
58 |
+
def create_video(audio_file, custom_text, text_color, output_video="output.mp4"):
|
59 |
"""
|
60 |
+
Tạo video với văn bản tùy chỉnh.
|
61 |
"""
|
62 |
+
# Chọn file video nền mặc định
|
63 |
+
background_video = "default_background.mp4" # Đặt tên file video nền mặc định
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
# Định nghĩa văn bản trên video
|
66 |
+
text_on_video = custom_text if custom_text else ""
|
67 |
|
68 |
# Định nghĩa màu sắc chữ
|
69 |
color_map = {
|
|
|
79 |
command = [
|
80 |
"ffmpeg",
|
81 |
"-stream_loop", "-1", # Lặp lại video nền vô hạn
|
82 |
+
"-i", background_video, # Video nền mặc định
|
83 |
"-i", audio_file, # File audio đầu vào
|
84 |
"-vf", f"drawtext=text='{text_on_video}':fontcolor={font_color}:fontsize=50:fontfile=/LHanoienne.otf:x=10:y=10:box=1:[email protected]:boxborderw=5", # Văn bản trên video
|
85 |
"-t", "45.7", # Thời lượng video
|
|
|
111 |
use_generated_text = gr.Checkbox(label="Sử dụng văn bản được tạo bởi LLM", value=True)
|
112 |
manual_text_input = gr.Textbox(label="Nhập văn bản thủ công", visible=False)
|
113 |
language = gr.Dropdown(label="Chọn ngôn ngữ", choices=["vi", "en"], value="vi")
|
114 |
+
custom_text_input = gr.Textbox(label="Nhập văn bản tùy chỉnh (trống nếu không muốn hiển thị)", value="")
|
115 |
text_color = gr.Dropdown(label="Chọn màu chữ", choices=["Trắng", "Đỏ", "Xanh dương", "Xanh lá", "Vàng"], value="Trắng")
|
116 |
confirm_button = gr.Button("Xác nhận nội dung")
|
117 |
download_docx = gr.File(label="Tải xuống file DOCX", interactive=False)
|
|
|
148 |
status = f"Đã xảy ra lỗi: {str(e)}"
|
149 |
return "", None, True, None, status
|
150 |
|
151 |
+
async def confirm_content(content_output, use_generated_text, manual_text_input, language, custom_text_input, text_color):
|
152 |
try:
|
153 |
if use_generated_text:
|
154 |
text_to_convert = content_output
|
|
|
164 |
audio_path = await convert_text_to_speech(text_to_convert, language)
|
165 |
status_message.value = "Đã chuyển đổi văn bản thành giọng nói thành công!"
|
166 |
status_message.value = "Đang tạo video..."
|
167 |
+
video_path = create_video(audio_path, custom_text_input, text_color)
|
168 |
status_message.value = "Đã tạo video thành công!"
|
169 |
return docx_path, audio_path, video_path, status_message.value
|
170 |
except Exception as e:
|
|
|
181 |
|
182 |
# Liên kết sự kiện click của nút Xác nhận nội dung
|
183 |
confirm_button.click(confirm_content,
|
184 |
+
inputs=[content_output, use_generated_text, manual_text_input, language, custom_text_input, text_color],
|
185 |
outputs=[download_docx, download_audio, download_video, status_message])
|
186 |
|
187 |
return app
|