hivecorp commited on
Commit
b338e3d
·
verified ·
1 Parent(s): 95d33e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -74
app.py CHANGED
@@ -1,34 +1,3 @@
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
  # 字体文件路径
@@ -81,11 +50,11 @@ def text_to_video(text, voice, rate, pitch, video_width, video_height, bg_color,
81
 
82
  # Calculate the y position for each line of text
83
  lines = page.split("\n")
84
- y_position = (video_height - (len(lines) * (text_size + 10))) / 2 # Center vertically
85
 
86
  for line in lines:
87
  draw.text(int(video_width / 2), y_position, line)
88
- y_position += text_size + 10 # Move down for the next line
89
 
90
  draw(img) # Draw the text on the image
91
 
@@ -100,44 +69,3 @@ def text_to_video(text, voice, rate, pitch, video_width, video_height, bg_color,
100
  final_video_path = os.path.join(tempfile.gettempdir(), "output_video.mp4")
101
  final_video.write_videofile(final_video_path, fps=24, codec="libx264")
102
  return final_video_path, None
103
-
104
- # Gradio接口函数
105
- def tts_interface(text, voice, rate, pitch, video_width, video_height, bg_color, text_color, text_font, text_size):
106
- video, warning = text_to_video(text, voice, rate, pitch, video_width, video_height, bg_color, text_color, text_font, text_size)
107
- return None, video, warning
108
-
109
- # 创建Gradio应用
110
- async def create_demo():
111
- voices = await get_voices()
112
-
113
- demo = gr.Interface(
114
- fn=tts_interface,
115
- inputs=[
116
- gr.Textbox(label="Input Text", lines=5),
117
- gr.Dropdown(choices=[""] + list(voices.keys()), label="Select Voice", value=""),
118
- gr.Slider(minimum=-50, maximum=50, value=0, label="Rate Adjustment (%)", step=1),
119
- gr.Slider(minimum=-20, maximum=20, value=0, label="Pitch Adjustment (Hz)", step=1),
120
- gr.Slider(minimum=640, maximum=1920, value=1080, label="Video Width", step=10),
121
- gr.Slider(minimum=480, maximum=1080, value=720, label="Video Height", step=10),
122
- gr.ColorPicker(value="#000000", label="Background Color"),
123
- gr.ColorPicker(value="#FFFFFF", label="Text Color"),
124
- gr.Textbox(label="Text Font", value="msyh.ttf"), # 请确保字体文件路径正确
125
- gr.Slider(minimum=10, maximum=100, value=24, label="Text Size", step=1)
126
- ],
127
- outputs=[
128
- gr.Audio(label="Generated Audio", type="filepath"),
129
- gr.Video(label="Generated Video"),
130
- gr.Markdown(label="Warning", visible=False)
131
- ],
132
- title="Edge TTS Text to Speech and Video",
133
- 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.",
134
- analytics_enabled=False,
135
- allow_flagging=False,
136
- )
137
-
138
- return demo
139
-
140
- # 运行应用
141
- if __name__ == "__main__":
142
- demo = asyncio.run(create_demo())
143
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # 文字转视频功能
2
  def text_to_video(text, voice, rate, pitch, video_width, video_height, bg_color, text_color, text_font, text_size):
3
  # 字体文件路径
 
50
 
51
  # Calculate the y position for each line of text
52
  lines = page.split("\n")
53
+ y_position = int((video_height - (len(lines) * (text_size + 10))) / 2) # Center vertically
54
 
55
  for line in lines:
56
  draw.text(int(video_width / 2), y_position, line)
57
+ y_position += int(text_size + 10) # Move down for the next line (ensure y_position is an integer)
58
 
59
  draw(img) # Draw the text on the image
60
 
 
69
  final_video_path = os.path.join(tempfile.gettempdir(), "output_video.mp4")
70
  final_video.write_videofile(final_video_path, fps=24, codec="libx264")
71
  return final_video_path, None