how can i convert text to speech (mp3 file) in python?

  • Last Update :
  • Techknowledgy :

There are several APIs available to convert text to speech in Python. One of such APIs is the Google Text to Speech API commonly known as the gTTS API. gTTS is a very easy to use tool which converts the text entered, into audio which can be saved as a mp3 file.,The gTTS API supports several languages including English, Hindi, Tamil, French, German and many more. The speech can be delivered in any one of the two available audio speeds, fast or slow. However, as of the latest update, it is not possible to change the voice of the generated audio.,Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.,This works for any platform.Now we are all set to write a sample program that converts text to speech.

To install the gTTS API, open terminal and write

pip install gTTS

Output

The output of the above program should be a
voice saying, 'Welcome to geeksforgeeks!'

Suggestion : 2

To generate the Audio file from the text file, i am using this code i hope it can help you

from comtypes.client
import CreateObject
engine = CreateObject("SAPI.SpVoice")
stream = CreateObject("SAPI.SpFileStream")
from comtypes.gen
import SpeechLib
infile = "SHIVA.txt"
outfile = "SHIVA-audio.wav"
stream.Open(outfile, SpeechLib.SSFMCreateForWrite)
engine.AudioOutputStream = stream
f = open(infile, 'r')
theText = f.read()
f.close()
engine.speak(theText)
stream.Close()

Suggestion : 3

Learning how to use Speech Recognition Python library for performing speech recognition to convert audio speech to text in Python.,Speech synthesis (or Text to Speech) is the computer-generated simulation of human speech. It converts human language text into human-like speech audio. In this tutorial, you will learn how you can convert text to speech in Python.,Well, pyttsx3 library comes to the rescue, it is a text to speech conversion library in Python, it looks for TTS engines pre-installed in your platform and uses them, here are the text-to-speech synthesizers that this library uses:,Learn how to play and record sound files using different libraries such as playsound, Pydub and PyAudio in Python.

To get started, let's install the required modules:

pip3 install gTTS pyttsx3 playsound

Open up a new Python file and import:

import gtts
from playsound
import playsound

It's pretty straightforward to use this library, you just need to pass text to the gTTS object that is an interface to Google Translate's Text to Speech API:

# make request to google to get synthesis
tts = gtts.gTTS("Hello world")

Awesome, you'll see a new file appear in the current directory, let's play it using playsound module installed previously:

# play the audio file
playsound("hello.mp3")

It isn't available only in English, you can use other languages as well by passing the lang parameter:

# in spanish
tts = gtts.gTTS("Hola Mundo", lang = "es")
tts.save("hola.mp3")
playsound("hola.mp3")

Suggestion : 4

Python Design Patterns,Now, we will define the complete Python program of text into speech.

{
   'af': 'Afrikaans',
   'sq': 'Albanian',
   'ar': 'Arabic',
   'hy': 'Armenian',
   'bn': 'Bengali',
   'bs': 'Bosnian',
   'ca': 'Catalan',
   'hr': 'Croatian',
   'cs': 'Czech',
   'da': 'Danish',
   'nl': 'Dutch',
   'en': 'English',
   'et': 'Estonian',
   'tl': 'Filipino',
   'fi': 'Finnish',
   'fr': 'French',
   'de': 'German',
   'el': 'Greek',
   'en-us': 'English (US)',
   'gu': 'Gujarati',
   'hi': 'Hindi',
   'hu': 'Hungarian',
   'is': 'Icelandic',
   'id': 'Indonesian',
   'it': 'Italian',
   'ja': 'Japanese',
   'en-ca': 'English (Canada)',
   'jw': 'Javanese',
   'kn': 'Kannada',
   'km': 'Khmer',
   'ko': 'Korean',
   'la': 'Latin',
   'lv': 'Latvian',
   'mk': 'Macedonian',
   'ml': 'Malayalam',
   'mr',
   'en-in': 'English (India)'
}
200
[<pyttsx3.voice.Voice object at 0x000002D617F00A20>, <pyttsx3.voice.Voice object at 0x000002D617D7F898>, <pyttsx3.voice.Voice object at 0x000002D6182F8D30>]

Suggestion : 5

To use the gTTS API to convert text into voice in Python:,Before you can use the text-to-speech converter in Python, you need to install the gTTS module on your system.,This guide teaches you how to use Python to convert text to speech. After reading this guide, you have the knowledge to build a small text-to-speech converter.,Today you learned how to use the gTTS module to convert text to speech in your Python program.

And if you are looking for a quick answer, first install gTTS module with:

pip install gTTS

And then create a Python script like this:

from gtts
import gTTS
import os

mytext = "Hi, this is an example of converting text to audio. This is a bot speaking here, not a real human!"
audio = gTTS(text = mytext, lang = "en", slow = False)

audio.save("example.mp3")
os.system("start example.mp3")

You can use pip to install it. Open up the command line tools and run the following command:

pip install gTTS

For example:

mytext = "Hi, this is an example of converting text to audio. This is a bot speaking here, not a real human!"

Now, let’s input the text to the gTTS engine and specify the language as English:

audio = gTTS(text = mytext, lang = "en", slow = False)