使用HTTP GET获取音频文件,然后在python 3中播放它.在python 3中运行TTS吗? [英] Get an audio file with HTTP GET and then play it in python 3. TTS in python 3?
问题描述
基本上我想要的是python 3中的TTS ...我已经尝试过pyttsx,但显然它仅适用于python 2我已经尝试过其他东西了...现在,我正在尝试使用 voicerss.org 中的api,但是我不知道如何通过HTTP获取它,然后再玩搭配pyglet的python ...
Basically what i want is TTS in python 3... I already tried pyttsx but aparently it only works with python 2 I already tried other things too... Now i'm trying with the api from voicerss.org but i can't figure out how can HTTP GET it and then play it with python with pyglet...
到目前为止,这是我的代码:
here is my code so far:
import pyglet
import urllib3
http = urllib3.PoolManager()
speechrequest = http.request('GET', 'http://api.voicerss.org/?key=04f49802d32d442ca997d4d2ea76d3d5&hl=pt-pt&c=wav&src=texto')
speech = pyglet.media.load(speechrequest)
speech.play()
pyglet.app.run()
错误是:
Traceback (most recent call last):
File "C:/Users/Diogo/Documents/test.py", line 6, in <module>
speech = pyglet.media.load(speechrequest)
File "C:\Users\Diogo\AppData\Local\Programs\Python\Python35\lib\site-packages\pyglet\media\__init__.py", line 1429, in load
source = get_source_loader().load(filename, file)
File "C:\Users\Diogo\AppData\Local\Programs\Python\Python35\lib\site-packages\pyglet\media\__init__.py", line 1410, in load
return riff.WaveSource(filename, file)
File "C:\Users\Diogo\AppData\Local\Programs\Python\Python35\lib\site-packages\pyglet\media\riff.py", line 185, in __init__
file = open(filename, 'rb')
TypeError: invalid file: <urllib3.response.HTTPResponse object at 0x0000002EB0730D30>
我想它没有将wav文件返回给python不知道为什么,因为.如果我输入' http://api.voicerss.org/?key=04f49802d32d442ca997d4d2ea76d3d5&hl=pt-pt&c=wav&src=texto ',它会返回一个wav文件...
i guess its not returning an wav file to python don't know why because. If i enter 'http://api.voicerss.org/?key=04f49802d32d442ca997d4d2ea76d3d5&hl=pt-pt&c=wav&src=texto' in internet explorer it returns a wav file...
所以,我的问题是:
我在代码中做错了什么,或者如果这样不可能,python怎么会说话?
So, my questions are:
what i am doing wrong with the code or if is impossible this way how can python speak?
请耐心等待,我只有18岁,而且我不会说英语,因为我是葡萄牙语...
Please be patient with me, i'm only 18 years old and i can't speak english properly, because i'm portuguese...
推荐答案
您可以在本地保存远程wav文件,并使用默认应用程序播放该文件:
You could save the remote wav file locally and play it using a default application:
#!/usr/bin/env python3
from urllib.request import urlretrieve
import webbrowser
filename = 'speech.wav'
urlretrieve("http://api.voicerss.org/?key=04f49802d32d442ca997d4d2ea76d3d5"
"&hl=pt-pt&c=wav&src=texto", filename)
webbrowser.open(filename) # play the file using default application
在Windows上,您可以使用 os.startfile()
代替 webbrowser.open()
,或直接在Python中播放wav文件,而无需外部应用程序:/p>
On Windows, you could use os.startfile()
instead of webbrowser.open()
or to play the wav file directly in Python without an external application:
import winsound
winsound.PlaySound('speech.wav', winsound.SND_FILENAME)
您的代码至少有两个问题:
Your code have at least two issues:
- 您应该将文件名(字符串)传递给
pyglet.media.load()
函数,而不是HTTPResponse
对象. -
pyglet
可能仍然无法播放:我得到pyglet.media.riff.RIFFFormatException:格式块的大小不正确.
系统上的错误.
- You should pass a filename (a string) to
pyglet.media.load()
function, not anHTTPResponse
object. pyglet
may fail to play it anyway: I getpyglet.media.riff.RIFFFormatException: Size of format chunk is incorrect.
error on my system.
您还可以播放远程wav文件而不将其保存到磁盘,即可以播放流式内容:
You could also play a remote wav file without saving it to disk i.e., you can play streaming content:
#!/usr/bin/env python3
from urllib.parse import quote
from urllib.request import urlopen
api_key = open('api.key', encoding='ascii').read().strip()
language = 'pt-pt'
text = quote("1 2 3")
playwav(urlopen('https://api.voicerss.org/?c=wav&f=8khz_8bit_mono&ssml=false&'
'key={api_key}&src={text}&hl={language}'.format(**vars())))
其中 playwav()
:
import wave
from contextlib import closing
import pyaudio # Windows: py -m pip install pyaudio
# $ sudo apt-get install python{,3}-pyaudio
player = None
def playwav(path_or_file):
global player
if player is None:
player = pyaudio.PyAudio()
with closing(wave.open(path_or_file, 'rb')) as wavfile, \
closing(player.open(
format=player.get_format_from_width(wavfile.getsampwidth()),
channels=wavfile.getnchannels(),
rate=wavfile.getframerate(),
output=True)) as stream:
while True:
data = wavfile.readframes(1024) # read 1024 frames at once
if not data: # EOF
break
stream.write(data)
这篇关于使用HTTP GET获取音频文件,然后在python 3中播放它.在python 3中运行TTS吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!