使用不需要互联网的 Python 进行逼真的文本到语音转换?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48438686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Realistic text to speech with Python that doesn't require internet?
提问by Edgecase
I'm trying to create an artificially intelligent program (nothing really big or special) and I wanted it to have a voice (who wouldn't?). I've looked into espeak, festival, gTTS and they're nice and usable, but not realistic enough for me to really be proud of, if that makes sense. I've been looking for something more realistic. Like this
我正在尝试创建一个人工智能程序(没有什么特别大或特别的),我希望它有声音(谁不想?)。我已经研究过 espeak、festival、gTTS,它们很好且可用,但不够现实让我感到自豪,如果这是有道理的。我一直在寻找更现实的东西。像这样
from gtts import gTTS
tts = gTTS(text='what to say', lang='en')
tts.save('/path/to/file.mp3')
gTTS works fine. I love it. It's realistic, but it requires Internet.. The issue is, I want my application to be as independent as possible. And I hate depending on Internet access.
gTTS 工作正常。我喜欢它。这是现实的,但它需要互联网.. 问题是,我希望我的应用程序尽可能独立。我讨厌依赖互联网访问。
Are there any other options?
还有其他选择吗?
PS: I'm currently running Linux, so your OS might have a different solution.
PS:我目前正在运行 Linux,所以您的操作系统可能有不同的解决方案。
回答by Isma
Try to use pyttsx3 2.5, according the documentation:
根据文档,尝试使用 pyttsx3 2.5 :
gTTS which works perfectly in python3 but it needs internet connection to work since it relies on google to get the audio data.But Pyttsx is completely offline and works seemlesly and has multiple tts-engine support.
gTTS 在 python3 中完美运行,但它需要互联网连接才能工作,因为它依赖于谷歌来获取音频数据。
Works for Python 2 and 3
适用于 Python 2 和 3
To install it:
要安装它:
pip install pyttsx3
Using it should be as simple as:
使用它应该很简单:
import pyttsx3;
engine = pyttsx3.init();
engine.say("I will speak this text");
engine.runAndWait() ;
Edit 1 - Changing the voice
编辑 1 - 改变声音
To get a less robotic voice you can try to change the voice as follows:
要获得不那么机械化的声音,您可以尝试按如下方式更改声音:
engine.setProperty('voice', voice.id)
To get the available voices
获取可用的声音
voices = engine.getProperty('voices')
You can try the different available voices as explained in this question: Changing the voice with PYTTSX module in python.
您可以按照以下问题的说明尝试不同的可用语音:使用 python 中的 PYTTSX 模块更改语音。
Edit 2 - Selecting speech engine
编辑 2 - 选择语音引擎
The library supports the following engines:
该库支持以下引擎:
- sapi5 - SAPI5 on Windows
- nsss - NSSpeechSynthesizer on Mac OS X
- espeak - eSpeak on every other platform
- sapi5 - Windows 上的 SAPI5
- nsss - Mac OS X 上的 NSSpeechSynthesizer
- espeak - 在其他所有平台上进行 eSpeak
If espeak is not very natural you can try sapi5 if you are on Windows or nsss if you are on Mac OS X.
如果 espeak 不是很自然,您可以在 Windows 上尝试 sapi5,如果您在 Mac OS X 上,可以尝试 nsss。
You can specify the engine in the init method, e.g.:
您可以在 init 方法中指定引擎,例如:
pyttsx3.init(driverName='sapi5')
More info here: http://pyttsx3.readthedocs.io/en/latest/engine.html