Amir Saffari’s Website

Getting the number of frames of an AVI video file for OpenCV

by Amir on Jun.04, 2007, under Computer Vision

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:

  1. CvCapture  *capture = cvCaptureFromAVI( path2Video );
  2. cvQueryFrame( capture );
  3. 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.

  1. int  nFrames;
  2. char tempSize[4];
  3.  
  4. // Trying to open the video file
  5. ifstream  videoFile( path2Video , ios::in | ios::binary );
  6. // Checking the availablity of the file
  7. if ( !videoFile ) {
  8. cout << “Couldn’t open the input file “ << path2Video << endl;
  9. exit( 1 );
  10. }
  11.  
  12. // get the number of frames
  13. videoFile.seekg( 0×30 , ios::beg );
  14. videoFile.read( tempSize , 4 );
  15. nFrames = (unsigned char ) tempSize[0] + 0×100*(unsigned char ) tempSize[1] + 0×10000*(unsigned char ) tempSize[2] +    0×1000000*(unsigned char ) tempSize[3];
  16.  
  17. videoFile.close(  );
11 comments for this entry:
  1. joewan

    Hi!I’m chinese.Nice to know you,I am eager to make friends with you and know you more!
    I also use opencv lately,and I find your blog when I search “opencv+get the number of frames in an avi” in google.
    I use a loop to know the number of frames in an AVI,
    for(int i= 0; i<1000; i++){
    if(cvGrabFrame(capture))
    printf(”frame%d\n”,i);
    cvRetrieveFrame(capture);
    }
    Do you have another better way to implement it.
    I hope that you can email me!
    thanks!

  2. joewan

    int numFrames = (int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_COUNT)
    it does not detect the correct number of frames from an AVI video file.
    it is one less frame.

    “for loop” gets 223 frames,but CV_CAP_PROP_FRAME_COUNT only gets 222!

  3. Amir

    Well Hi,

    As I said, since the OpenCV doesn’t give you the correct number of frames, you can just use my code to read the correct one from the AVI file directly. Just replace the path2Video with the path to your video file. For using it, you don’t need the OpenCV for it, just copy and paste the code into your own C++ program and compile it. Hope this would solve your problem.

    Amir

  4. luke

    Hi!

    Sorry, but your code doesn’t work. For an avi File with 74 seconds your functions returns me over 7000000 Frames. Can you explain me why you multiply the the values with 100^x ?
    Can you also explain me how such an avi File looks internally?

    Thank you.

    Luke

  5. Amir

    Hi Luke,

    I don’t know exactly why it doesn’t work on your AVI file, however, if you could send me your AVI file, I might be able to see what’s going wrong.

    Regarding the AVI file format, you can check this documentation:
    “http://msdn.microsoft.com/en-us/library/ms779631(VS.85).aspx”

    From there if you go to AVI RIFF File Reference , and from there to AVIMAINHEADER , you can see that total number of frames are specified as “DWORD dwTotalFrames” in the header of an AVI file. And the last time I calculated it, the address of it in the header section of the AVI file was in 0×30 . So what my code is doing is that it simply jumps there and reads 4 bytes (DWORD) and converts it to an integer.

    Again if you found out why my code is not able to get the number of frames right out of your AVI, please let me know or simply write a comment there so it would be useful for other people too.

    Cheers
    Amir

  6. Luke

    Hi!

    I tried and found another easy way to get the number of frames being based on your solution. I’ve tested it on 3 files and I always got the right frame number.

    int nFrames;
    char tempSize[4];

    ifstream videoFile(filename, ios::in|ios::binary);
    videoFile.seekg(0×30, ios::beg);

    videoFile.read(tempSize, 4);

    videoFile.close();
    nFrames = tmpSize[0] + 256 * tempSize[1] + 256^2 * tempSize[2] + 256^3 * tempSize[3];

    Thank you for your inspiration!

    Cheers,

    Luke

  7. Amir

    Hi Luke,

    Thanks for your feedback, the only difference that I see between your code and mine is the unsinged char casting which I have, as 0×100 = 256, 0×10000 = 256^2, etc. So that might be the source of the problem there, I will try to take a look into it.

    Cheers
    Amir

  8. Florin

    Hi,

    I am Florin and I am from Romania.

    I have a problem when I use cvCaptureFromAVI function. The return value is NULL. I have installed ffmpeg codec (my OS is Linux).

    Do you know what else do I need to capture from a video file (AVI) ?

    Thanks a lot !

  9. Amir

    Hi Florin,

    May be you need to check if the filename/path is correct.

    Cheers

  10. Youssef

    Hi Amir,

    Thanks for the code, it helped me get the video running, although the number of frames seems to be a bit larger than what it’s supposed to be. It looks like it’s double the frame count.

    But I wanted to ask you about something slightly different. I need to get other info out of the file header. Namely the width and height. In the AVI Ref you mentioned it says at what order they come in and that they’re DWords. I tried calculating the position from which to read these values from the stream but I’m not getting the expected results.
    Could you please tell me how you got to 0×30?

    Thanks

  11. Amir

    Hi Youssef, what I did was to open a few AVI files (small ones) in a hex editor and try to find out the location of the information in the header.Probably you can do the same and check sequentially the header information and see where the height and width size is stored.

    Cheers
    Amir

Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...