Problem for Read Frame of Video Clip (.mp4)

5 views (last 30 days)
Taegeun
Taegeun on 14 Nov 2013
Commented: Walter Roberson on 6 Jul 2018
I am having trouble using VideoReader to determine the correct number of frames in a video file,
videoObj = VideoReader('test.mp4') ;
Warning: Unable to determine the number of frames in this file.
This workaround fails,
lastFrame = read(videoObj, inf)
Error using VideoReader/read (line 86) The frame index requested is beyond the end of the file. The following code returns a different number of frames each time it's run, e.g. 100, 104, 112.
LenSeq = 300;
videoObj = VideoReader(test.mp4');
nFrames = xyloObj.NumberOfFrames;
vidHeight = xyloObj.Height;
vidWidth = xyloObj.Width;
cnt = 1;
for k = 1:10:LenSeq,
tp_image = rgb2gray( read(xyloObj, k) );
end
If VideoReader skipping or repeating frames on each pass through the file, how can I get the correct number of frames?
In addition, xyloObj.NumberOfFrames have value of []. (nothing) Is this cause a problem?
More info: problem is present for both R2011a run on Windows 7 (64-bit versions), video format is MPEG-4/H.264, size of the video is 4,782KB, and it's a variable frame rate file.

Answers (2)

Walter Roberson
Walter Roberson on 14 Nov 2013
NumberOfFrames would not be meaningfully filled in on a variable frame rate file, instead reflecting only the highest frame that you have read so far.
What if you ask to read the last frame first off, then throw that away and go from the beginning?
read(xyloObj, inf);
After that NumberOfFrames should be meaningful.

Abhik
Abhik on 5 Jul 2018
Does any one have solution to this problem, for variable frame rate .mp4 files, i see that read and readFrame throws error MATLAB:audiovideo:VideoReader:invalidFrameVarFrameRate and MATLAB:audiovideo:VideoReader:EndOfFile respectively.
Thanks in advance.
  1 Comment
Walter Roberson
Walter Roberson on 6 Jul 2018
The possibilities depend on which MATLAB release you are using.
My experience is that the only reliable method is to read the file through one frame at a time counting frames.
There was a range of releases where asking to open a video triggered it internally reading the entire video in order to count the frames. However, the accurate count was often not needed, and often not needed every time the video was opened, so MATLAB changed to only estimate at most. I think it was the same release that introduced readFrame()

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!