AI 生成: 一个基于 AI 的全自动短视频生成工具,输入主题即可自动生成脚本、配音、字幕和合成视频。使用 Python + Flask 提供 Web 界面,支持命令行模式。
This commit is contained in:
+33
@@ -0,0 +1,33 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""语音合成模块(pyttsx3 离线 TTS)"""
|
||||
|
||||
import os
|
||||
import pyttsx3
|
||||
|
||||
|
||||
def text_to_speech(text: str, out_path: str, rate: int = 160) -> str:
|
||||
"""将文本合成为音频文件。
|
||||
|
||||
Args:
|
||||
text: 待合成文本
|
||||
out_path: 输出音频路径(如 output/audio/1.mp3)
|
||||
rate: 语速
|
||||
|
||||
Returns:
|
||||
str: 输出文件路径
|
||||
"""
|
||||
os.makedirs(os.path.dirname(out_path), exist_ok=True)
|
||||
engine = pyttsx3.init()
|
||||
engine.setProperty("rate", rate)
|
||||
# 选择第一个可用语音(不同系统可能不同)
|
||||
voices = engine.getProperty("voices")
|
||||
if voices:
|
||||
engine.setProperty("voice", voices[0].id)
|
||||
engine.save_to_file(text, out_path)
|
||||
engine.runAndWait()
|
||||
engine.stop()
|
||||
return out_path
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(text_to_speech("你好,这是一段测试语音。", "output/audio/test.mp3"))
|
||||
Reference in New Issue
Block a user