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.
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.
If the above statement runs without error, the environment setup is successful.
- 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:
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:
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.