将HLS段文件读取到OpenCV VideoCapture时出现问题:读取标题错误

时间:2019-01-10 13:28:27

标签: python opencv hls

使用cv2.VideoCapture()

将HLS(HTTP实时流)段文件读取到OpenCV时出现以下错误

    [mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f941f2ca200] could not find corresponding trex
    [mov,mp4,m4a,3gp,3g2,mj2 @ 0x7f941f2ca200] error reading header

段文件是mp4,HLS服务器是Amazon Kinesis Video Stream。错误消息说我缺少mp4的标头部分,并且看起来有些合理,因为HLS提供了一系列文件。

我的问题是,将HLS段读取到OpenCV是完全错误的方法吗?还是我们有一些解决方法?


    import boto3
    import cv2
    import requests
    import re

    kinesis_client = boto3.client('kinesisvideo',
                                  region_name='ap-northeast-1'
                                  )

    endpoint = kinesis_client.get_data_endpoint(
        StreamARN='MY_ARN',
        APIName='GET_HLS_STREAMING_SESSION_URL'
    )

    data_endpoint = endpoint['DataEndpoint']
    url_prefix = data_endpoint + "/hls/v1/"

    video_client = boto3.client('kinesis-video-archived-media',
                                endpoint_url=data_endpoint
                                )

    # Retrieve HLS manifest URL from Kinesis Video Stream
    session_url = video_client.get_hls_streaming_session_url(
        StreamARN='MY_ARN',
        HLSFragmentSelector={'FragmentSelectorType': 'PRODUCER_TIMESTAMP'}
    )

    session_url = session_url['HLSStreamingSessionURL']

    # Fetch segment(mp4) file urls
    master_playlist = requests.get(session_url)
    media_playlist_url = url_prefix + master_playlist.text.split("\n")[-2]
    media_playlist = requests.get(media_playlist_url)
    media_playlist = media_playlist.text
    pattern = r"getMP4Media"
    segments  = filter(lambda x: re.match(pattern,x), media_playlist.split("\n"))

    for segment in segments:
      segment_url = url_prefix + segment

      # Debug segment url
      # This emits something like: https://b-87178fb5.kinesisvideo.ap-northeast-1.amazonaws.com/hls/v1/getMP4MediaFragment.mp4?FragmentNumber=91343852333186478711651164912799448912305946338&SessionToken=CiAFReSdi9NKKz8vDum-Bs_8MFJ-5jIk06WmiyKDQQazihIQLdLPLuvr6scwc4HhAo6uDRoZWbgLHyJK01pTzFujc-cSkfl0oo4pIFD2sSIghdfInJL4XqMc_brBoLCikS73I3Nxxxxxxxxxxx
      print(segment_url)


      # Read mp4 file from Kinesis Video Sterams
      cap = cv2.VideoCapture(segment_url)

      while(cap.isOpened()):
        ret, frame = cap.read()
        cv2.imshow("Frame",frame)

      cap.release()

0 个答案:

没有答案