离线文本转语音库pyttsx3(目前接触到的声音效果最好的,基本上拿来就能用)

在现代应用程序中,文本转语音(Text-to-Speech, TTS)技术越来越受到重视。无论是为视力障碍人士提供帮助,还是为教育和娱乐应用增添趣味,TTS 都能发挥重要作用。今天,我们将介绍一个简单易用的 Python 库——pyttsx3,它可以帮助你轻松实现文本转语音功能。

什么是 pyttsx3?

pyttsx3 是一个 Python 库,用于将文本转换为语音。与其他 TTS 库不同,pyttsx3 是一个离线库,这意味着它不依赖于互联网连接,可以在本地计算机上运行。官网:https://github.com/nateshmbhat/pyttsx3

它支持多种语音引擎,包括 SAPI5(Windows)、NSSpeechSynthesizer(macOS)和 espeak(Linux)。具体不通系统使用的语音引擎为:

LinuxmacOSWindows
AVSpeech✅︎
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 pyobjc>=9.0.1"

engine.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 (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-ng' is installed
engine.save_to_file('Hello World', 'test.mp3')
engine.runAndWait()

支持说中文语音

import pyttsx3

def say_chinese(text):
    engine = pyttsx3.init()
    voices = engine.getProperty('voices')
    for voice in voices:
        if "Chinese" in voice.id:
            engine.setProperty('voice', voice.id)
            break
    engine.say(text)
    engine.runAndWait()

say_chinese("你好,世界")

试了一下,效果相当不错!

或者说,是目前测试的最好的!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值