通过下代码确定视频是否读取成功
is_open= video.isOpened()
读取成功后,通过VideoCapture.get(cv2.prop)来获取各种信息,全部信息
见此
。
CAP_PROP_FRAME_WIDTH :视频宽
CAP_PROP_FRAME_HEIGHT :视频长
CAP_PROP_FPS :视频帧率
CAP_PROP_FRAME_COUNT :视频总帧数
整体示例如下:
import cv2
#打开视频
video= cv2.VideoCapture(r'./prototype.mp4')
#读取是否成功
open_not = video.isOpened()
#视频的宽高(分辨率)
video_width= video.get(cv2.CAP_PROP_FRAME_WIDTH)
video_height= video.get(cv2.CAP_PROP_FRAME_HEIGHT)
#视频总的帧数
total_frame= video.get(cv2.CAP_PROP_FRAME_COUNT)
#视频的帧率
fps= video.get(cv2.CAP_PROP_FPS)
#视频时长就是总帧数除以帧率,以秒为单位
total_time= total_frame/fps
print(' video open is {on}\n resolution:{w}x{h}\n total_frame:{af}\n fps:{f}\n total_time:{vt}\n'.format(on=open_not,w=video_width,h=video_height,af=total_frame, f=fps, vt=total_time))
(pytorch) C:\Downloads>python getVideoInfo.py
video open is True
resolution:1920.0x1080.0
total_frame:4284.0
fps:30.0
total_time:142.8
文档
在此
cameraCapture = cv2.
Video
Capture('./res/test.mp4')
print(cameraCapture.get(cv2.CAP_PROP_POS_MSEC))
print(cameraCapture.get(cv2.CAP_PROP_POS_FRAMES))
print(cameraCapt...
capture.set(CAP_PROP_FRAME_WIDTH, SRC_WIDTH); //设置宽度
capture.set(CAP_PROP_FRAME_HEIGHT, SRC_HEIGHT); //设置长度
capture = cv2.
Video
Capture("mapping.mp4")
# 得到
视频
的高度
height = capture.get(cv2.CAP_PROP_FRAME_HEIGHT)
# 得到
视频
的宽度
width = capture.get(cv2.CAP_PROP_FRAME_WIDTH)
# 得到
视频
的帧数
count = capture.get(cv2.CAP_PROP_FRAME_COUNT)
# 得到
视频
的帧速
fps = cap