Файл .mp3, сгенерированный AWS Polly, не запускается вообще
Я создал rest API с помощью Django Rest Framework. Я собираюсь преобразовать предложение в речь с помощью AWS Polly и попытался запустить его в моей локальной среде. Когда я запускаю конечную точку, в корневой папке создается файл .mp3. В сообщении о результате говорится: "Успешное сохранение!". Но файл не работает, так как файл содержит размер. Я ничего не слышу. Так как на самом деле файл результата должен работать Media player.
Я следил за статьей AWS Polly, [здесь][1]
Класс:
class TexttoSpeechView(CreateAPIView):
serializer_class = TexttoSpeechSerializer
permission_classes = (IsAccessedUser,)
def post(self, request, *args, **kwargs):
serializer = self.serializer_class(data=request.data)
if serializer.is_valid():
text = serializer.data.get('text')
voice = "Mizuki"
response = use_polly(text)
if "AudioStream" in response:
# Note: Closing the stream is important because the service throttles on the
# number of parallel connections. Here we are using contextlib.closing to
# ensure the close method of the stream object will be called automatically
# at the end of the with statement's scope.
with closing(response["AudioStream"]) as stream:
output = os.path.join(BASE_DIR, "speech.mp3")
data = stream.read()
try:
# Open a file for writing the output as a binary stream
with open(output, "wb") as file:
file.write(data)
print("save success")
except IOError as error:
# Could not write to file, exit gracefully
print("error1")
sys.exit(-1)
else:
# The response didn't contain audio data, exit gracefully
print("Could not stream audio")
sys.exit(-1)
return Response(serializer.data, status=status.HTTP_204_NO_CONTENT)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Определить use_polly
def use_polly(text):
# create session
# Create a client using the credentials and region defined in the [adminuser]
# section of the AWS credentials file (~/.aws/credentials).
session = Session(profile_name="topdev_8888",region_name='ap-northeast-1')
polly = session.client("polly")
#print(polly)
# create audio data
try:
response = polly.synthesize_speech(
Engine = "standard",
LanguageCode = "ja-JP",
OutputFormat = "mp3",
Text = text,
VoiceId = "Mizuki")
return response
except (BotoCoreError, ClientError) as error:
# The service returned an error, exit gracefully
print("error2")
sys.exit(-1)
Пожалуйста, помогите! Заранее спасибо. [1]: https://docs.aws.amazon.com/polly/latest/dg/get-started-what-next.html