Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert text to speech (mp3 file) in python?

I can convert text to speech in python using puttsx. and I can do record audio using microphone(headphone) to mp3 file.

What I want to do is to convert text to mp3 file.
Is there a way to store audio playing using pyttsx to memory or unicode string.

Can anyone help me storing audio to memory, or how I can convert that string to mp3 file.

like image 681
Deepak Verma Avatar asked Mar 20 '13 06:03

Deepak Verma


People also ask

How do I convert text to an mp3 in Python?

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.

How do I convert text to audio files?

Open a document using text speaker and click Speak >> Convert to Audio. The audio properties such as the sound quality, frequency, bit rate, and audio channel are already set for you to produce the optimum audio files. However, you can change these settings depending on your requirement.


2 Answers

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()
like image 119
SHIVA73 Avatar answered Oct 24 '22 02:10

SHIVA73


I do not know about pyttsx, but a while ago I used the Google TTS API to generate MP3s from text.

You can get an idea of how it works from this code snippet. The free version of Google TTS is limited to a certain number of letters for each request, So I'd recommend splitting the text into sentences and creating a file for each sentence.

If you need help with that, please tell me.

like image 32
Thorsten Kranz Avatar answered Oct 24 '22 02:10

Thorsten Kranz