Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know total number of Frame in a file with cv2 in python

How to know total number of Frame in a file ( .avi) through Python using open cv module.

If possible what all the information (resolution, fps,duration,etc) we can get of a video file through this.

like image 961
Niraj Avatar asked Aug 18 '14 08:08

Niraj


People also ask

How do I count frames in OpenCV?

In OpenCV 3 the name of the frame count property is cv2. CAP_PROP_FRAME_COUNT while in OpenCV 2.4 the property is named cv2. cv. CV_CAP_PROP_FRAME_COUNT .


2 Answers

With a newer OpenCV version (I use 3.1.0) it works like this:

import cv2  cap = cv2.VideoCapture("video.mp4") length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) print( length ) 

And similar for other video properties cv2.CAP_PROP_*

like image 60
phev8 Avatar answered Oct 21 '22 13:10

phev8


import cv2  cap = cv2.VideoCapture(fn)  if not cap.isOpened():      print("could not open :",fn)     return      length = int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)) width  = int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)) fps    = cap.get(cv2.cv.CV_CAP_PROP_FPS) 

see here for more info.

also, all of it with a grain of salt, not all those props are mandatory, some might not be available with your capture / video codec

like image 35
berak Avatar answered Oct 21 '22 12:10

berak