Number Of Frames in Video File with Matlab 2015b

조회 수: 68 (최근 30일)
albayenes
albayenes 2015년 10월 21일
댓글: Walter Roberson 2021년 3월 17일
Hi,
In Matlab 2015b, NumberOfFrames property of VideoReader is deprecated. So, what is the best way to learn number of frames in a video?
  댓글 수: 1
Cristina Vasco
Cristina Vasco 2016년 7월 18일
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?

댓글을 달려면 로그인하십시오.

채택된 답변

Dinesh Iyer
Dinesh Iyer 2015년 10월 21일
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)
readFrame(vidObj);
numFrames = numFrames + 1;
end
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.
Dinesh
  댓글 수: 7
Billy Pilgrim
Billy Pilgrim 2021년 3월 3일
Hi Dinesh,
Thanks for your answer above. I've been using the 'while hasFrame' approach for a while now for getting exact frame numbers.
I was wondering if you had any approaches that might expedite the process? Preallocating isn't a thing in this case because numFrames is just a single value, not an array we're adding to? Similarly, there's no way to read a subset of the frame rather than the entire frame, correct? Just wondering if you had any ideas of tricks for speeding things up...
Walter Roberson
Walter Roberson 2021년 3월 17일
You could potentially estimate the number of frames, then subtract a bit in case of estimation error, and then use the read() method instead of readFrame() to position to a particular frame number, and then readFrame from there to find the actual last frame.
However, it is not clear that read() with a frame number is efficient: it might have to read all of the frames in order to count them.
What is efficient is positioning by time. But positioning by time does not give you a frame count.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by