我想使用 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
我听说此错误表明该文件不是波形文件。
所以我想知道 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)
...
关于python - gTTS 可以保存哪些类型的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49745456/