2023年11月6日 星期一

text to Speech Sample

# 先在console 視窗下  pip install gTTs

 from gtts import gTTS

import os

import subprocess


# 要轉換的文本

text = "明天會更好tomorrow will be better"


# 創建TTS物件

tts = gTTS(text, lang='zh-tw')  # 使用台灣華語語音


# 將語音儲存為檔案

tts.save("output.mp3")


# 播放語音

# os.system("mpg321 output.mp3")  # 需要安裝mpg321或使用其他播放器

# os.system("start wmplayer.exe output.mp3") #windows 10

subprocess.call(["start", "output.mp3"], shell=True) # windows 11


# 刪除暫存檔案

# os.remove("output.mp3")

沒有留言:

張貼留言