hivecorp commited on
Commit
4da00bb
·
verified ·
1 Parent(s): 536587c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -2
app.py CHANGED
@@ -1,3 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  def text_to_video(text, voice, rate, pitch, video_width, video_height, bg_color, text_color, text_font, text_size):
2
  # 字体文件路径
3
  font_path = os.path.abspath(text_font)
@@ -49,7 +81,6 @@ def text_to_video(text, voice, rate, pitch, video_width, video_height, bg_color,
49
  lines = page.split("\n")
50
  for j, line in enumerate(lines):
51
  draw.text(int(video_width / 2), (j + 1) * (text_size + 10), line)
52
- # Apply the drawing to the image
53
  draw(img) # Correct usage to apply the drawing
54
  img.format = 'png'
55
  img_path = os.path.join(tempfile.gettempdir(), f"page_{i}.png")
@@ -63,6 +94,43 @@ def text_to_video(text, voice, rate, pitch, video_width, video_height, bg_color,
63
  final_video.write_videofile(final_video_path, fps=24, codec="libx264")
64
  return final_video_path, None
65
 
 
 
 
 
66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
- demo.launch(share=True)
 
 
 
 
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):
34
  # 字体文件路径
35
  font_path = os.path.abspath(text_font)
 
81
  lines = page.split("\n")
82
  for j, line in enumerate(lines):
83
  draw.text(int(video_width / 2), (j + 1) * (text_size + 10), line)
 
84
  draw(img) # Correct usage to apply the drawing
85
  img.format = 'png'
86
  img_path = os.path.join(tempfile.gettempdir(), f"page_{i}.png")
 
94
  final_video.write_videofile(final_video_path, fps=24, codec="libx264")
95
  return final_video_path, None
96
 
97
+ # Gradio接口函数
98
+ def tts_interface(text, voice, rate, pitch, video_width, video_height, bg_color, text_color, text_font, text_size):
99
+ video, warning = text_to_video(text, voice, rate, pitch, video_width, video_height, bg_color, text_color, text_font, text_size)
100
+ return None, video, warning
101
 
102
+ # 创建Gradio应用
103
+ async def create_demo():
104
+ voices = await get_voices()
105
+
106
+ demo = gr.Interface(
107
+ fn=tts_interface,
108
+ inputs=[
109
+ gr.Textbox(label="Input Text", lines=5),
110
+ gr.Dropdown(choices=[""] + list(voices.keys()), label="Select Voice", value=""),
111
+ gr.Slider(minimum=-50, maximum=50, value=0, label="Rate Adjustment (%)", step=1),
112
+ gr.Slider(minimum=-20, maximum=20, value=0, label="Pitch Adjustment (Hz)", step=1),
113
+ gr.Slider(minimum=640, maximum=1920, value=1080, label="Video Width", step=10),
114
+ gr.Slider(minimum=480, maximum=1080, value=720, label="Video Height", step=10),
115
+ gr.ColorPicker(value="#000000", label="Background Color"),
116
+ gr.ColorPicker(value="#FFFFFF", label="Text Color"),
117
+ gr.Textbox(label="Text Font", value="msyh.ttf"), # 请确保字体文件路径正确
118
+ gr.Slider(minimum=10, maximum=100, value=24, label="Text Size", step=1)
119
+ ],
120
+ outputs=[
121
+ gr.Audio(label="Generated Audio", type="filepath"),
122
+ gr.Video(label="Generated Video"),
123
+ gr.Markdown(label="Warning", visible=False)
124
+ ],
125
+ title="Edge TTS Text to Speech and Video",
126
+ 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.",
127
+ analytics_enabled=False,
128
+ allow_flagging=False,
129
+ )
130
+
131
+ return demo
132
 
133
+ # 运行应用
134
+ if __name__ == "__main__":
135
+ demo = asyncio.run(create_demo())
136
+ demo.launch(share=True) # Set share=True to create a public link