3

我将 Amazon Polly 用于 TTS,但我无法了解如何将转换后的语音保存到计算机中的 .mp3 文件中

我已经尝试过 gTTS,但我的任务需要 Amazon Polly。

import boto3
client = boto3.client('polly')
response = client.synthesize_speech
(Text = "Hello my name is Shubham", OuptutFormat = "mp3", VoiceId = 'Aditi')

现在,我应该怎么做才能播放这个转换后的语音或将它作为 .mp3 文件保存到我的电脑中?

4

2 回答 2

9

此代码示例直接取自文档:https ://docs.aws.amazon.com/polly/latest/dg/SynthesizeSpeechSamplePython.html

import boto3

polly_client = boto3.Session(
                aws_access_key_id=,                     
    aws_secret_access_key=,
    region_name='us-west-2').client('polly')

response = polly_client.synthesize_speech(VoiceId='Joanna',
                OutputFormat='mp3', 
                Text = 'This is a sample text to be synthesized.')

file = open('speech.mp3', 'wb')
file.write(response['AudioStream'].read())
file.close()
于 2019-07-06T10:36:11.307 回答
1

虽然与原始问题没有直接关系,但我回复了关于 hot to get to the audio stream without save the audio to a file 的评论之一。

您还可以查看此示例的文档: https ://docs.aws.amazon.com/polly/latest/dg/example-Python-server-code.html

这表明从 Polly 获得响应:

    response = polly.synthesize_speech(Text=text, VoiceId=voiceId, OutputFormat=outputFormat)
    data_stream=response.get("AudioStream")

第一行向 Polly 发出请求并将响应存储在响应对象中,而第二行从响应对象中获取音频流。

于 2019-07-27T15:57:34.963 回答