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

半世苍凉 提交于 2019-12-10 13:27:16

问题


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.


回答1:


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.




回答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
from comtypes.gen import SpeechLib    
engine = CreateObject("SAPI.SpVoice")
stream = CreateObject("SAPI.SpFileStream")
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()


来源:https://stackoverflow.com/questions/15516572/how-can-i-convert-text-to-speech-mp3-file-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!