Skip to content
This repository was archived by the owner on Jan 25, 2022. It is now read-only.

Video reading 2 frames less #181

Open
ggian opened this issue Jul 30, 2015 · 2 comments
Open

Video reading 2 frames less #181

ggian opened this issue Jul 30, 2015 · 2 comments
Labels

Comments

@ggian
Copy link

ggian commented Jul 30, 2015

I started using mexopencv 2 days ago. Congratulations and thank you for your work. The problem I have is that when a read a video it reads 2 frames less than normal video length. I tested it with both MATLAB VideoFileReader (normal length) and mexopencv (2 frames less).
The code I use is

cap = cv.VideoCapture(video_path);
for i=1:cap.get('FrameCount') 
        img = cap.read(); 
        if isempty(img), break; end 
        imshow(img) 
        drawnow
        disp(i);
end 

e.g. A video with 1446 frames goes until 1444 frames.

thanks

@amroamroamro
Copy link
Collaborator

Here is a quote from MATLAB's VideoReader:

NumberOfFrames will be removed in a future release.

Some files store video at a variable frame rate, including many Windows Media Video files.
For these files, VideoReader cannot determine the number of frames until you read the
last frame.

To count the number of frames in a variable frame rate file,
use the read method to read the last frame of the file.

Which is why both VideoReader and vision.VideoFileReader suggest iterating over frames one-by-one, instead of querying the count at the beginning...

The same thing applies to OpenCV, the FrameCount property is sometimes off, and the only way to exactly determine the number of frames is to read all the video frames:

vid = cv.VideoCapture('video.mp4');
i = 0;
while true
    frame = vid.read();
    if isempty(frame), break; end
    i = i + 1;
end
display(i)

@amroamroamro
Copy link
Collaborator

Also I wouldn't exclude the possibility of a bug in OpenCV (or one of the underlying video drivers it uses). There are a couple of reports online regarding this...

If that's the case, we can't do much about it in mexopencv, and it should be reported upstream.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants