카테고리 없음

AWS Kinesis Video Stream Viewer(Consumer) 만들기

Wood Pecker 2021. 7. 27. 15:06

1. 개요

Raspberry Pi에서 비디오 스트림을 보내고 이를 Web을 통하여 스트림을 플레이하여 보자.

 

 

2. 웹브라우저에서 viewer 만들기(Consumer)

GetHLSStreamingSessionURL은 AWS에서 제공하는 API로 HTTP Live Streaming (HLS) 을 받아올 수 있다.

HTML(Javascript) 버전의 프로그램 :
https://aws-samples.github.io/amazon-kinesis-video-streams-media-viewer
(소스코드: https://github.com/aws-samples/amazon-kinesis-video-streams-media-viewer)

 

 

3. 파이썬 뷰어 만들기(Consumer)

import boto3
import cv2

STREAM_NAME = "testStream"
AWS_REGION='us-east-1'

def aws_hls_stream():
    kv_client = boto3.client("kinesisvideo", aws_access_key_id='Access_key_id', aws_secret_access_key='Secret_access_key', region_name='us-east-1')
    endpoint = kv_client.get_data_endpoint(
        StreamName=STREAM_NAME,
        APIName="GET_HLS_STREAMING_SESSION_URL"
    )['DataEndpoint']
    print(endpoint)

    ## Grab the HLS Stream URL from the endpoint
    kvam_client = boto3.client("kinesis-video-archived-media",
                               aws_access_key_id='Access_key_id',
                               aws_secret_access_key='Secret_access_key',
                               region_name=AWS_REGION,
                               endpoint_url=endpoint)
    url = kvam_client.get_hls_streaming_session_url(
        StreamName=STREAM_NAME,
        PlaybackMode="LIVE"
    )['HLSStreamingSessionURL']

    vcap = cv2.VideoCapture(url)

    while(True):
        # Capture frame-by-frame
        ret, frame = vcap.read()
        if frame is not None:
            # Display the resulting frame
            cv2.imshow('frame', frame)
            # Press q to close the video windows before it ends if you want
            if cv2.waitKey(22) & 0xFF == ord('q'):
                break
        else:
            print("Frame is None")
            break

    # When everything done, release the capture
    vcap.release()
    cv2.destroyAllWindows()
    print("Video stop")

if __name__ == '__main__':
    aws_hls_stream()
반응형