Read Video Files
This example shows how to read frames from a video starting at a specific time or frame index, read frames within a specified interval, and read all the frames in the video.
Read Frames Beginning at Specified Time or Frame Index
Read part of a video file starting 0.5 second from the beginning of the file. Then, read the video starting from frame index 100 to the end of the video file.
VideoReader object for the sample video file
vidObj = VideoReader("xylophone_video.mp4");
Specify that reading should begin 0.5 second from the beginning of the file by setting the
vidObj.CurrentTime = 0.5;
Read video frames until the end of the file by using the
while hasFrame(vidObj) vidFrame = readFrame(vidObj); imshow(vidFrame) pause(1/vidObj.FrameRate) end
Alternatively, you can read frames from a video starting at a specified frame index to the end of the video by using the
read method. Specify the indices to read as
[100 Inf]. The
read method returns all the frames starting at 100 to the end of the video file.
vidframes = read(vidObj,[100 Inf]);
Read Frames Within Specified Interval
Read a part of a video file by specifying the time or frame interval.
Read the video frames between 0.6 and 0.9 seconds. First, create a
VideoReader object and a structure array to hold the frames.
vidObj = VideoReader("xylophone_video.mp4"); s = struct("cdata",zeros(vidObj.Height,vidObj.Width,3,"uint8"),colormap=);
Then specify that reading should begin 0.6 second from the beginning of the file by setting the
vidObj.CurrentTime = 0.6;
Read one frame at a time until
CurrentTime reaches 0.9 second. Append data from each video frame to the structure array. View the number of frames in the structure array.
s is a 1-by-10 structure array indicating that 10 frames were read. For information on displaying the frames in the structure array
s as a movie, see the
movie function reference page.
k = 1; while vidObj.CurrentTime <= 0.9 s(k).cdata = readFrame(vidObj); k = k+1; end whos s
Name Size Bytes Class Attributes s 1x10 2305344 struct
Alternatively, you can read all the frames in a specified interval by using frame indices. For example, specify the range of frames to read as
[18 27]. The
read method returns an m-by-n-by-p-by-10 array (where each frame has size m-by-n-by-p), which indicates that 10 frames were read.
frames = read(vidObj,[18 27]);
Name Size Bytes Class Attributes frames 240x320x3x10 2304000 uint8
Read All Frames
Read all the frames from the video, one frame at a time or all of the frames at once.
VideoReader object and display the total number of frames in the video.
vidObj = VideoReader("xylophone_video.mp4");
ans = 141
Read all of the frames, one frame at a time, by using the
readFrame method, and display the frames.
while hasFrame(vidObj) frame = readFrame(vidObj); imshow(frame) pause(1/vidObj.FrameRate) end
Alternatively, you can read all the video frames at once. The
read method returns an m-by-n-by-p-by-141 array (where each frame has size m-by-n-by-p) of video frames.
allFrames = read(vidObj);
Name Size Bytes Class Attributes allFrames 240x320x3x141 32486400 uint8
Troubleshooting and Tips for Video Reading
On Windows® platforms, you cannot modify or delete an AVI file that is referenced by a
VideoReaderobject in your workspace. To remove
VideoReaderobjects from your workspace, use the
For some MP4 files, the
NumFramesproperty can return different values on Windows, Mac, and Linux® platforms. This variation results from differences in the underlying platform-specific APIs.
hasFramemethod might return logical
true) when the value of the
CurrentTimeproperty is equal to the value of the
Durationproperty. This behavior is due to a limitation in the underlying platform-specific APIs used.
Seeking to the last frame in a video file by setting the
CurrentTimeproperty to a value close to the
Durationvalue is not recommended. For some files, this operation returns an error indicating that the end-of-file has been reached, even though the
CurrentTimevalue is less than the
Durationvalue. This issue 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.
Use of the
Durationproperty to limit the reading of data from a video file is not recommended. Use the
hasFramemethod to check whether there is a frame available to read. It is best to read data until the file reports that there are no more frames available to read.
Video Reading Performance on Windows Systems: To achieve better video reader performance on Windows for
MOVfiles, MATLAB® uses the system’s graphics hardware for decoding. However, in some cases using the graphics card for decoding can result in poorer performance depending on the specific graphics hardware on the system. If you notice slower video reader performance on your system, turn off the hardware acceleration by typing:
matlab.video.read.UseHardwareAcceleration('off'). You can reenable hardware acceleration by typing: