How to convert text into audio file and play in br

2019-07-12 19:07发布

How to convert text into audio file which can be played in browser via python/django views? How can I do text-to-speech conversion in python? I want to convert a string to a .wav file, that will be played in a browser via python/django views.

For example:

text = "how are you?"
convert text to audio file (text.wav)
open text.wav file & play in browser via django view. 

2条回答
Lonely孤独者°
2楼-- · 2019-07-12 19:55

I have tried to do like following way & it works for me. Thanks.

#Write text to file
text_file_path = '/user/share/project/test.txt'
audio_file_path = '/user/share/project/test.wav'
text_file = open(text_file_path, "w")
text_file.write('How are you?')
text_file.close()

#Convert file
conv = 'flite -f "%s" -o "%s"' % (text_file_path, audio_file_path)
response = commands.getoutput(conv)

if os.path.isfile(audio_file_path):
    response = HttpResponse()
    f = open(audio_file_path, 'rb')
    response['Content-Type'] = 'audio/x-wav'
    response.write(f.read())
    f.close()
    return response
查看更多
女痞
3楼-- · 2019-07-12 20:02

As Tichodroma says, you should always see if someone has already asked your question before asking it again. Google search for python text to speech returns http://code.google.com/p/pyspeech/ and How to make Python speak, among others.

查看更多
登录 后发表回答