Home >Backend Development >Python Tutorial >How to convert text to audio through Python's pyttsx3 library

How to convert text to audio through Python's pyttsx3 library

WBOY
WBOYforward
2023-04-20 13:22:112235browse

    1. What is pyttsx3?

    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.

    2. Install pyttsx3

    pip install pyttsx3

    3. Check the pyttsx3 version

     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

    4. Use of pyttsx3

    1.Introduce the library

    import pyttsx3

    2.Define the text that needs to be converted

    text = '大家好,我是空空star,本篇给大家分享一下文字转音频,这是通过pyttsx3转换的音频。'

    3.Initialize the pyttsx3 engine

    engine = pyttsx3.init()

    4. Set the sound

    # 获取所有可用的声音列表
    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

    5.Set the speaking speed

    engine.setProperty('rate', 150)

    6.Set the volume

    engine.setProperty('volume', 0.8)

    7.Play the voice

    engine.say(text)

    8.Save the voice

    local = '/Users/kkstar/Downloads/video/'
    engine.save_to_file(text, local+"audio_pyttsx3.mp3")

    9. Wait for the language conversion to be completed

    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.

    5. Voice effect

    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

    How to convert text to audio through Python's pyttsx3 library

    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!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete