Retrieving the current frame number in OpenCV

In openCV version 3.4, the correct flag is:

cap.get(cv2.CAP_PROP_POS_FRAMES)

The way of doing it in OpenCV python is like this:

import cv2
cam = cv2.VideoCapture(<filename>);
print cam.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)

You can use the "get" method of your capture object like below :

capture.get(CV_CAP_PROP_POS_FRAMES); // retrieves the current frame number

and also :

capture.get(CV_CAP_PROP_FRAME_COUNT); // returns the number of total frames 

Btw, these methods return a double value.

You can also use cvGetCaptureProperty method (if you use old C interface).

cvGetCaptureProperty(CvCapture* capture,int property_id);

property_id options are below with definitions:

CV_CAP_PROP_POS_MSEC 0

CV_CAP_PROP_POS_FRAME 1

CV_CAP_PROP_POS_AVI_RATIO 2

CV_CAP_PROP_FRAME_WIDTH 3

CV_CAP_PROP_FRAME_HEIGHT 4

CV_CAP_PROP_FPS 5

CV_CAP_PROP_FOURCC 6

CV_CAP_PROP_FRAME_COUNT 7

  • POS_MSEC is the current position in a video file, measured in milliseconds.
  • POS_FRAME is the position of current frame in video (like 55th frame of video).
  • POS_AVI_RATIO is the current position given as a number between 0 and 1 (this is actually quite useful when you want to position a trackbar to allow folks to navigate around your video).
  • FRAME_WIDTH and FRAME_HEIGHT are the dimensions of the individual frames of the video to be read (or to be captured at the camera’s current settings).
  • FPS is specific to video files and indicates the number of frames per second at which the video was captured. You will need to know this if you want to play back your video and have it come out at the right speed.
  • FOURCC is the four-character code for the compression codec to be used for the video you are currently reading.
  • FRAME_COUNT should be the total number of frames in video, but this figure is not entirely reliable.

(from Learning OpenCV book )