Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,33 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
import asyncio
|
4 |
-
|
|
|
|
|
5 |
from wand.image import Image
|
6 |
-
from wand.color import Color
|
7 |
from wand.drawing import Drawing
|
8 |
-
from
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
#
|
11 |
async def text_to_speech(text, voice, rate, pitch):
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
# 文字转视频功能
|
19 |
def text_to_video(text, voice, rate, pitch, video_width, video_height, bg_color, text_color, text_font, text_size):
|
@@ -57,52 +71,72 @@ def text_to_video(text, voice, rate, pitch, video_width, video_height, bg_color,
|
|
57 |
audio_clips.append(audio_clip)
|
58 |
|
59 |
# 使用 wand 生成视频片段
|
60 |
-
with
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
# Calculate the y position for each line of text
|
69 |
lines = page.split("\n")
|
70 |
-
|
|
|
|
|
71 |
|
72 |
-
for line in lines:
|
73 |
-
draw.text(int(video_width / 2),
|
74 |
-
y_position += int(text_size + 10) # Move down for the next line (ensure y_position is an integer)
|
75 |
|
76 |
-
draw(img) #
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
-
img.format = 'png'
|
79 |
-
img_path = os.path.join(tempfile.gettempdir(), f"page_{i}.png")
|
80 |
-
img.save(filename=img_path)
|
81 |
-
text_clip = ImageClip(img_path).set_duration(audio_clip.duration).set_audio(audio_clip)
|
82 |
-
video_clips.append(text_clip)
|
83 |
-
|
84 |
# 合并所有视频片段
|
85 |
final_video = concatenate_videoclips(video_clips)
|
86 |
final_video_path = os.path.join(tempfile.gettempdir(), "output_video.mp4")
|
87 |
final_video.write_videofile(final_video_path, fps=24, codec="libx264")
|
88 |
return final_video_path, None
|
89 |
|
90 |
-
#
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
voice = "zh"
|
95 |
-
rate = 1.0
|
96 |
-
pitch = 1.0
|
97 |
-
video_width = 640
|
98 |
-
video_height = 480
|
99 |
-
bg_color = "white"
|
100 |
-
text_color = "black"
|
101 |
-
text_font = "path/to/your/font.ttf" # 更新为实际字体路径
|
102 |
-
text_size = 30
|
103 |
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import edge_tts
|
3 |
import asyncio
|
4 |
+
import tempfile
|
5 |
+
import os
|
6 |
+
from moviepy.editor import TextClip, concatenate_videoclips, CompositeVideoClip, AudioFileClip, ImageClip
|
7 |
from wand.image import Image
|
|
|
8 |
from wand.drawing import Drawing
|
9 |
+
from wand.color import Color
|
10 |
+
|
11 |
+
# 获取所有可用的语音
|
12 |
+
async def get_voices():
|
13 |
+
voices = await edge_tts.list_voices()
|
14 |
+
return {f"{v['ShortName']} - {v['Locale']} ({v['Gender']})": v['ShortName'] for v in voices}
|
15 |
|
16 |
+
# 文字转语音功能
|
17 |
async def text_to_speech(text, voice, rate, pitch):
|
18 |
+
if not text.strip():
|
19 |
+
return None, gr.Warning("Please enter the text to convert.")
|
20 |
+
if not voice:
|
21 |
+
return None, gr.Warning("Please select a voice.")
|
22 |
+
|
23 |
+
voice_short_name = voice.split(" - ")[0]
|
24 |
+
rate_str = f"{rate:+d}%"
|
25 |
+
pitch_str = f"{pitch:+d}Hz"
|
26 |
+
communicate = edge_tts.Communicate(text, voice_short_name, rate=rate_str, pitch=pitch_str)
|
27 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
28 |
+
tmp_path = tmp_file.name
|
29 |
+
await communicate.save(tmp_path)
|
30 |
+
return tmp_path, None
|
31 |
|
32 |
# 文字转视频功能
|
33 |
def text_to_video(text, voice, rate, pitch, video_width, video_height, bg_color, text_color, text_font, text_size):
|
|
|
71 |
audio_clips.append(audio_clip)
|
72 |
|
73 |
# 使用 wand 生成视频片段
|
74 |
+
with Drawing() as draw:
|
75 |
+
draw.font = font_path
|
76 |
+
draw.font_size = text_size
|
77 |
+
draw.fill_color = Color(text_color)
|
78 |
+
draw.text_alignment = 'center'
|
79 |
+
draw.text_interline_spacing = 10
|
80 |
+
|
81 |
+
with Image(width=video_width, height=video_height, background=Color(bg_color)) as img:
|
|
|
82 |
lines = page.split("\n")
|
83 |
+
# Centering text vertically
|
84 |
+
total_text_height = len(lines) * (text_size + 10) # Height of text area
|
85 |
+
start_y = (video_height - total_text_height) // 2 # Start position to center vertically
|
86 |
|
87 |
+
for j, line in enumerate(lines):
|
88 |
+
draw.text(int(video_width / 2), start_y + (j * (text_size + 10)), line)
|
|
|
89 |
|
90 |
+
draw(img) # Apply the drawing to the image
|
91 |
+
img.format = 'png'
|
92 |
+
img_path = os.path.join(tempfile.gettempdir(), f"page_{i}.png")
|
93 |
+
img.save(filename=img_path)
|
94 |
+
text_clip = ImageClip(img_path).set_duration(audio_clip.duration).set_audio(audio_clip)
|
95 |
+
video_clips.append(text_clip)
|
96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
# 合并所有视频片段
|
98 |
final_video = concatenate_videoclips(video_clips)
|
99 |
final_video_path = os.path.join(tempfile.gettempdir(), "output_video.mp4")
|
100 |
final_video.write_videofile(final_video_path, fps=24, codec="libx264")
|
101 |
return final_video_path, None
|
102 |
|
103 |
+
# Gradio接口函数
|
104 |
+
def tts_interface(text, voice, rate, pitch, video_width, video_height, bg_color, text_color, text_font, text_size):
|
105 |
+
video, warning = text_to_video(text, voice, rate, pitch, video_width, video_height, bg_color, text_color, text_font, text_size)
|
106 |
+
return None, video, warning
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
+
# 创建Gradio应用
|
109 |
+
async def create_demo():
|
110 |
+
voices = await get_voices()
|
111 |
+
|
112 |
+
demo = gr.Interface(
|
113 |
+
fn=tts_interface,
|
114 |
+
inputs=[
|
115 |
+
gr.Textbox(label="Input Text", lines=5),
|
116 |
+
gr.Dropdown(choices=[""] + list(voices.keys()), label="Select Voice", value=""),
|
117 |
+
gr.Slider(minimum=-50, maximum=50, value=0, label="Rate Adjustment (%)", step=1),
|
118 |
+
gr.Slider(minimum=-20, maximum=20, value=0, label="Pitch Adjustment (Hz)", step=1),
|
119 |
+
gr.Slider(minimum=640, maximum=1920, value=1080, label="Video Width", step=10),
|
120 |
+
gr.Slider(minimum=480, maximum=1080, value=720, label="Video Height", step=10),
|
121 |
+
gr.ColorPicker(value="#000000", label="Background Color"),
|
122 |
+
gr.ColorPicker(value="#FFFFFF", label="Text Color"),
|
123 |
+
gr.Textbox(label="Text Font", value="msyh.ttf"), # 请确保字体文件路径正确
|
124 |
+
gr.Slider(minimum=10, maximum=100, value=24, label="Text Size", step=1)
|
125 |
+
],
|
126 |
+
outputs=[
|
127 |
+
gr.Audio(label="Generated Audio", type="filepath"),
|
128 |
+
gr.Video(label="Generated Video"),
|
129 |
+
gr.Markdown(label="Warning", visible=False)
|
130 |
+
],
|
131 |
+
title="Edge TTS Text to Speech and Video",
|
132 |
+
description="Convert text to speech and video using Microsoft Edge TTS. Adjust rate and pitch: 0 is the default value, positive values increase, and negative values decrease.",
|
133 |
+
analytics_enabled=False,
|
134 |
+
allow_flagging=False,
|
135 |
+
)
|
136 |
+
|
137 |
+
return demo
|
138 |
+
|
139 |
+
# 运行应用
|
140 |
+
if __name__ == "__main__":
|
141 |
+
demo = asyncio.run(create_demo())
|
142 |
+
demo.launch()
|