MATLAB Answers


Problem for Read Frame of Video Clip (.mp4)

Asked by Taegeun
on 14 Nov 2013
Latest activity Answered by Walter Roberson
on 14 Nov 2013

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) );          

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.



No products are associated with this question.

1 Answer

Answer by 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.


Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!