Main Content

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.

Create a VideoReader object for the sample video file xylophone_video.mp4.

vidObj = VideoReader("xylophone_video.mp4");

Specify that reading should begin 0.5 second from the beginning of the file by setting the CurrentTime property.

vidObj.CurrentTime = 0.5;

Read video frames until the end of the file by using the readFrame method.

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]);

Clear the VideoReader object.

clear vidObj

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 CurrentTime property.

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]);
whos frames
  Name          Size                    Bytes  Class    Attributes

  frames      240x320x3x10            2304000  uint8              

Clear the VideoReader object.

clear vidObj

Read All Frames

Read all the frames from the video, one frame at a time or all of the frames at once.

Create a VideoReader object and display the total number of frames in the video.

vidObj = VideoReader("xylophone_video.mp4");
vidObj.NumFrames
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);
whos allFrames
  Name             Size                      Bytes  Class    Attributes

  allFrames      240x320x3x141            32486400  uint8              

Clear the VideoReader object.

clear vidObj

Troubleshooting and Tips for Video Reading

  • On Windows® platforms, you cannot modify or delete an AVI file that is referenced by a VideoReader object in your workspace. To remove VideoReader objects from your workspace, use the clear function.

  • For some MP4 files, the NumFrames property can return different values on Windows, Mac, and Linux® platforms. This variation results from differences in the underlying platform-specific APIs.

  • The hasFrame method might return logical 1 (true) when the value of the CurrentTime property is equal to the value of the Duration property. 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 CurrentTime property to a value close to the Duration value is not recommended. For some files, this operation returns an error indicating that the end-of-file has been reached, even though the CurrentTime value is less than the Duration value. 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 Duration property to limit the reading of data from a video file is not recommended. Use the hasFrame method 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 MP4 and MOV files, 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: matlab.video.read.UseHardwareAcceleration('on').

See Also

| | | |

Related Topics