Skip to content
Related Articles

Related Articles

How to save pyttsx3 results to MP3 or WAV file?
  • Difficulty Level : Hard
  • Last Updated : 25 Feb, 2021
GeeksforGeeks - Summer Carnival Banner

In this article, we will see how to generate and save pyttsx3 results as mp3 and wav file. Pyttsx3 is a python module that provides a Text to Speech API. We can use this API to convert the text into voice. 

Environment setup:

To use pyttsx3 we have to install espeak and ffmpeg first.

sudo apt update
sudo apt install espeak
sudo apt install ffmpeg

Additionally, we need to install the latest version of pyttsx3

python3 -m pip install pyttsx3

We can confirm the installation by importing the module.

import pyttsx3

If the above statement runs without error, the environment setup is successful.

Using Pyttsx3

  • First, we have to initialize the pyttsx3 engine. The init() method does that for us.
  • Next, we need to create a string with the text we want to convert to audio.
  • The say() method takes the string as a parameter. It will set the string it has to speak.
  • Since the speech will take a while to play on the speaker of the machine, we need to wait for the process to complete. Hence, we need to call the runAndWait() method in order to let the interpreter stop the execution till then.
  • Below is the code for the above steps:

Python3






# Import the required module
import pyttsx3
  
# Create a string
string = "Lorem Ipsum is simply dummy text " \
    + "of the prting and typesetting industry."
  
# Initialize the Pyttsx3 engine
engine = pyttsx3.init()
  
# Command it to speak the given string
engine.say(string)
  
# Wait until above command is not finished.
engine.runAndWait()

Output:

Saving the produced sound in a file

  • Note that we need to have ffmpeg in our system. So make sure that the environment setup was done correctly.
  • Pyttsx3 comes with a save_to_file() method which takes the text to speak and the file path as an argument.
  • This method saves the given file in the path. However, this module is in development state, so in some operating systems, the volume and rate options may not work properly.
  • We have to keep the library updated to its latest version. Install the module using:
sudo apt install git
python3 -m pip install git+https://github.com/nateshmbhat/pyttsx3
  • This will directly install the latest version available.
  • Below is the code to do the same:

Python3




# Import the required module
import pyttsx3
  
# Create a string
string = "Lorem Ipsum is simply dummy text " \
    + "of the prting and typesetting industry."
  
# Initialize the Pyttsx3 engine
engine = pyttsx3.init()
  
# We can use file extension as mp3 and wav, both will work
engine.save_to_file(string, 'speech.mp3')
  
# Wait until above command is not finished.
engine.runAndWait()

Output:


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :