有什么好的网站吗,开课啦wordpress主题下载,网站建设吉金手指排名14,做app模板网站有哪些在现代应用程序中#xff0c;文本转语音#xff08;Text-to-Speech, TTS#xff09;技术越来越受到重视。无论是为视力障碍人士提供帮助#xff0c;还是为教育和娱乐应用增添趣味#xff0c;TTS 都能发挥重要作用。今天#xff0c;我们将介绍一个简单易用的 Python 库——…在现代应用程序中文本转语音Text-to-Speech, TTS技术越来越受到重视。无论是为视力障碍人士提供帮助还是为教育和娱乐应用增添趣味TTS 都能发挥重要作用。今天我们将介绍一个简单易用的 Python 库——pyttsx3它可以帮助你轻松实现文本转语音功能。
什么是 pyttsx3
pyttsx3 是一个 Python 库用于将文本转换为语音。与其他 TTS 库不同pyttsx3 是一个离线库这意味着它不依赖于互联网连接可以在本地计算机上运行。官网https://github.com/nateshmbhat/pyttsx3
它支持多种语音引擎包括 SAPI5Windows、NSSpeechSynthesizermacOS和 espeakLinux。具体不通系统使用的语音引擎为
LinuxmacOSWindowsAVSpeech✅︎eSpeak✅︎✅︎✅︎NSSpeechSynthesizer✅︎SAPI5✅︎ 实践操作
安装pyttsx3
只要安装pyttsx3库即可。如果是在linux系统需要安装espeak-ng库。windows下以前已经安装过espeak-ng库所以倒不确定了
pip install pyttsx3
sudo apt update sudo apt install espeak-ng libespeak1
使用
简单使用
初始化引擎然后朗读文本
import pyttsx3
engine pyttsx3.init()# For Mac, If you face error related to pyobjc when running the init() method :
# Install 9.0.1 version of pyobjc : pip install pyobjc9.0.1engine.say(I will speak this text)
engine.runAndWait()
最简单语句使用
只需要一条命令就可以直接朗读文本
import pyttsx3
pyttsx3.speak(I will speak this text)
修改语音voice、速率rate和音量volume
import pyttsx3
engine pyttsx3.init() # object creation# RATE
rate engine.getProperty(rate) # getting details of current speaking rate
print (rate) # printing current voice rate
engine.setProperty(rate, 125) # setting up new voice rate# VOLUME
volume engine.getProperty(volume) # getting to know current volume level (min0 and max1)
print (volume) # printing current volume level
engine.setProperty(volume,1.0) # setting up volume level between 0 and 1# VOICE
voices engine.getProperty(voices) # getting details of current voice
#engine.setProperty(voice, voices[0].id) # changing index, changes voices. o for male
engine.setProperty(voice, voices[1].id) # changing index, changes voices. 1 for femaleengine.say(Hello World!)
engine.say(My current speaking rate is str(rate))
engine.runAndWait()
engine.stop()# Saving Voice to a file
# On Linux, make sure that espeak-ng is installed
engine.save_to_file(Hello World, test.mp3)
engine.runAndWait()
支持说中文语音
import pyttsx3def say_chinese(text):engine pyttsx3.init()voices engine.getProperty(voices)for voice in voices:if Chinese in voice.id:engine.setProperty(voice, voice.id)breakengine.say(text)engine.runAndWait()say_chinese(你好世界)试了一下效果相当不错
或者说是目前测试的最好的