Accepted answer

the get() for cap_prop_frame_count is never supposed to be accurate! if you check the opencv source code. you can find this:

int64_t cvcapture_ffmpeg::get_total_frames() const
    int64_t nbf = ic->streams[video_stream]->nb_frames;
    if (nbf == 0)
        nbf = (int64_t)floor(get_duration_sec() * get_fps() + 0.5);
    return nbf;

this means it will first look into the stream header for nb_frames, which you can check with ffprobe. if there is no such field, then there is no better way to get frame number than directly decoding the video. the opencv did a rough estimation by get_duration_sec() * get_fps() + 0.5 which surely not mean for accuracy.

thus, to obtain the correct frame number you have to decode and read through the entire stream, or you have to ask the video generator to generate correct stream header with nb_frames field.

video_capture = cv2.videocapture(video) video_length = int(video_capture.get(cv2.cap_prop_frame_count)) count = 0 while(true): # capture frame-by-frame ret, frame = video_capture.read() if not ret: break count += 1 print video_length, count # when everything done, release the capture video_capture.release() cv2.destroyallwindows()

on my machine it gives me:

$ ./openstack_video_frames.py 
1232 1232
                                            

cv_cap_prop_frame_count gives the property of 'number of frames', that comes from the video header. the other number is basically "how many frames can i read from this video file?".

if the video contains frames that cannot be read/decoded (e.g., due to being corrupted), opencv skips those frames (after trying to read them) and gives you the next valid frame. so the difference between your two numbers is the number of frames that couldn't be read.

also, if your video header is corrupted and/or cannot be parsed by the underlying codecs that opencv uses, then those numbers can be different too.

  • python opencv cv2.cv.CV_CAP_PROP_FRAME_COUNT get wrong numbers
  • How to get previous frame of a video in opencv python
  • How to get video frame by frame from stream using openCV and python
  • get frame from video with CV_CAP_PROP_POS_FRAMES in opencv python
  • How to know total number of Frame in a file with cv2 in python
  • How to process images of a video, frame by frame, in video streaming using OpenCV and Python
  • Python OpenCV cv2 drawing rectangle with text
  • How to get the latest frame from capture device (camera) in opencv
  • Sending live video frame over network in python opencv
  • How to get pixel coordinates from Feature Matching in OpenCV Python
  • Python OpenCv cv2 equivalent for CV_FILLED
  • Python Opencv SolvePnP yields wrong translation vector
  • Get RGB value opencv python
  • How to get frame from video by its index via OpenCV and Python?
  • How to update imshow() window for Python OpenCV CV2
  • Stream OpenCV frame to HTML in Python
  • Unable to get double click event in OpenCV for python
  • I get a error when using HoughCircles with Python OpenCV that a module is missing
  • count number of black pixels in an image in Python with OpenCV
  • How to get image from video using opencv python
  • Get UnsatisfiableError when Installing OpenCV for Python through Anaconda on Windows
  • How to get the OpenCV image from Python and use it in C++ in pybind11?
  • How to process video files with python OpenCV faster than file frame rate?
  • python opencv cv2 matchTemplate with transparency
  • Get frame to Hikvision IP Camera in python
  • Get frame from video with libvlc smem and convert it to opencv Mat. (c++)
  • OpenCV Python count pixels
  • Getting current frame with OpenCV VideoCapture in Python
  • OpenCV Python - No module named cv2 (again)
  • Wrong python packages path for opencv cmake installation
  • More Query from same tag

  • Convert Image to CMYK and split the channels in OpenCV
  • Output two distinct images to two different Video outputs display using OpenCV
  • How to fix error LNK2019 when using videoInput.lib with OpenCv2.1?
  • Why is Loading from an XML File in OpenCV Corrupting my Data?
  • How do I type-hint OpenCV images in Python?
  • viz_utils.visualize_boxes_and_labels_on_image_array get category
  • Debug Assertion Failed! when I add opencv headers in VC++ windows form application
  • Cmake can not find local version of opencv on ubuntu 18.04
  • Green screen / chroma key iOS
  • Emgu CV video playing is slow?
  • How to get the contours of colored objects in a image
  • nvcc : fatal error : Option '--cubin (-cubin)' is not allowed when compiling for multiple GPU code instances
  • list file extensions supported by OpenCV
  • bmpFactoryOptions don't work, no idea why
  • Android OpenCV - How to process camera frames from background service?
  • Converting MatOfByte to Base64 string
  • Why does my python script crash every time I run OpenCV function?
  • How to subscribe and publish images in ROS
  • Python opencv returns Bad Properties
  • Opencv detect all generic objects shapes on a table (birdview)
  • Find coordinates of the lines detected by the Canny edge detector
  • Approximating true heightmap gradient magnitude with opencv's Sobel filter
  • Given two images of same object from two views.How to decide which one is left view and which oen is right view
  • Count connected components in binary image using openCV
  • Mat modified in JNI does not reflect changes back in Java
  • How to automatically adjust the threshold for template matching with opencv?
  • Pass and return OpenCv Mat object with JNI
  • Field of view of a GoPro camera
  • How to find out the topmost coordinate point where the pixel value got changed in an image?
  • cv2 triangulatePoints always returns same Z value
  •