gTTS 可以保存什么样的文件?
What kind of files can gTTS save?
我想使用 gTTS
模块制作 WAVE
个文件。
我从 https://pypi.python.org/pypi/gTTS`
下载了 gTTS
模块
我可以用它制作一些文件并通过单击这些文件来播放声音。
但是我想知道gTTS
可以保存什么样的文件?
这是示例代码:
import tempfile
from gtts import gTTS
tts = gTTS(text='hello', lang='en', slow=True)
tts.save("hello.wav")
f = tempfile.TemporaryFile()
tts.write_to_fp(f)
f.close()
当我用我的代码使用声音文件时,我做不到。
from PySide import QtGui
from PySide import QtCore
import sys
import os
class HelloSpeaker(QtGui.QPushButton):
def __init__(self,parent=None):
super(HelloSpeaker,self).__init__(parent=None)
self.setText("Stream")
self.connect(self,QtCore.SIGNAL("clicked()"),self.play_sound)
def play_sound(self):
sound = QtGui.QSound(os.getcwd()+"hello.wav")
sound.play(os.getcwd()+"hello.wav")
def main():
try:
QtGui.QApplication([])
except Exception as e:
print(28,e)
hello = HelloSpeaker()
hello.show()
sys.exit(QtGui.QApplication.exec_())
if __name__ == "__main__":
main()
是的,我在堆栈溢出中发现了一些 QSound 问题。
由于某些未知原因,QSound 无法正常工作。
例如:
QSound::play("soundpath") call works but a QSound object doesn't
Playing sound with Pyqt4 and QSound
QT5 QSound does not play all wave files
打扰别人
但我不认为是 QSound 造成的,我怀疑 gTTS 不能制作 .wav
文件。
因为
import wave
wf = wave.open("hello.wav", "rb")
结果:
wave.Error: file does not start with RIFF id
我听说这个错误表明这个文件不是 wave 文件。
所以我想知道 gTTS 不会生成“.wav”文件。
(但我可以通过iTunes,其他播放器播放。)
只能gTTS
制作"mp3"个文件吗?
环境:
python 3.6.3 pyside1.2.4
gTTS只是直接保存字节,不做任何转换,而且这些字节是mp3编码的,你可以通过查看source code:
来验证
....
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=InsecureRequestWarning)
r = requests.get(self.GOOGLE_TTS_URL,
params=payload,
headers=headers,
proxies=urllib.request.getproxies(),
verify=False)
if self.debug:
print("Headers: {}".format(r.request.headers))
print("Request url: {}".format(r.request.url))
print("Response: {}, Redirects: {}".format(r.status_code, r.history))
r.raise_for_status()
for chunk in r.iter_content(chunk_size=1024): # write the bytes directly
fp.write(chunk)
...
我想使用 gTTS
模块制作 WAVE
个文件。
我从 https://pypi.python.org/pypi/gTTS`
下载了gTTS
模块
我可以用它制作一些文件并通过单击这些文件来播放声音。
但是我想知道gTTS
可以保存什么样的文件?
这是示例代码:
import tempfile
from gtts import gTTS
tts = gTTS(text='hello', lang='en', slow=True)
tts.save("hello.wav")
f = tempfile.TemporaryFile()
tts.write_to_fp(f)
f.close()
当我用我的代码使用声音文件时,我做不到。
from PySide import QtGui
from PySide import QtCore
import sys
import os
class HelloSpeaker(QtGui.QPushButton):
def __init__(self,parent=None):
super(HelloSpeaker,self).__init__(parent=None)
self.setText("Stream")
self.connect(self,QtCore.SIGNAL("clicked()"),self.play_sound)
def play_sound(self):
sound = QtGui.QSound(os.getcwd()+"hello.wav")
sound.play(os.getcwd()+"hello.wav")
def main():
try:
QtGui.QApplication([])
except Exception as e:
print(28,e)
hello = HelloSpeaker()
hello.show()
sys.exit(QtGui.QApplication.exec_())
if __name__ == "__main__":
main()
是的,我在堆栈溢出中发现了一些 QSound 问题。
由于某些未知原因,QSound 无法正常工作。
例如:
QSound::play("soundpath") call works but a QSound object doesn't Playing sound with Pyqt4 and QSound QT5 QSound does not play all wave files
打扰别人
但我不认为是 QSound 造成的,我怀疑 gTTS 不能制作 .wav
文件。
因为
import wave
wf = wave.open("hello.wav", "rb")
结果:
wave.Error: file does not start with RIFF id
我听说这个错误表明这个文件不是 wave 文件。
所以我想知道 gTTS 不会生成“.wav”文件。 (但我可以通过iTunes,其他播放器播放。)
只能gTTS
制作"mp3"个文件吗?
环境:
python 3.6.3 pyside1.2.4
gTTS只是直接保存字节,不做任何转换,而且这些字节是mp3编码的,你可以通过查看source code:
来验证....
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=InsecureRequestWarning)
r = requests.get(self.GOOGLE_TTS_URL,
params=payload,
headers=headers,
proxies=urllib.request.getproxies(),
verify=False)
if self.debug:
print("Headers: {}".format(r.request.headers))
print("Request url: {}".format(r.request.url))
print("Response: {}, Redirects: {}".format(r.status_code, r.history))
r.raise_for_status()
for chunk in r.iter_content(chunk_size=1024): # write the bytes directly
fp.write(chunk)
...