python - gTTS 可以保存哪些类型的文件?

标签 python pyqt4 pyside wave

我想使用 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/

相关文章:

python - QSqlQuery::exec: 数据库未打开,PyQt

pyqt4 - 在 PyQt 中使用参数创建信号

python - 为什么我的 QListView 在 IconMode 中是空的?

python - 如何在不点击标题的情况下按列对 QTableWidget 进行排序?

python - 使用 python 将 Dialogflow 代理导出/转换为 csv 或 excel 文件

python - 如何在 matplotlib.pyplot 中将 colorbar 与 hist2d 一起使用?

python - 当我使用 [ :]? 时,为什么我的子类的 __getitem__ 和 __setitem__ 没有被调用

python - 在 python 3 中使用带逗号的打印

python - 使用装饰器在 __init__ 之后注入(inject)函数调用

python - PyCharm 使用 Anaconda python.exe 和模块失败 - ModuleNotFoundError : No module named 'PySide'