Ask Your Question

Revision history [back]

Help with detecting a wrong video header giving wrong frame amount

I have received a video from a research partner containing 1440 frames, but it has been cut with a cheap free video cutting tool from a larger video file. Result of this is that openCV actually thinks there are 15056 frames in the image when retrieving the amount of frames through the corresponding parameter retrieval.

This gives problems in the following situation:

VideoCapture input_video ("C:\\data\\video_test.avi");
Mat frame;
int frame_count = (int)input_video.get(CV_CAP_PROP_FRAME_COUNT);
for(int i = 0; i < frame_count; i++){
   input_video >> frame;
   imshow("frame", frame); 
   int key = waitKey(10);
   if (key == 27){
    break;
   }
}

It crashes the minute we go over the 1440 frames, because the next frame read is actually rubbish. Error that I retrieve when trying to read the unexisting 1441 frame is:

image description

However, when leaving out the visualization part, and switching the code of imshow to an imwrite(), all of a sudden, it continuous processing after frame 1440 and just outputs corrupted images until frame 15056.

I would like to know if anyone has a suggestion on how to capture the fact that this frame is corrupted, so that I can add a break statement in my for loop.