Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

For me its working (remember videos are 0-based indexed):

using namespace std;
using namespace cv;


int main() {
    VideoCapture cap("test.mp4");
    if( !cap.isOpened()){
        cout << "Cannot open the video file" << endl;
        return -1;
    }

int count = (int) cap.get(CV_CAP_PROP_FRAME_COUNT); //get total frame count
cout<< "Max frames: "<< count << endl;

Mat frame;
int index = 0;
while(cap.read(frame))
{
    cout << "the current frame: " << index << endl;
    index++;
}
cout << "exit first loop" << endl;
cap.set(CV_CAP_PROP_POS_FRAMES, 0); //Set index to 0 (start frame)
int index2 = 0;
while(cap.read(frame))
{
    cout << "the current frame: " << index2 << endl;
    index2++;
}
cout << "exit second loop" << endl;
cout << "index1: "<< index << " / index2: "<< index2 <<endl;
}

Output (see attached mp4 file C:\fakepath\test.mp4.jpg for own test, rename it to .mp4 ;-) ):

Max frames: 145
..
index1: 145 / index2: 145