MATLAB: Randomly picking a fram from a video in matlab

randomvideo

my problem is that i want to randomly pick a frame from a video , using mmreader , and also i am getting the last frame of the video so that i can set limit, so that when using rand function the number can be picked from 1 to the last frame number,the code is;
readerobj = mmreader(myvideoname);
last_Frame_number = read(readerobj, inf);
end_frame=readerobj.NumberOfFrames;
random_frame=randi([1,end_frame]);
can someone tell a better solution because this take alot of time because every video has a varying size , and i want to make it more efficient.

Best Answer

For videos that are not variable-frame-rate, the NumberOfFrames property will be set when the object is created. If you are not getting a warning when you create the object (before you even do anything to it) then the NumberOfFrames will reflect the total number in the file.
Related Question