参考资料:
视频处理VideoCapture类---OpenCV-Python开发指南(38)_python opencv videocapture_李元静的博客-CSDN博客
OpenCV VideoCapture.get()参数详解 - 简书
FOURCC四字符码对照表_4fvcc_Kellybook的博客-CSDN博客
import cv2 as cv
import numpy as np
#1.创建VideoCapture对象
# video = cv.VideoCapture(filePath)
# filePath: 视频文件路径
# 参考资料:https://blog.csdn.net/liyuanjinglyj/article/details/114374223
video = cv.VideoCapture("../SampleVideos/Unity2D.mp4")
print("Video Opened?", video.isOpened())
#2.获得视频属性
# video.get(propId)
# propId: 属性ID
# cv.CAP_PROP_POS_MSEC - 视频文件的当前位置(ms)
# cv.CAP_PROP_POS_FRAMES - 当前帧序号(从0开始)
# cv.CAP_PROP_POS_AVI_RATIO - 视频文件相对位置(0表示开始,1表示结束)
# cv.CAP_PROP_FRAME_WIDTH - 视频的帧宽度
# cv.CAP_PROP_FRAME_HEIGHT - 视频的帧高度
# cv.CAP_PROP_FPS - 帧率
# cv.CAP_PROP_FOURCC - 视频编码的四字节编码
# cv.CAP_PROP_FRAME_COUNT - 视频文件的帧的数量
# cv.CAP_PROP_FORMAT - 对象的格式
# 参考资料:https://www.jianshu.com/p/676bef32e655
# https://blog.csdn.net/qq_30622831/article/details/82082122
width = video.get(cv.CAP_PROP_FRAME_WIDTH)
height = video.get(cv.CAP_PROP_FRAME_HEIGHT)
fps = video.get(cv.CAP_PROP_FPS)
fourcc = int(video.get(cv.CAP_PROP_FOURCC))
totalFrames = video.get(cv.CAP_PROP_FRAME_COUNT)
print("Video Properties: resolution - (", width, height, ") FPS - "
, fps, " FOURCC - "
, chr(fourcc&0xFF), chr((fourcc>>8)&0xFF), chr((fourcc>>16)&0xFF),chr((fourcc>>24)&0xFF)
, " Frame Count - ", totalFrames)
#3. 读取视频的一帧图像
# ret,frame = video.read()
# read()按帧读取视频,ret,frame是获cap.read()方法的两个返回值
# 其中ret读取帧是正确的话则返回True,如果文件读取到结尾,它的返回值就为False
frame_delay = int(1000 / fps)
while True:
ret,frame = video.read()
cv.imshow('Opencv Video Play', frame)
key = cv.waitKey(frame_delay)
if key == 27: #检测ESC键
break
#4. 释放视频对象
video.release()
cv.destroyAllWindows()