Get frame count from AVI

Here’s a little code snippet I wrote to get the number of frames in an AVI video file.

int getFrameCount(const char *filename) {
    // only works on AVIs
    int frameCount;
    char size[4];
    ifstream fin(filename, ios::in|ios::binary);
    if(!fin) {
        cerr << "Could not open " << filename << endl;
    fin.seekg(0x30, ios::beg); // number of frames is stored at this location, 4);
    frameCount = size[0] | size[1]<<8 | size[2]<<16 | size[3]<<24;
    return frameCount;

Use it however you like. I’m using it because OpenCV’s cvGetCaptureProperty doesn’t seem to work.

Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *