Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# 文字转视频功能
|
| 2 |
def text_to_video(text, voice, rate, pitch, video_width, video_height, bg_color, text_color, text_font, text_size):
|
| 3 |
# 字体文件路径
|
|
@@ -69,3 +86,23 @@ def text_to_video(text, voice, rate, pitch, video_width, video_height, bg_color,
|
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import tempfile
|
| 3 |
+
import asyncio
|
| 4 |
+
from moviepy.editor import AudioFileClip, ImageClip, concatenate_videoclips
|
| 5 |
+
from wand.image import Image
|
| 6 |
+
from wand.color import Color
|
| 7 |
+
from wand.drawing import Drawing
|
| 8 |
+
from gtts import gTTS
|
| 9 |
+
|
| 10 |
+
# 文本转语音功能
|
| 11 |
+
async def text_to_speech(text, voice, rate, pitch):
|
| 12 |
+
# 使用 gTTS 生成语音
|
| 13 |
+
tts = gTTS(text=text, lang=voice)
|
| 14 |
+
audio_file = os.path.join(tempfile.gettempdir(), "audio.mp3")
|
| 15 |
+
tts.save(audio_file)
|
| 16 |
+
return audio_file, None
|
| 17 |
+
|
| 18 |
# 文字转视频功能
|
| 19 |
def text_to_video(text, voice, rate, pitch, video_width, video_height, bg_color, text_color, text_font, text_size):
|
| 20 |
# 字体文件路径
|
|
|
|
| 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 |
+
if __name__ == "__main__":
|
| 92 |
+
# 测试功能
|
| 93 |
+
text = "你好,这是一个测试视频。"
|
| 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 |
+
video_path, warning = text_to_video(text, voice, rate, pitch, video_width, video_height, bg_color, text_color, text_font, text_size)
|
| 105 |
+
if warning:
|
| 106 |
+
print("Warning:", warning)
|
| 107 |
+
else:
|
| 108 |
+
print("Video saved at:", video_path)
|