opencv 打开和保存视屏(python版)

opencv 打开和保存视屏(python版)

打开视屏,并播放视屏

import cv2

# 获取video_path的视屏
cap = cv2.VideoCapture("video_path")
while True:
    # 一帧一帧的获取图像
    ret,frame = cap.read()
    if ret == True:
        # 显示结果帧
        cv2.imshow("frame", frame) 
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break
# 释放视屏资源
cap.release()
cv2.destroyAllWindows()

打开视屏,并另保存视屏(可实现对视屏的其他操作)

import cv2

# 获取video_path的视屏
cap = cv2.VideoCapture("video_path")
fourcc = cv2.VideoWriter_fourcc("M", "P", "4", "2")  # 编码格式:MPEG-4.2 编码占内存最小
cap_fps = cap.get(cv2.CAP_PROP_FPS)  # 获取读取视屏的帧率
cap_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)  # 获取读取视屏的宽度
cap_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))  # 获取读取视屏的高度
# Video_save_path.avi:保存视屏地址
out = cv2.VideoWriter(r'Video_save_path.avi', fourcc, cap_fps, (cap_width, cap_height), True)
while True:
    # 一帧一帧的获取图像
    ret,frame = cap.read()
    if ret == True:
        # 写入帧
        out.write(frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break
# 释放视屏资源
cap.release()
out.release()
cv2.destroyAllWindows()

你可能感兴趣的:(opencv)