【问题标题】:How to save file as mp3 from Amazon Polly using Python如何使用 Python 从 Amazon Polly 将文件另存为 mp3
【发布时间】:2019-11-16 16:32:58
【问题描述】:

我正在使用 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 文件保存到我的电脑中?

【问题讨论】:

    标签: python amazon-web-services boto3 text-to-speech amazon-polly


    【解决方案1】:

    此代码示例直接取自文档: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()
    

    【讨论】:

    【解决方案2】:

    虽然与原始问题没有直接关系,但我回应了其中一个关于 hot to get to the audio stream without save the audio to a file的cmets。

    您还可以查看此示例的文档: 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 发出请求并将响应存储在响应对象中,而第二行从响应对象中获取音频流。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-17
      • 2023-03-14
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-30
      • 1970-01-01
      相关资源
      最近更新 更多