Read Video Files

Read frames from a video starting at a specific time or frame index, read frames within a specified interval, or 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.

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

vidObj.CurrentTime = 0.5;

Read video frames until the end of the file is reached 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]);

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 video reader object and a structure array to hold the frames.

vidObj = VideoReader('xylophone.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 indicating that 10 frames were read. For information on displaying the frames in the structure 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            2305432  struct              

Alternatively, you can read all the frames in a specified interval by using frame indices. For example, specify the second argument of read as [18 27]. The read method returns a FrameSize-by-10 array indicating that 10 frames were read.

frames = read(vidObj,[18 27]);
whos frames
  Name          Size                    Bytes  Class    Attributes

  frames      240x320x3x10            2304000  uint8              

Read All Frames

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

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

vidObj = VideoReader('xylophone.mp4');
vidObj.NumFrames
ans = 141

Read all 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 a FrameSize-by-141 array of video frames.

allFrames = read(vidObj);
whos allFrames
  Name             Size                      Bytes  Class    Attributes

  allFrames      240x320x3x141            32486400  uint8              

Troubleshooting and Tips For Video Reading

  • 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 is due to a limitation in the underlying 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 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