How to fetch the audio file from Kinesis Video stream created from Amazon connect in proper format?
H

0

6

I am trying to setup a VOICE MAIL feature for the callcenter in Amazon connect.I have added the Start Media streaming block to the flow.I have also added a Lambda function which is triggered by the Kinesis stream.My idea is NOT to do a Live streaming but to download the file once streaming is completed.

import json
import base64
import boto3
import time

def lambda_handler(event,context):
    # print(event)
    record = base64.b64decode(event["Records"][0]["kinesis"]["data"]).decode('utf-8')
    record_obj = json.loads(record)
    print('record', record)
    bucket = 'yyyy'
    key = 'streams/sample123.raw'
    # try:
    s3_client = boto3.client('s3',region_name='us-east-1')
    kinesis_client_1 = boto3.client('kinesisvideo',region_name='us-east-1')
    get_ep = kinesis_client_1.get_data_endpoint(StreamARN='arn:aws:kinesisvideo:us-east-1:237980099910:stream/xxxx/1580972532224',APIName='GET_MEDIA_FOR_FRAGMENT_LIST')
    t = get_ep['DataEndpoint']
    print(t)
    kinesis_client_2= boto3.client('kinesis-video-archived-media',region_name='us-east-1',endpoint_url=t)
    response = kinesis_client_2.list_fragments(StreamName='xxxx',
        # MaxResults=123,
        # NextToken='string',
        FragmentSelector={
            'FragmentSelectorType': 'SERVER_TIMESTAMP',
            'TimestampRange': {
                'StartTimestamp': '2020-02-07T05:21:30Z',
                'EndTimestamp': '2020-02-07T05:22:08Z'
            }
        }
        )

    fragments_list = res = [ sub['FragmentNumber'] for sub in response['Fragments'] ]
    print(fragments_list)

    stream = kinesis_client_2.get_media_for_fragment_list(
        StreamName='xxxx',
        Fragments=[
            'above list'
        ]
        )
    print(stream)
    stream_payload =stream['Payload'].read()
    s3_client.put_object(Bucket=bucket, Key=key, Body= stream_payload)

In lambda function , I try to get the audio file and download as .raw in s3 bucket. When I download that and import in audacity tool (16 bit signed PCM,8000 Hz,1 channel), the audio has lot of noise. Can someone tell me how to resolve this? I want to hear the clear audio and convert it into .wav file.Coding either in python or node.js.

Lambda trigger from kinesis:

{
    "AWSAccountId": "xxxx",
    "AWSContactTraceRecordFormatVersion": "2017-03-10",
    "Agent": null,
    "AgentConnectionAttempts": 0,
    "Attributes": {},
    "Channel": "VOICE",
    "ConnectedToSystemTimestamp": "2020-02-07T05:21:16Z",
    "ContactId": "7f8a125c-88d6-44e7-acde-xxxx",
    "CustomerEndpoint": {
        "Address": "+",
        "Type": "TELEPHONE_NUMBER"
    },
    "DisconnectTimestamp": "2020-02-07T05:22:08Z",
    "InitialContactId": null,
    "InitiationMethod": "INBOUND",
    "InitiationTimestamp": "2020-02-07T05:21:16Z",
    "InstanceARN": "arn",
    "LastUpdateTimestamp": "2020-02-07T05:23:15Z",
    "MediaStreams": [{
        "Type": "AUDIO"
    }],
    "NextContactId": null,
    "PreviousContactId": null,
    "Queue": {
        "ARN": "arn",
        "DequeueTimestamp": "2020-02-07T05:22:08Z",
        "Duration": 44,
        "EnqueueTimestamp": "2020-02-07T05:21:24Z",
        "Name": "General Queue"
    },
    "Recording": null,
    "Recordings": [{
        "DeletionReason": null,
        "FragmentStartNumber": "91343852333181432392682062626319863574829261800",
        "FragmentStopNumber": "91343852333181432759112314254792434826550738671",
        "Location": "arn:aws:kinesisvideo:us-east-1:237980099910:stream/xxxx/1580972532224",
        "MediaStreamType": "AUDIO",
        "ParticipantType": "CUSTOMER",
        "StartTimestamp": "2020-02-07T05:21:30Z",
        "Status": null,
        "StopTimestamp": "2020-02-07T05:22:08Z",
        "StorageType": "KINESIS_VIDEO_STREAM"
    }],
    "SystemEndpoint": {
        "Address": "+",
        "Type": "TELEPHONE_NUMBER"
    },
    "TransferCompletedTimestamp": null,
    "TransferredToEndpoint": null
} 

Hemistich answered 7/2, 2020 at 9:27 Comment(5)
Did u find the ans to your question?Churinga
Hey, how did you fix this ?Lontson
AWS has provided some JAVA code to solve this. Let me find that urlHemistich
Can you share that URL ?Shipway
@Hemistich did you find the url, can you share it pleaseLatria

© 2022 - 2024 — McMap. All rights reserved.