Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -30,10 +30,17 @@ async def text_to_speech(text, voice, rate, pitch):
|
|
| 30 |
return tmp_path, None
|
| 31 |
|
| 32 |
# 文字转视频功能
|
| 33 |
-
def text_to_video(text, voice, rate, pitch, video_width, video_height,
|
| 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是行间距
|
|
@@ -70,7 +77,6 @@ def text_to_video(text, voice, rate, pitch, video_width, video_height, bg_color,
|
|
| 70 |
audio_clip = AudioFileClip(audio)
|
| 71 |
audio_clips.append(audio_clip)
|
| 72 |
|
| 73 |
-
# 使用 wand 生成视频片段
|
| 74 |
# 使用 wand 生成视频片段
|
| 75 |
with Drawing() as draw:
|
| 76 |
draw.font = font_path
|
|
@@ -79,17 +85,24 @@ def text_to_video(text, voice, rate, pitch, video_width, video_height, bg_color,
|
|
| 79 |
draw.text_alignment = 'center'
|
| 80 |
draw.text_interline_spacing = 10
|
| 81 |
|
| 82 |
-
with Image(width=video_width, height=video_height, background=Color(
|
| 83 |
lines = page.split("\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
for j, line in enumerate(lines):
|
| 85 |
-
draw.text(int(video_width / 2),
|
| 86 |
|
| 87 |
draw(img) # Apply the drawing to the image
|
| 88 |
img.format = 'png'
|
| 89 |
img_path = os.path.join(tempfile.gettempdir(), f"page_{i}.png")
|
| 90 |
img.save(filename=img_path)
|
|
|
|
| 91 |
text_clip = ImageClip(img_path).set_duration(audio_clip.duration).set_audio(audio_clip)
|
| 92 |
-
|
|
|
|
|
|
|
| 93 |
|
| 94 |
# 合并所有视频片段
|
| 95 |
final_video = concatenate_videoclips(video_clips)
|
|
@@ -98,8 +111,8 @@ def text_to_video(text, voice, rate, pitch, video_width, video_height, bg_color,
|
|
| 98 |
return final_video_path, None
|
| 99 |
|
| 100 |
# Gradio接口函数
|
| 101 |
-
def tts_interface(text, voice, rate, pitch, video_width, video_height,
|
| 102 |
-
video, warning = text_to_video(text, voice, rate, pitch, video_width, video_height,
|
| 103 |
return None, video, warning
|
| 104 |
|
| 105 |
# 创建Gradio应用
|
|
@@ -115,7 +128,7 @@ async def create_demo():
|
|
| 115 |
gr.Slider(minimum=-20, maximum=20, value=0, label="Pitch Adjustment (Hz)", step=1),
|
| 116 |
gr.Slider(minimum=640, maximum=1920, value=1080, label="Video Width", step=10),
|
| 117 |
gr.Slider(minimum=480, maximum=1080, value=720, label="Video Height", step=10),
|
| 118 |
-
gr.
|
| 119 |
gr.ColorPicker(value="#FFFFFF", label="Text Color"),
|
| 120 |
gr.Textbox(label="Text Font", value="msyh.ttf"), # 请确保字体文件路径正确
|
| 121 |
gr.Slider(minimum=10, maximum=100, value=24, label="Text Size", step=1)
|
|
|
|
| 30 |
return tmp_path, None
|
| 31 |
|
| 32 |
# 文字转视频功能
|
| 33 |
+
def text_to_video(text, voice, rate, pitch, video_width, video_height, bg_image, text_color, text_font, text_size):
|
| 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是行间距
|
|
|
|
| 77 |
audio_clip = AudioFileClip(audio)
|
| 78 |
audio_clips.append(audio_clip)
|
| 79 |
|
|
|
|
| 80 |
# 使用 wand 生成视频片段
|
| 81 |
with Drawing() as draw:
|
| 82 |
draw.font = font_path
|
|
|
|
| 85 |
draw.text_alignment = 'center'
|
| 86 |
draw.text_interline_spacing = 10
|
| 87 |
|
| 88 |
+
with Image(width=video_width, height=video_height, background=Color('transparent')) as img:
|
| 89 |
lines = page.split("\n")
|
| 90 |
+
# Centering text vertically
|
| 91 |
+
total_text_height = len(lines) * (text_size + 10) # Height of text area
|
| 92 |
+
start_y = (video_height - total_text_height) // 2 # Start position to center vertically
|
| 93 |
+
|
| 94 |
for j, line in enumerate(lines):
|
| 95 |
+
draw.text(int(video_width / 2), start_y + (j * (text_size + 10)), line)
|
| 96 |
|
| 97 |
draw(img) # Apply the drawing to the 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 |
+
text_clip = text_clip.set_position('center').set_duration(audio_clip.duration)
|
| 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)
|
|
|
|
| 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 |
+
video, warning = text_to_video(text, voice, rate, pitch, video_width, video_height, bg_image, text_color, text_font, text_size)
|
| 116 |
return None, video, warning
|
| 117 |
|
| 118 |
# 创建Gradio应用
|
|
|
|
| 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", optional=True),
|
| 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)
|