opencv打开摄像头并保存为本地mp4

发布时间 2023-04-17 15:44:01作者: bitterteaer
import cv2

# 打开摄像头
url = "rtsp://admin:qwer1234!.@192.168.1.65:554"

cap = cv2.VideoCapture(url)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

fourcc = cv2.VideoWriter_fourcc(*"mp4v")
out = cv2.VideoWriter('test.mp4', fourcc, 20, (width, height))

while True:
    ret, frame = cap.read()
    if ret:
        out.write(frame)
        cv2.imshow('capture', frame)
        if cv2.waitKey(25) & 0xFF == ord('q'):  # 按键盘Q键退出
            break
    else:
        continue
cap.release()
out.release()
cv2.destroyAllWindows()