Ask Your Question
2

Counting the Total number of Frames in a video file ?

asked 2015-02-13 00:48:27 -0600

aj_nikhil gravatar image

updated 2015-02-13 00:52:04 -0600

I am trying to count total number of Frames in my video file ('foo.h264').

>>> import numpy as nm
>>> import cv2
>>> cap = cv2.VideoCapture('foo.h264')
>>> cap.get(CV_CAP_PROP_FRAME_COUNT)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'CV_CAP_PROP_FRAME_COUNT' is not defined
>>> cap.get(5)
25.0
>>> cap.get(7)
-192153584101141.0

So I think get(5) is giving frame rate and get(7) gives total number of frames. Obviously get(7) is incorrect in the above case. So I tried to find these values in an .avi file.

>>> cap = cv2.VideoCapture('foo.avi')
>>> cap.get(5)
29.97002997002997
>>> cap.get(7)
256379.0

I can calculate the total number of frames by multiplying FPS by duration of the video, but I'm not sure if the FPS given for .h264 is right or not. Why does it give negative number of total frames? Is this a bug?
P.S: I recorded this video file(.h264) using raspberry pi camera.

edit retag flag offensive close merge delete

Comments

I think your problem is that 'CV_CAP_PROP_FRAME_COUNT' is not defined... Are you using OpenCV 3? And I think your resolution is here

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-02-13 02:22:41 -0600 )edit

3 answers

Sort by » oldest newest most voted
3

answered 2015-02-13 03:53:56 -0600

updated 2018-08-27 02:43:21 -0600

Okay first of all the cap interface isn't the best at retrieving properties. Many of it underlying interfaces for camera capture don't support more than half of the get and set parameters. Getting the total amount of frames will be 100% correctly if you just calculated it manually. If this suits your needs and time is not an issue, then simply go for that approach.

A quick code sample

VideoCapture interface("location_to_file");
int counter = 0;
Mat frame;
while(cap >> frame){
   if(frame.empty()){
      break;
   }
   counter++
}

Update:

Since the introduction of this solution, there was a new videocapture property introduced, called CAP_PROP_FRAME_COUNT, which can be retrieved by invoking the .get() function on the VideoCapture element.

edit flag offensive delete link more
2

answered 2015-02-13 06:02:41 -0600

aj_nikhil gravatar image

So .h264 video that I have is NAL stream and doesn't have the information that's why maybe OpenCv is not able to return it. One method is to wrap .h264 in a container like .mp4 and then count the number of frames of the wrapped video. get(7) returns the total frame in that case. I verified both ways to calculate the total number of frames. Both match.

edit flag offensive delete link more
-1

answered 2018-08-25 19:07:59 -0600

stiv-yakovenko gravatar image

Going through each frame with VideoCapture might be slow. You can get 3x perfomance boost if you invoke ffprobe.exe, assuming its in current folder:

#include <iomanip>
#include <string>
#include <iostream>
using namespace std;

string exec(string cmd) {
    array<char, 128> buffer;
    string result;
    shared_ptr<FILE> pipe(_popen(cmd.c_str(), "r"), _pclose);
    if (!pipe) throw std::runtime_error("popen() failed!");
    while (!feof(pipe.get())) {
        if (fgets(buffer.data(), 128, pipe.get()) != nullptr)
            result += buffer.data();
    }
    return result;
}
int measureFile(string fileName) {
    string cmd = "ffprobe -v error -count_frames -select_streams v:0 -show_entries stream=nb_read_frames -of default=nokey=1:noprint_wrappers=1 ";
    auto lnch = cmd + fileName;
    cout << lnch << endl;
    string res = exec(lnch);
    try {
        return stoi(res);
    }
    catch (...) {
        return -1;
    }
}

ffprobe.exe is standalone file, you can download it here: https://ffbinaries.com/downloads

I think this will also work on linux, but you need to replace _popen->popen and _pclose->pclose.

edit flag offensive delete link more

Comments

I updated my solution with a faster approach. Calling an external library seems a complete overkill to me ...

StevenPuttemans gravatar imageStevenPuttemans ( 2018-08-27 02:43:59 -0600 )edit

@StevePuttemans, CAP_PROP_FRAME_COUNT is unreliable, for some files it gives huge negative value and iterating frames is slow, so using ffprobe is the only user-friendly option.

stiv-yakovenko gravatar imagestiv-yakovenko ( 2018-09-05 00:20:47 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-02-13 00:48:27 -0600

Seen: 23,659 times

Last updated: Aug 27 '18