当前位置:   article > 正文

Python调用pyttsx3实现离线文字转语音_python pyttsx3

python pyttsx3

pyttsx3是 Python 中的文本到语音的离线转换库。

安装依赖:

pip install pyttsx3

Linux 安装要求:

apt update && apt install espeak ffmpeg libespeak1

使用方式:

  1. import pyttsx3
  2. engine = pyttsx3.init()
  3. engine.say("I will speak this text")
  4. engine.runAndWait()

带默认选项的朗读功能的单行用法

  1. import pyttsx3
  2. pyttsx3.speak("I will speak this text")

改变声音、语速和音量:

  1. import pyttsx3
  2. engine = pyttsx3.init() # object creation
  3. # 语速
  4. rate = engine.getProperty('rate')   # getting details of current speaking rate
  5. print (rate)                        #printing current voice rate
  6. engine.setProperty('rate', 125)     # setting up new voice rate
  7. # 音量
  8. volume = engine.getProperty('volume')   #getting to know current volume level (min=0 and max=1)
  9. print (volume)                          #printing current volume level
  10. engine.setProperty('volume',1.0)    # setting up volume level  between 0 and 1
  11. # 声音
  12. voices = engine.getProperty('voices')       #getting details of current voice
  13. #engine.setProperty('voice', voices[0].id)  #changing index, changes voices. o for male
  14. engine.setProperty('voice', voices[1].id)   #changing index, changes voices. 1 for female
  15. engine.say("Hello World!")
  16. engine.say('My current speaking rate is ' + str(rate))
  17. engine.runAndWait()
  18. engine.stop()
  19. # 保存到文件
  20. # On linux make sure that 'espeak' and 'ffmpeg' are installed
  21. engine.save_to_file('Hello World', 'test.mp3')
  22. engine.runAndWait()

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

闽ICP备14008679号