OpenCV reads the video and obtains relevant attribute information

Read the video using VideoCapture:

video= cv2.VideoCapture(r'./prototype.mp4')

Use the following code to determine whether the video is read successfully

is_open= video.isOpened()

After reading successfully, use VideoCapture.get(cv2.prop) to get various information, see here for all information .

CAP_PROP_FRAME_WIDTH : Video width

CAP_PROP_FRAME_HEIGHT : video length

CAP_PROP_FPS : Video frame rate

CAP_PROP_FRAME_COUNT : The total number of video frames

 The overall example is as follows:

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))

output:

(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 

 

Document here

Guess you like

Origin blog.csdn.net/neowell/article/details/117690943