Gingerbread
Gingerbread

Reputation: 2122

OpenCV's frame count always printing 0

I am new to OpenCV and after reading a lot of posts, I figured out how to get the number of frames in a video by using the following code. All my videos are about 30 seconds long each. However, OpenCV's frame count method always returns 0. I am not sure why. My video type is .mp4 . Can someone help me with this?

I tried using the same code for different length videos. In all these cases, it printed 0 as the frame count.

My OpenCV version is 2.4.11

Here is the code snippet.

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

Thank you!

Upvotes: 1

Views: 1791

Answers (1)

saurabheights
saurabheights

Reputation: 4564

I faced the same dilemma in version 2.4.11(when dealing with total frame count) and upgraded to commit id 0e436c3fe9e9c30c23a54449be44b2618aec1cb1, the head at the time - 24 June 2016. You can also try updated version of opencv2 - 2.4.13, it should have been fixed since this was released 29 days ago, but I haven't tried it. Check all releases here.

Note:- OpenCV3 introduces some design changes which might make your old code break. Some examples: - Highgui is now broken to VideoIO and ImgCodecs. Also, Core.rectangle like functions has moved to ImgProc. These changes, though, in my opinion, makes OpenCV more intuitive and easy to use.

** In general, **

This issue often has three possible causes:

  1. The underlying video decoder library being used by OpenCV. In most cases, its FFmpeg, unless you have overridden it while compiling OpenCV.

  2. Any corruption in the file, so try with few other files(captured from different cameras).

  3. Finally, the third issue could be a bug in OpenCV.

Upvotes: 1

Related Questions