Read Video Files Read All Frames in Video File This example shows how to read and store data from all frames in a video file, display one frame, and then play all frames at the video's frame rate. Construct a VideoReader object associated with the sample file, xylophone.mp4. vidObj = VideoReader('xylophone.mp4');
Determine the height and width of the frames. vidHeight = vidObj.Height; vidWidth = vidObj.Width;
Create a MATLAB® movie structure array, s. s = struct('cdata',zeros(vidHeight,vidWidth,3,'uint8'),... 'colormap',[]);
Read one frame at a time using readFrame until the end of the file is reached. Append data from each video frame to the structure array. k = 1; while hasFrame(vidObj) s(k).cdata = readFrame(vidObj); k = k+1; end
Get information about the movie structure array, s. whos s
Name Size Bytes Class Attributes s 1x141 32503552 struct s is a 1by141 structure array, containing data from the 141 frames in the video file. Display the fifth frame stored in s. image(s(5).cdata)
Resize the current figure and axes based on the video's width and height. Then, play the movie once at the video's frame rate using the movie function. set(gcf,'position',[150 150 vidObj.Width vidObj.Height]); set(gca,'units','pixels'); set(gca,'position',[0 0 vidObj.Width vidObj.Height]); movie(s,1,vidObj.FrameRate);
Close the figure. close
Read All Frames Beginning at Specified Time Read part of a video file starting 0.5 second from the beginning of the file. Construct a VideoReader object associated with the sample file, 'xylophone.mp4'.
vidObj = VideoReader('xylophone.mp4');
Specify that reading should begin 0.5 second from the beginning of the file by setting theCurrentTime property. vidObj.CurrentTime = 0.5;
Create an axes to display the video. Then, read video frames until the end of the file is reached. currAxes = axes; while hasFrame(vidObj) vidFrame = readFrame(vidObj); image(vidFrame, 'Parent', currAxes); currAxes.Visible = 'off'; pause(1/vidObj.FrameRate); end
Read Video Frames Within Specified Time Interval Read part of a video file from 0.6 to 0.9 second. Construct a VideoReader object associated with the sample file, 'xylophone.mp4'. vidObj = VideoReader('xylophone.mp4');
Specify that reading should begin 0.6 second from the beginning of the file by setting theCurrentTime property. vidObj.CurrentTime = 0.6;
Read one frame at a time until the CurrentTime reaches 0.9 second. Append data from each video frame to the structure array, s. k = 1; while vidObj.CurrentTime <= 0.9 s(k).cdata = readFrame(vidObj); k = k+1; end
View the number of frames in s.
whos s
Name Size Bytes Class Attributes s 1x141 32503552 struct
s is a 1by141 structure array because readFrame read 141 frames. View the CurrentTime property of the VideoReader object. vidObj.CurrentTime
ans = 0.9333
The CurrentTime property is now greater than 0.9.
Troubleshooting: Cannot Read Last Frame of Video File The hasFrame method might return logical 1 (true) when the value of the CurrentTimeproperty is equal to the value of the Duration property. This is due to a limitation in the underlying APIs used. Avoid seeking to the last frame in a video file by setting the CurrentTime property to a value close to the Duration value. For some files, this operation returns an error indicating that the endoffile has been reached, even though the CurrentTime value is less than the Durationvalue. This typically occurs if the file duration is larger than the duration of the video stream, and there is no video available to read near the end of the file. Do not use the Duration property to limit the reading of data from a video file. It is best to read data until the file reports that there are no more frames available to read. That is, use thehasFrame method to check whether there is a frame available to read.
See Also mmfileinfo | movie | VideoReader
More About Supported Video File Formats