Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -34,13 +34,6 @@ def text_to_video(text, voice, rate, pitch, video_width, video_height, bg_image,
|
|
34 |
# 字体文件路径
|
35 |
font_path = os.path.abspath(text_font)
|
36 |
|
37 |
-
# 读取背景图像
|
38 |
-
if bg_image is not None:
|
39 |
-
bg_image_path = bg_image.name
|
40 |
-
bg_clip = ImageClip(bg_image_path).set_duration(0) # Create a static clip
|
41 |
-
else:
|
42 |
-
bg_clip = ColorClip((video_width, video_height), color=(255, 255, 255)) # Default white background
|
43 |
-
|
44 |
# 计算每页可以容纳的行数和每行可以容纳的字符数
|
45 |
max_chars_per_line = video_width // (text_size // 2) # 字体宽度假设为字体大小的一半
|
46 |
max_lines_per_page = video_height // (text_size + 15) # 10是行间距
|
@@ -68,6 +61,7 @@ def text_to_video(text, voice, rate, pitch, video_width, video_height, bg_image,
|
|
68 |
# 为每页生成独立音频
|
69 |
audio_clips = []
|
70 |
video_clips = []
|
|
|
71 |
for i, page in enumerate(pages):
|
72 |
# 将每页的文本连贯朗读生成一个音频文件
|
73 |
audio_text = page.replace("\n", " ") # 移除换行符以防止 TTS 停顿
|
@@ -98,21 +92,29 @@ def text_to_video(text, voice, rate, pitch, video_width, video_height, bg_image,
|
|
98 |
img.format = 'png'
|
99 |
img_path = os.path.join(tempfile.gettempdir(), f"page_{i}.png")
|
100 |
img.save(filename=img_path)
|
101 |
-
|
102 |
text_clip = ImageClip(img_path).set_duration(audio_clip.duration).set_audio(audio_clip)
|
103 |
-
|
104 |
-
final_clip = CompositeVideoClip([bg_clip, text_clip]) # Overlay text on background
|
105 |
-
video_clips.append(final_clip)
|
106 |
|
107 |
# 合并所有视频片段
|
108 |
final_video = concatenate_videoclips(video_clips)
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
final_video_path = os.path.join(tempfile.gettempdir(), "output_video.mp4")
|
110 |
final_video.write_videofile(final_video_path, fps=24, codec="libx264")
|
111 |
return final_video_path, None
|
112 |
|
113 |
# Gradio接口函数
|
114 |
def tts_interface(text, voice, rate, pitch, video_width, video_height, bg_image, text_color, text_font, text_size):
|
115 |
-
|
|
|
|
|
|
|
|
|
|
|
116 |
return None, video, warning
|
117 |
|
118 |
# 创建Gradio应用
|
@@ -128,7 +130,7 @@ async def create_demo():
|
|
128 |
gr.Slider(minimum=-20, maximum=20, value=0, label="Pitch Adjustment (Hz)", step=1),
|
129 |
gr.Slider(minimum=640, maximum=1920, value=1080, label="Video Width", step=10),
|
130 |
gr.Slider(minimum=480, maximum=1080, value=720, label="Video Height", step=10),
|
131 |
-
gr.File(label="Upload Background Image or Video", type="file"
|
132 |
gr.ColorPicker(value="#FFFFFF", label="Text Color"),
|
133 |
gr.Textbox(label="Text Font", value="msyh.ttf"), # 请确保字体文件路径正确
|
134 |
gr.Slider(minimum=10, maximum=100, value=24, label="Text Size", step=1)
|
|
|
34 |
# 字体文件路径
|
35 |
font_path = os.path.abspath(text_font)
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
# 计算每页可以容纳的行数和每行可以容纳的字符数
|
38 |
max_chars_per_line = video_width // (text_size // 2) # 字体宽度假设为字体大小的一半
|
39 |
max_lines_per_page = video_height // (text_size + 15) # 10是行间距
|
|
|
61 |
# 为每页生成独立音频
|
62 |
audio_clips = []
|
63 |
video_clips = []
|
64 |
+
|
65 |
for i, page in enumerate(pages):
|
66 |
# 将每页的文本连贯朗读生成一个音频文件
|
67 |
audio_text = page.replace("\n", " ") # 移除换行符以防止 TTS 停顿
|
|
|
92 |
img.format = 'png'
|
93 |
img_path = os.path.join(tempfile.gettempdir(), f"page_{i}.png")
|
94 |
img.save(filename=img_path)
|
|
|
95 |
text_clip = ImageClip(img_path).set_duration(audio_clip.duration).set_audio(audio_clip)
|
96 |
+
video_clips.append(text_clip)
|
|
|
|
|
97 |
|
98 |
# 合并所有视频片段
|
99 |
final_video = concatenate_videoclips(video_clips)
|
100 |
+
|
101 |
+
# If a background image is provided, create a CompositeVideoClip
|
102 |
+
if bg_image:
|
103 |
+
bg_clip = ImageClip(bg_image).set_duration(final_video.duration).resize(newsize=(video_width, video_height))
|
104 |
+
final_video = CompositeVideoClip([bg_clip, final_video.set_position("center")])
|
105 |
+
|
106 |
final_video_path = os.path.join(tempfile.gettempdir(), "output_video.mp4")
|
107 |
final_video.write_videofile(final_video_path, fps=24, codec="libx264")
|
108 |
return final_video_path, None
|
109 |
|
110 |
# Gradio接口函数
|
111 |
def tts_interface(text, voice, rate, pitch, video_width, video_height, bg_image, text_color, text_font, text_size):
|
112 |
+
# Check if a background image is uploaded
|
113 |
+
if bg_image is not None:
|
114 |
+
video, warning = text_to_video(text, voice, rate, pitch, video_width, video_height, bg_image.name, text_color, text_font, text_size)
|
115 |
+
else:
|
116 |
+
# Pass None for the bg_image if not uploaded
|
117 |
+
video, warning = text_to_video(text, voice, rate, pitch, video_width, video_height, None, text_color, text_font, text_size)
|
118 |
return None, video, warning
|
119 |
|
120 |
# 创建Gradio应用
|
|
|
130 |
gr.Slider(minimum=-20, maximum=20, value=0, label="Pitch Adjustment (Hz)", step=1),
|
131 |
gr.Slider(minimum=640, maximum=1920, value=1080, label="Video Width", step=10),
|
132 |
gr.Slider(minimum=480, maximum=1080, value=720, label="Video Height", step=10),
|
133 |
+
gr.File(label="Upload Background Image or Video", type="file"),
|
134 |
gr.ColorPicker(value="#FFFFFF", label="Text Color"),
|
135 |
gr.Textbox(label="Text Font", value="msyh.ttf"), # 请确保字体文件路径正确
|
136 |
gr.Slider(minimum=10, maximum=100, value=24, label="Text Size", step=1)
|