1

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!

Gingerbread
  • 1,938
  • 8
  • 22
  • 36
  • Are you sure that the file exists? You can try the full path, or `isOpened` function to check. – ilke444 Jan 13 '17 at 21:26
  • This may help... http://answers.opencv.org/question/55121/counting-the-total-number-of-frames-in-a-video-file/ – Mark Setchell Jan 13 '17 at 21:57
  • @ilke444: Yes, I entered the full path. I converted the video to AVI format but it still doesn't work!! – Gingerbread Jan 14 '17 at 07:37
  • @Gingerbread [this also could help](http://stackoverflow.com/questions/31472155/python-opencv-cv2-cv-cv-cap-prop-frame-count-get-wrong-numbers). It shows a workaround (and sometimes the function is only `cv2.CV_CAP`.. etc. Another difference: no parenthesis on print instruction. – marcoresk Jan 14 '17 at 11:27
  • @marcoresk: I tried this and it still hadn't given me the correct output. I figured it out now. Thanks for the help though! – Gingerbread Jan 15 '17 at 07:35
  • @MarkSetchell: Thanks Mark! I figured it out! – Gingerbread Jan 15 '17 at 07:35

1 Answers1

1

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.

saurabheights
  • 3,967
  • 2
  • 31
  • 50
  • FYI: Using a particular commit id in between releases is usually dangerous. Releases are called so, as they go through a good alpha/beta testing. So, try for version 3.2 or 2.4.13 – saurabheights Jan 14 '17 at 21:06
  • Thanks for pointing this out. I upgraded to 3.2 and in addition, I had set environment variables for my Anaconda to use the FFMPEG codec to process the videos. – Gingerbread Jan 15 '17 at 07:34