Home >Backend Development >Python Tutorial >How to convert text to audio through Python's pyttsx3 library
pyttsx3 is an open source Python text-to-speech library that can convert text into natural human speech. It provides rich and flexible configuration options, you can customize the voice, speaking speed, intonation, etc., and supports multi-language conversion. In addition, it also supports asynchronous operations and can automatically call the speech synthesis engine in the background without blocking the main program. Pyttsx3 can be widely used in various fields, such as automated voice prompts, intelligent voice assistants, voice verification, etc. It is a very excellent Python voice processing library.
pip install pyttsx3
pip show pyttsx3
Name: pyttsx3
Version: 2.90
Summary: Text to Speech (TTS) library for Python 2 and 3. Works without internet connection or delay. Supports multiple TTS engines, including Sapi5, nsss, and espeak.
Home-page: https://github.com/nateshmbhat/pyttsx3
import pyttsx3
text = '大家好,我是空空star,本篇给大家分享一下文字转音频,这是通过pyttsx3转换的音频。'
engine = pyttsx3.init()
# 获取所有可用的声音列表 voices = engine.getProperty('voices') # 选择一个指定语音(粤语语音sinji) engine.setProperty('voice', voices[36].id)
Here I am using the Cantonese voice under the mac system
com.apple.speech.synthesis.voice.sinji
engine.setProperty('rate', 150)
engine.setProperty('volume', 0.8)
engine.say(text)
local = '/Users/kkstar/Downloads/video/' engine.save_to_file(text, local+"audio_pyttsx3.mp3")
engine.runAndWait()
engine.runAndWait() is used to wait for the text conversion to be completed before continuing the execution of the program when converting text to speech. Its purpose is to avoid premature termination of the program when converting text to speech, resulting in the text not being fully converted.
Since mp3 files cannot be inserted into the blog, we first convert them to mp4, and everyone can listen to the conversion effect through the mp4 sound.
pyttsx3-text to audio effect demonstration
The above is the detailed content of How to convert text to audio through Python's pyttsx3 library. For more information, please follow other related articles on the PHP Chinese website!