概述
Pyttsx3 是一个 python 模块,提供了文本到语音的 API。我们可以用这个 API 把文本转换成语音。
安装
pip install pyttsx
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyttsx3
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pyttsx3
使用
pypi上的描述已经很详细,可点击参考:pyttsx3 · PyPI
拷贝部分代码至此供参考
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 (min=0 and max=1)
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 female
engine.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' and 'ffmpeg' are installed
engine.save_to_file('Hello World', 'test.mp3')
engine.runAndWait()
在进行 engine.setProperty('voice', voices[1].id)时,可以先通过 engine.getProperty('voices')查看本地支持的voice type。
import pyttsx3
engine = pyttsx3.init()
voices = engine.getProperty('voices')
for voice in voices:
print("Voices:")
print(" - ID: %s" % voice.id)
print(" - NAME: %s" % voice.name)
print(" - Languages: %s" % voice.languages)
print(" - Gender: %s" % voice.gender)
print(" - Age: %s" % voice.age)
根据读取到的voice id,进行自定义设置。
zh_voice_id = 'com.apple.speech.synthesis.voice.meijia'
zh_voice_id = 'com.apple.speech.synthesis.voice.tingting'
engine.setProperty('voice', zh_voice_id)
ubuntu上安装pyttsx3
sudo apt update
sudo apt install espeak
sudo apt install ffmpeg
pip install pyttsx3
如果本地linux系统不支持中文语音,可参考如下链接配置本地环境:
[ubuntu完美安装espeak支持中文和粤语 不再报错:Full dictionary is not installed for 'zh'
Ubuntu18.04上生成的中文音频文件真的是不敢恭维,语音真的很机械。如需播报mp3文件,建议在Mac的本子上创建mp3文件,拷贝至Ubuntu上进行播报。
其他
MAC 或者 Ubuntu 如需播报mp3音频文件,需要 play 命令,安装sox即可。
MAC
brew install sox
Ubuntu18.04
sudo apt install sox
sudo apt-get install -y libsox-fmt-all
1. sudo usermod -a -G pulse-access userName (添加当前用户到pulse-access)
2. pactl list short sinks (查询当前声卡状态)
3. pulseaudio -k (可选,当声卡的状态是auto_null时执行此命令,目的是删除pulseaudio已有的链接)
4. sudo pulseaudio --system -D (删除已有的声卡链接,并给予当前用户新建链接)
5. pactl list short sinks (声卡状态查询)
0 alsa_output.pci-0000_00_1f.3.analog-stereo module-alsa-card.c s16le 2ch 44100Hz SUSPENDED