python-3.x - 是否可以在不编写新文件的情况下将文本合成语音?

标签 python-3.x google-cloud-platform text-to-speech google-text-to-speech

我想使用 GCP 文本到语音 API 来合成文本到语音,几乎我能找到的每个示例都会写入一个新文件,我想在该函数输入文本并通过计算机扬声器读取它时执行此操作。我一直在尝试转换 GCP 上传的代码,表示“你好,世界”。我还没有找到一种方法可以在转换后立即阅读它。好像Watson和Azure有这个服务,但GCP没有?

client = texttospeech.TextToSpeechClient(credentials=credentials)


synthesis_input = texttospeech.types.SynthesisInput(text=string)


voice = texttospeech.types.VoiceSelectionParams(
    language_code='en-US',
    ssml_gender=texttospeech.enums.SsmlVoiceGender.NEUTRAL)


audio_config = texttospeech.types.AudioConfig(
    audio_encoding=texttospeech.enums.AudioEncoding.MP3)


response = client.synthesize_speech(synthesis_input, voice, audio_config)

with open('output.mp3', 'wb') as out:
    out.write(response.audio_content)
    print('Audio content written to file "output.mp3"')

任何帮助将不胜感激,我猜我缺少一些文档或简单的配置。

最佳答案

GCP 文本转语音 API 返回包含音频数据的响应。返回后如何处理这些数据取决于您。在上面的示例中,数据被写入文件。如果您愿意,您可以将该数据传输到音频播放器以立即播放,而不涉及文件。数据格式的选择是 WAV、MP3 或 OGG ...请参阅 https://cloud.google.com/text-to-speech/docs/reference/rest/v1beta1/text/synthesize#AudioEncoding .

至于播放音频数据的API... Play audio with Python

关于python-3.x - 是否可以在不编写新文件的情况下将文本合成语音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58310377/

相关文章:

java - 文本转语音功能不起作用

python - 无法从 url 读取压缩文件

python-3.x - 如何使用 Python3.6.4 为 CentOS 6.10 安装 Curses?

text-to-speech - Watson Java SDK 和 OkHttp 内存泄漏

google-cloud-platform - 谷歌云 SDK 文档

ssh - Google Cloud VM 实例 SSH 连接超时约 60 秒,保活 30 秒

Python GTTS错误: AttributeError: 'NoneType' object has no attribute 'group'

python-3.x - 形状 (1,) 和 () 在条件运算符上不兼容

python - 为什么文本没有在 Canvas 上渲染 Pygame

google-cloud-platform - Google Cloud Endpoints 以 VM 实例(计算引擎)的内部 IP 为目标?