opencv-python version:4.4.0.46
代码如下:
import cv2
# 通过cv2中的类获取视频流操作对象cap
cap = cv2.VideoCapture('rtsp://admin:[email protected]:554') #此为摄像头URL,可以替换为本地视频路径;若设置为0(表示调用本地的摄像头)
# 调用cv2方法获取cap的视频帧
fps = cap.get(cv2.CAP_PROP_FPS)
# 获取cap视频流的每帧大小
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
# 定义编码格式
fourcc = cv2.VideoWriter_fourcc('P', 'I', 'M', '1')
# 定义视频文件输入对象
outVideo = cv2.VideoWriter('saveDir.wmv', fourcc, fps, size)
# 循环使用cv2的read()方法读取视频帧
tot = 1
while cap.isOpened():
rval, frame = cap.read()
tot += 1
print('tot=', tot)
outVideo.write(frame)
cv2.waitKey(1)
cap.release()
outVideo.release()
cv2.destroyAllWindows()