0

I am trying to use python to read audio from AWS Kinesis Video Stream.

I have followed the sample here

import boto3
client = boto3.client('kinesis-video-media')
response = client.get_media(
    StreamARN='string',
    StartSelector={
        'StartSelectorType': 'NOW'
    }
)

To which I get the response as

{
    'ContentType': 'string',
    'Payload': StreamingBody()
}

The StreamBody() is in bytes. If I do print(response) I get this

'Payload': <botocore.response.StreamingBody object at 0x0000027F1DAABCA0>

How do I decode the data which is in bytes? All I know data is supposed to be in MKV as noted here https://github.com/boto/boto3/issues/2415

Is there some way read or handle the data which is in MKV format?

The closest I got was this set of code

def read_in_chunks(file_object, chunk_size=1024):
    """Lazy function (generator) to read a file piece by piece.
    Default chunk size: 1k."""
    while True:
        data = file_object.read(chunk_size)
        if not data:
            break
        yield data


for piece in read_in_chunks(stream['Payload']):
    pya = pyaudio.PyAudio()
    stream = pya.open(format=pya.get_format_from_width(
        width=2), channels=2, rate=160000, output=True)
    stream.write(piece)
    stream.stop_stream()
    stream. Close()

The above code just gave blips of noise. I am assuming that is cause the input bytes are for MKV type of data. Any idea how I can play it via python or convert the stream to another format to play the audio?

The goal is to stream audio out from Amazon Connect service using Kinesis Video Stream. The provided solution is in JAVA and I do not understand JAVA strong enough to understand it completely.

Abilash Amarasekaran
  • 817
  • 2
  • 10
  • 26
  • MKV is a wrapper format. Such a file can contain metadata, multiple video streams, multiple audio streams, and other data and streams. You'll need decoders for both the MKV format and for the audio stream you wish to extract. (Note that asking where to get those decoders is out of scope for Stackoverflow.) – Ouroborus Oct 21 '22 at 21:17

0 Answers0