I am working on processing real-time audio from Amazon Connect by retrieving fragments from Kinesis Video Streams and saving them to S3. However, the MKV file I export to S3 is not playable and seems to be corrupted.
import boto3
import os
s3_bucket = 'BUCKET_NAME'
def lambda_handler(event, context):
MediaStreams = event['Details']['ContactData']['MediaStreams']
CustomerAudio = MediaStreams['Customer']['Audio']
StartFragmentNumber = CustomerAudio['StartFragmentNumber']
StreamARN = CustomerAudio['StreamARN']
kvs_client = boto3.client("kinesisvideo", region_name="eu-central-1")
endpoint = kvs_client.get_data_endpoint(
StreamARN=StreamARN,
APIName="GET_MEDIA"
)["DataEndpoint"]
media_client = boto3.client("kinesis-video-media", endpoint_url=endpoint, region_name="eu-central-1")
response = media_client.get_media(
StreamARN=StreamARN,
StartSelector={
"StartSelectorType": "FRAGMENT_NUMBER",
"AfterFragmentNumber": StartFragmentNumber,
}
)
process_audio(response["Payload"])
def process_audio(payload):
s3_client = boto3.client('s3')
s3_key = 'stream.mkv'
temp_audio_path = '/tmp/stream.mkv'
with open(temp_audio_path, 'wb') as f:
f.write(payload.read())
s3_client.upload_file(temp_audio_path, s3_bucket, s3_key)
os.remove(temp_audio_path)
return {
'statusCode': 200,
'body': f'Audio file saved to s3://{s3_bucket}/{s3_key}'
}
The stream.mkv file saved in S3 is not playable. It seems corrupt, and standard media players cannot open it. I suspect that the data stream is not being correctly read or written.
How can I correctly parse and save these real-time media fragments?