当前位置:   article > 正文

Python调用edge-tts实现在线文字转语音_edge_tts

edge_tts

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-ttsicon-default.png?t=N7T8https://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

改变速率、音量和音高

  1. edge-tts --rate=-50% --text "Hello, world!" --write-media hello.mp3 --write-subtitles hello.vtt
  2. edge-tts --volume=-50% --text "Hello, world!" --write-media hello.mp3 --write-subtitles hello.vtt
  3. edge-tts --pitch=-50Hz --text "Hello, world!" --write-media hello.mp3 --write-subtitles hello.vtt

播放音频

edge-playback

edge-playback 用于播放生成的语音。它采用与 edge-tts 相同的参数。

Python代码方式

文字转音频

  1. import asyncio
  2. import edge_tts
  3. TEXT = "Hello World!"
  4. VOICE = "en-GB-SoniaNeural"
  5. OUTPUT_FILE = "test.mp3"
  6. async def amain() -> None:
  7. """Main function"""
  8. communicate = edge_tts.Communicate(TEXT, VOICE)
  9. await communicate.save(OUTPUT_FILE)
  10. if __name__ == "__main__":
  11. loop = asyncio.get_event_loop_policy().get_event_loop()
  12. try:
  13. loop.run_until_complete(amain())
  14. finally:
  15. loop.close()

使用VoicesManager进行动态语音选择的示例

  1. import asyncio
  2. import random
  3. import edge_tts
  4. from edge_tts import VoicesManager
  5. TEXT = "Hoy es un buen día."
  6. OUTPUT_FILE = "spanish.mp3"
  7. async def amain() -> None:
  8. """Main function"""
  9. voices = await VoicesManager.create()
  10. voice = voices.find(Gender="Male", Language="es")
  11. # Also supports Locales
  12. # voice = voices.find(Gender="Female", Locale="es-AR")
  13. communicate = edge_tts.Communicate(TEXT, random.choice(voice)["Name"])
  14. await communicate.save(OUTPUT_FILE)
  15. if __name__ == "__main__":
  16. loop = asyncio.get_event_loop_policy().get_event_loop()
  17. try:
  18. loop.run_until_complete(amain())
  19. finally:
  20. loop.close()

流式传输来自TTS的音频数据

  1. import asyncio
  2. import edge_tts
  3. TEXT = "Hello World!"
  4. VOICE = "en-GB-SoniaNeural"
  5. OUTPUT_FILE = "test.mp3"
  6. async def amain() -> None:
  7. """Main function"""
  8. communicate = edge_tts.Communicate(TEXT, VOICE)
  9. with open(OUTPUT_FILE, "wb") as file:
  10. async for chunk in communicate.stream():
  11. if chunk["type"] == "audio":
  12. file.write(chunk["data"])
  13. elif chunk["type"] == "WordBoundary":
  14. print(f"WordBoundary: {chunk}")
  15. if __name__ == "__main__":
  16. loop = asyncio.get_event_loop_policy().get_event_loop()
  17. try:
  18. loop.run_until_complete(amain())
  19. finally:
  20. loop.close()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/364377?site
推荐阅读
相关标签
  

闽ICP备14008679号