# update: 2022-2-23
"""
image.shape: 图片尺寸 (height, width, number)
"""
import cv2 as cv
import time


def get_capture_once(capture_path):
    """获取capture"""

    # --- check ---
    cap = cv.VideoCapture(capture_path)
    if not cap.isOpened():
        return None

    # --- check ---
    fps = int(cap.get(cv.CAP_PROP_FPS))
    if not 4 < fps < 100:
        return None

    return cap


def get_capture(capture_path):
    """获取capture"""

    while True:

        # --- check ---
        cap = cv.VideoCapture(capture_path)
        if not cap.isOpened():
            print('m1: sleep 3s.')
            time.sleep(3)
            continue

        # --- check ---
        fps = int(cap.get(cv.CAP_PROP_FPS))
        if not 4 < fps < 100:
            print('m2: sleep 3s.')
            time.sleep(3)
            continue

        return cap


def rtsp_is_live(capture_path):
    """"""

    # --- check ---
    cap = cv.VideoCapture(capture_path)
    if not cap.isOpened():
        return False

    # --- check ---
    fps = int(cap.get(cv.CAP_PROP_FPS))
    if not 4 < fps < 100:
        return False

    return True


if __name__ == '__main__':
    print(get_capture('rtsp://admin:DEVdev123@192.168.30.235:554/h264/ch1/sub/av_stream'))
    print(rtsp_is_live('rtsp://admin:DEVdev123@192.168.30.235:554/h264/ch1/sub/av_stream'))