|On this page…|
To import video data from a file, construct a reader object with VideoReader and read selected frames with the read function.
For example, import all frames in the file xylophone.mp4:
xyloObj = VideoReader('xylophone.mp4'); vidFrames = read(xyloObj);
It is not necessary to close the multimedia object.
Use the read function to specify a video frame to read. For example, read the second frame in a hypothetical file, indexedImg.avi.
obj = VideoReader('indexedImg.avi'); vidFrame = read(obj,2);
Use the image function to display the video frame. For indexed video, you can apply a colormap. In this case, gray(256) applies a linear grayscale colormap of length 256.
This example shows how to save memory by processing a video one frame at a time. For faster processing, preallocate memory to store the video data. Convert the file xylophone.mp4 to a MATLAB® movie, and play it with the movie function
Create a VideoReader object for xylophone.mp4. Determine the number of frames, and the height and width of the frames.
xyloObj = VideoReader('xylophone.mp4'); nFrames = xyloObj.NumberOfFrames; vidHeight = xyloObj.Height; vidWidth = xyloObj.Width;
Preallocate the movie structure.
mov(1:nFrames) = ... struct('cdata',zeros(vidHeight,vidWidth,3,'uint8'),... 'colormap',);
Read one frame at a time.
for k = 1 : nFrames mov(k).cdata = read(xyloObj,k); end
Play back the movie once at the video's frame rate.
Some files store video at a variable frame rate, including many Windows Media® Video files. For these files, VideoReader cannot determine the number of frames until you read the last frame.
For example, consider a hypothetical file VarFrameRate.wmv that has a variable frame rate. A call to VideoReader to create the multimedia object such as
obj = VideoReader('VarFrameRate.wmv')
returns the following warning and summary information:
Warning: Unable to determine the number of frames in this file. Summary of Multimedia Reader Object for 'VarFrameRate.wmv'. Video Parameters: 23.98 frames per second, RGB24 1280x720. Unable to determine video frames available.
To determine the number of frames in a variable frame rate file, call read with the following syntax:
lastFrame = read(obj, inf);
This command reads the last frame and sets the NumberOfFrames property of the multimedia object. Because VideoReader must decode all video data to count the frames reliably, the call to read sometimes takes a long time to run.
For any video file, you can specify the frames to read with a range of indices. Usually, if you request a frame beyond the end of the file, read returns an error.
However, if the file uses a variable frame rate, and the requested range straddles the end of the file, read returns a partial result. For example, given a file with 2825 frames associated with the multimedia object obj, a call to read frames 2800 - 3000 as follows:
images = read(obj, [2800 3000]);
Warning: The end of file was reached before the requested frames were read completely. Frames 2800 through 2825 were returned.