%% Read Video Using Frame Index and Time Interchangeably
% Read a frame from a video by specifying a frame index, and then read the remaining frames of the video one frame at a time.
% Create a VideoReader object and display the value of the CurrentTime property. For this video, a zero value for the CurrentTime property indicates that no frames have been read from the video.

vidObj = VideoReader('xylophone.mp4');
vidObj.CurrentTime

% Read the 20th frame from the video by specifying the frame index. Then, display the value of the CurrentTime property. The read method automatically updates the CurrentTime property to reflect that the 20th frame has been read. At this step, a call to the readFrame function would return the 21st frame.

frame20 = read(vidObj,20);
vidObj.CurrentTime

% Read the remaining frames of the video using the readFrame method. The readFrame method returns the frame corresponding to the time in the CurrentTime property. For instance, this code reads and displays the frames starting at the 21st frame and continues until there are no more frames to read.

while(hasFrame(vidObj))
    frame = readFrame(vidObj);
    imshow(frame);
    title(sprintf('Current Time = %.3f sec', vidObj.CurrentTime));
    pause(2/vidObj.FrameRate);
end

%  Copyright 2019 The MathWorks, Inc.