0

I have audio as bytes in the form of:

b'ID3\x04\x00\x00\x00\x00\x00#TSSE\x00\x00\x00\x0f\x00\x00\x03Lavf57.71.100\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\...

That I got from Amazon web services:

import boto3

client = boto3.client('polly')
response = client.synthesize_speech(
    Engine='neural',
    LanguageCode='en-GB',
    OutputFormat='mp3',
    SampleRate='8000',
    Text='hey whats up this is a test',
    VoiceId='Brian'
)

And I want to input it into moviepy audiofile using

AudioFileClip()

AudioFileClip takes filename or an array representing a sound. I know I can save the audio as a file and read it, but I would like to have AudioFileClip take the bytes output I showed above.

I tried:

AudioFileClip(response['AudioStream'].read())

But this gives the error:

TypeError: endswith first arg must be bytes or a tuple of bytes, not str

What can I do?

Qwertford
  • 1,039
  • 2
  • 14
  • 25

1 Answers1

-1

You need to convert the stream of audio to a different type. (Thats why its called TypeError). You are putting it as a string and it wants a byte format.

You can convert a str to a byte by using the bytearrayfunction! https://docs.python.org/3/library/functions.html#func-bytearray

You can also look at this question: Best way to convert string to bytes in Python 3?

For more help just comment on this anwser, and Ill try to help you as soon as possible.

Hope this can help you on your project,

PythonMasterLua

Brody Critchlow
  • 45
  • 3
  • 21