Getting the number of frames of an AVI video file for OpenCV
I’ve been using the OpenCV (ver. 1.0.0) lately and I noticed that under Linux, using the “cvCaptureFromAVI” does not detect the correct number of frames from an AVI video file. So this code returns 0 as the number of frames:
-
CvCapture *capture = cvCaptureFromAVI( path2Video );
-
cvQueryFrame( capture );
-
int nFrames = (int) cvGetCaptureProperty( capture , CV_CAP_PROP_FRAME_COUNT );
So I wrote a small piece of code which reads the number of frames directly from the AVI file. Please use it and let me know if you had any problem.
-
int nFrames;
-
char tempSize[4];
-
-
// Trying to open the video file
-
ifstream videoFile( path2Video , ios::in | ios::binary );
-
// Checking the availablity of the file
-
if ( !videoFile ) {
-
cout << “Couldn’t open the input file “ << path2Video << endl;
-
exit( 1 );
-
}
-
-
// get the number of frames
-
videoFile.seekg( 0×30 , ios::beg );
-
videoFile.read( tempSize , 4 );
-
nFrames = (unsigned char ) tempSize[0] + 0×100*(unsigned char ) tempSize[1] + 0×10000*(unsigned char ) tempSize[2] + 0×1000000*(unsigned char ) tempSize[3];
-
-
videoFile.close( );