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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-04-20 13:22:11
forward
2449 people have browsed it

    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
    Copy after login

    3. Check the pyttsx3 version

     pip show pyttsx3
    Copy after login

    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
    Copy after login

    2.Define the text that needs to be converted

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

    3.Initialize the pyttsx3 engine

    engine = pyttsx3.init()
    Copy after login

    4. Set the sound

    # 获取所有可用的声音列表
    voices = engine.getProperty('voices')
    # 选择一个指定语音(粤语语音sinji)
    engine.setProperty('voice', voices[36].id)
    Copy after login

    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)
    Copy after login

    6.Set the volume

    engine.setProperty('volume', 0.8)
    Copy after login

    7.Play the voice

    engine.say(text)
    Copy after login

    8.Save the voice

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

    9. Wait for the language conversion to be completed

    engine.runAndWait()
    Copy after login

    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!

    Related labels:
    source:yisu.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template