Failing to read a frame with VideoCapture when reaching the end of a video takes several seconds (Bug #1893)


Added by Julien Hirel almost 13 years ago. Updated over 12 years ago.


Status:Done Start date:2012-05-04
Priority:Normal Due date:
Assignee:- % Done:

0%

Category:highgui-video
Target version:2.4.3
Affected version: Operating System:
Difficulty: HW Platform:
Pull request:

Description

Hi

I'm trying to read a video in a loop using the VideoCapture object. I have created a simple program that works correctly except that the last read, when the end of the video has been reached, takes several seconds to fail.

I can solve this problem by comparing the current frame number and the total number of frames in the video (and bypass the last call to read), but trying to read frames until the method returns false seems to be the correct way to test for the end of the video (and should probably not take so long).

Here is my code:

 1int main(int ac, char *av[])
 2{
 3   if (ac <= 1)
 4   {
 5      std::cerr << "The name of the video file must be supplied as a parameter" << std::endl;
 6      exit(1);
 7   }
 8
 9   cv::VideoCapture video;
10
11   if (video.open(av[1]) == false)
12   {
13      std::cerr << "The video file (" << av[1] << ")could not be opened" << std::endl;
14      exit(1);
15   }
16
17   cv::namedWindow("Display");
18
19   cv::Mat videoFrame;
20
21   while(1)
22   {
23      std::cout << "Rewinding video" << std::endl;
24
25      // Rewind video
26      if (video.set(CV_CAP_PROP_POS_FRAMES, 0) == false)
27      {
28         std::cerr << "The video could not be rewinded" << std::endl;
29         exit(1);
30      }
31
32      while (video.read(videoFrame) == true)
33      {
34         cv::imshow("Display", videoFrame);
35         cv::waitKey(5);
36
37         std::cout << "timestamp = " << time(0) << std::endl;
38         // if (video.get(CV_CAP_PROP_POS_FRAMES) == video.get(CV_CAP_PROP_FRAME_COUNT))
39         // {
40         //    break;
41         // }
42      }
43   }
44}

Here is part of the output when the end of the video is reached:

timestamp = 1336145432
timestamp = 1336145432
timestamp = 1336145432
Rewinding video
timestamp = 1336145438
timestamp = 1336145438
timestamp = 1336145438

And if I uncomment the test where I break the loop when the last frame is reached:

timestamp = 1336145474
timestamp = 1336145474
timestamp = 1336145474
Rewinding video
timestamp = 1336145474
timestamp = 1336145474
timestamp = 1336145474

I'm using the 2.4.0 OpenCV release (from the tar) on ubuntu 12.04 amd64, with ffmpeg 0.8.1.


cvvideocapture.cpp - my program (1.3 kB) Julien Hirel, 2012-05-04 05:52 pm

scorpioHD.mp4 - Video that was tested (2.7 MB) Julien Hirel, 2012-05-04 05:52 pm


Associated revisions

Revision 57e26bde
Added by Alexander Reshetnikov almost 13 years ago

Accelerating the completion of video bypass (bug #1893)

History

Updated by Alexander Reshetnikov almost 13 years ago

Thank you for the report, Julien! It was fixed in r8315.

  • Status changed from Open to Done
  • Target version set to 2.4.1

Updated by Andrey Kamaev almost 13 years ago

  • Target version changed from 2.4.1 to 3.0

Updated by Andrey Kamaev over 12 years ago

  • Category changed from highgui-images to highgui-video

Updated by Andrey Kamaev over 12 years ago

  • Assignee deleted (Alexander Reshetnikov)
  • Target version changed from 3.0 to 2.4.3

Also available in: Atom PDF