Welcome folks today in this blog post we will be converting
text to audio
and saving it as mp3
audio file using gtts
library and we will be playing the audio file using playsound
library in python. All the full source code of the application is shown below.
Get Started
In order to get started you need to install the below libraries using the pip
command as shown below
pip install playsound
pip install gtts
After that you need to make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
from playsound import playsound from gtts import gTTS def playaudio(audio): playsound(audio) def convert_to_audio(text): audio = gTTS(text) audio.save("textaudio.mp3") playaudio("textaudio.mp3") convert_to_audio("John Williamson is the captain of New Zealand") |
As you can see we are importing the libraries at the top and then we are converting the text
to audio and saving it as mp3
file using the gtts
library. And then we are playing the audio
file using the playsound
library.