Python調用edge-tts實現(xiàn)在線文字轉語音效果
edge-tts是一個 Python 模塊,允許通過Python代碼或命令的方式使用 Microsoft Edge 的在線文本轉語音服務。
項目源碼
GitHub - rany2/edge-tts: Use Microsoft Edge's online text-to-speech service from Python WITHOUT needing Microsoft Edge or Windows or an API keyUse Microsoft Edge's online text-to-speech service from Python WITHOUT needing Microsoft Edge or Windows or an API key - rany2/edge-tts
https://github.com/rany2/edge-tts
安裝
pip install edge-tts
用法
命令行方式
- --write-media:輸出音頻
- --write-subtitles:輸出字幕
edge-tts --text "Hello, world!" --write-media hello.mp3 --write-subtitles hello.vtt
選項檢查可用的聲音
edge-tts --list-voices
改變聲音
--voice:指定聲音
edge-tts --voice zh-CN-XiaoxiaoNeural --text "君不見黃河之水天上來" --write-media hello.mp3 --write-subtitles hello.vtt
改變速率、音量和音高
edge-tts --rate=-50% --text "Hello, world!" --write-media hello.mp3 --write-subtitles hello.vtt edge-tts --volume=-50% --text "Hello, world!" --write-media hello.mp3 --write-subtitles hello.vtt edge-tts --pitch=-50Hz --text "Hello, world!" --write-media hello.mp3 --write-subtitles hello.vtt
播放音頻
edge-playback
edge-playback 用于播放生成的語音。它采用與 edge-tts 相同的參數(shù)。
Python代碼方式
文字轉音頻
import asyncio
import edge_tts
TEXT = "Hello World!"
VOICE = "en-GB-SoniaNeural"
OUTPUT_FILE = "test.mp3"
async def amain() -> None:
"""Main function"""
communicate = edge_tts.Communicate(TEXT, VOICE)
await communicate.save(OUTPUT_FILE)
if __name__ == "__main__":
loop = asyncio.get_event_loop_policy().get_event_loop()
try:
loop.run_until_complete(amain())
finally:
loop.close()使用VoicesManager進行動態(tài)語音選擇的示例
import asyncio
import random
import edge_tts
from edge_tts import VoicesManager
TEXT = "Hoy es un buen día."
OUTPUT_FILE = "spanish.mp3"
async def amain() -> None:
"""Main function"""
voices = await VoicesManager.create()
voice = voices.find(Gender="Male", Language="es")
# Also supports Locales
# voice = voices.find(Gender="Female", Locale="es-AR")
communicate = edge_tts.Communicate(TEXT, random.choice(voice)["Name"])
await communicate.save(OUTPUT_FILE)
if __name__ == "__main__":
loop = asyncio.get_event_loop_policy().get_event_loop()
try:
loop.run_until_complete(amain())
finally:
loop.close()流式傳輸來自TTS的音頻數(shù)據(jù)
import asyncio
import edge_tts
TEXT = "Hello World!"
VOICE = "en-GB-SoniaNeural"
OUTPUT_FILE = "test.mp3"
async def amain() -> None:
"""Main function"""
communicate = edge_tts.Communicate(TEXT, VOICE)
with open(OUTPUT_FILE, "wb") as file:
async for chunk in communicate.stream():
if chunk["type"] == "audio":
file.write(chunk["data"])
elif chunk["type"] == "WordBoundary":
print(f"WordBoundary: {chunk}")
if __name__ == "__main__":
loop = asyncio.get_event_loop_policy().get_event_loop()
try:
loop.run_until_complete(amain())
finally:
loop.close()到此這篇關于Python調用edge-tts實現(xiàn)在線文字轉語音的文章就介紹到這了,更多相關Python在線文字轉語音內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python + winrm 實現(xiàn)遠程連接Windows服務器并執(zhí)行指定命令的操作過程
Windows遠程管理(WinRM)是Windows Server 2003 R2,Windows Vista和Windows Server 2008中一種新式的方便遠程管理的服務,這篇文章主要介紹了python + winrm 實現(xiàn)遠程連接Windows服務器并執(zhí)行指定命令的操作過程,需要的朋友可以參考下2023-10-10

