Python Polly返回0字节
尝试通过AWS polly执行基本tts任务时,我可以使用CLI通过以下命令类型获得可播放的mp3:Python Polly返回0字节,python,amazon-web-services,python-os,amazon-polly,Python,Amazon Web Services,Python Os,Amazon Polly,尝试通过AWS polly执行基本tts任务时,我可以使用CLI通过以下命令类型获得可播放的mp3: aws polly synthesize-speech --output-format mp3 --voice-id Joanna --text 'Hello, my name is Joanna. I learned about the W3C on 10/3 of last year.' hello.mp3 但是,按照提供的示例,我将返回0字节的mp3文件。 我的代码: 您在带有的中的
aws polly synthesize-speech --output-format mp3 --voice-id Joanna --text 'Hello, my name is Joanna. I learned about the W3C on 10/3 of last year.' hello.mp3
但是,按照提供的示例,我将返回0字节的mp3文件。
我的代码:
您在带有的
中的缩进不正确
这项工作:
导入boto3
从botocore.exceptions导入botocorerror、ClientError
从上下文库导入关闭
导入系统
polly=bot3.客户(“polly”)
尝试:
回答=polly.synthesis\u语音(
Text=“这就是你写的。如果它也能起作用,那就太酷了。”,
OutputFormat=“mp3”,
VoiceId=“乔安娜”,
)
除了(BotoCreerror、ClientError)作为错误:
打印(错误)
系统出口(-1)
如果响应为“音频流”:
关闭(响应[“AudioStream”])作为流:
output=“speech.mp3”
尝试:
打开(输出,“wb”)作为文件:
file.write(stream.read())
除了IOError作为错误:
打印(错误)
系统出口(-1)
其他:
打印(“无法流式播放音频”)
系统出口(-1)
try块上缺少一个缩进。谢谢你,约翰
from boto3 import Session
from botocore.exceptions import BotoCoreError, ClientError
from contextlib import closing
import os
import sys
import subprocess
from tempfile import gettempdir
session = Session(profile_name="default")
polly = session.client("polly")
try:
response = polly.synthesize_speech(Text="This is what you wrote. It'd be cool if it also
worked.",
OutputFormat="mp3",
VoiceId="Joanna",
)
except (BotoCoreError, ClientError) as error:
print(error)
sys.exit(-1)
if "AudioStream" in response:
with closing(response["AudioStream"]) as stream:
output = ("speech.mp3")
print(os.path.join(gettempdir(), "hopa"))
try:
with open(output, "wb") as file:
file.write(stream.read())
except IOError as error:
print(error)
sys.exit(-1)
else:
print("Could not stream audio")
sys.exit(-1)
os.startfile(output)