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

3

6

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.

Parodic answered 20/3, 2013 at 6:15 Comment(0)
F
3

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.

Fleabitten answered 20/3, 2013 at 6:49 Comment(4)
I will try this technique at my end first. If i got any problem will ask you for help.Parodic
Note that Google Translate only accepts up to 100 characters via this method.Gipson
Yes. As I was not sure if this limit would evolve over time I did not mention precise numbers.Fleabitten
It returns: urllib2.HTTPError: HTTP Error 503: Service UnavailableEcchymosis
D
5

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()
Dial answered 16/5, 2013 at 15:1 Comment(0)
F
3

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.

Fleabitten answered 20/3, 2013 at 6:49 Comment(4)
I will try this technique at my end first. If i got any problem will ask you for help.Parodic
Note that Google Translate only accepts up to 100 characters via this method.Gipson
Yes. As I was not sure if this limit would evolve over time I did not mention precise numbers.Fleabitten
It returns: urllib2.HTTPError: HTTP Error 503: Service UnavailableEcchymosis
E
2

You can bypass the 100 character rule by splitting the 'theText' string into an array: theText = f.read().split("<null>") (i used "<null>" as the delimiter) put the delimiter in the text every sentence or space before 100 characters. Create a for-loop: for section in theText: and every section run engine.Speak(section)

I hope this helps with 100 character limits!

Edenedens answered 14/4, 2022 at 8:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.