MATLAB Answers


Number Of Frames in Video File with Matlab 2015b

Asked by albayenes on 21 Oct 2015
Latest activity Commented on by Akshay Ravindran on 25 Oct 2017
In Matlab 2015b, NumberOfFrames property of VideoReader is deprecated. So, what is the best way to learn number of frames in a video?

  1 Comment

When I use NumberOfFrames in a script, there's some hover-over advice to "use 'CurrentTime' instead." This is not particularly helpful and seems wrong. Could someone update this help?

Sign in to comment.

1 Answer

Answer by Dinesh Iyer on 21 Oct 2015
 Accepted Answer

The NumberOfFrames property is on the path to deprecation but querying this property still works. This deprecation indicates that this might be removed in some future release. This was first announced in 14b but has not been removed for the past three releases.
If this property is infact removed, then there are two ways to determine this.
First way is to compute it using the formula:
vidObj = VideoReader('myfile.mp4');
numFrames = ceil(vidObj.FrameRate*vidObj.Duration);
This formula above gives only an "estimate" of the number of frames. For fixed frame-rate files, the value will be within 1 of the actual number of frames due to rounding issues. Many video files are variable frame-rate and so for those files the actual number of frames can be more or less than the estimated value.
To get the exact value, you have no choice but to scan through the entire file using the code below:
vidObj = VideoReader('myfile.mp4');
numFrames = 0;
while hasFrame(vidObj)
numFrames = numFrames + 1;
If your application requires knowledge of exact number of frames use the second method. If you want to know this value for pre-allocating arrays use the first method.
Hope this helps.


Efficiency. Populating it requires parsing the entire file for variable frame-rate videos.
Also I would point out that if/when the toolbox starts supporting live streaming video, then NumberOfFrames will not have any useful meaning.
So what exactly does 'use current time' mean? Regardless of efficiency, isn't finding out the exact number of frames helpful if we want to extract each frame and process it.
I am getting a variable number of frame numbers in running the same code on two different machines
i.e. I am reading the number of frames using the above code snippet in two separate machines both with Matlab2017 installed, on the same video file. However, I am getting two different values for frame numbers on each machine. Could you please let me know why that is so?

Sign in to comment.