Main Content

VideoReader

Create object to read video files

Description

Use a VideoReader object to read files containing video data. The object contains information about the video file and enables you to read data from the video. You can create a VideoReader object using the VideoReader function, query information about the video using the object properties, and then read the video using object functions.

For more information, see Supported Video and Audio File Formats.

Creation

Description

example

v = VideoReader(filename) creates object v to read video data from the file named filename.

example

v = VideoReader(filename,Name,Value) sets the properties CurrentTime, Tag, and UserData using name-value arguments. For example, VideoReader('myfile.mp4','CurrentTime',1.2) starts reading 1.2 seconds into the video. You can specify multiple name-value arguments. Enclose each property name in single quotes followed by the corresponding value.

Input Arguments

expand all

File name, specified as a character vector or string scalar.

For more information, see Supported Video and Audio File Formats.

Example: 'myFile.mp4'

Example: '../dir/videos/myFile.avi'

Data Types: char | string

Properties

expand all

The VideoReader object has properties that contain information about the video file. Properties are read-only, except CurrentTime, Tag, and UserData. You can view or modify the value of a property after creating the object. For example, this command finds the value of the Duration property of the VideoReader object, v.

D = v.Duration;

This property is read-only.

Bits per pixel of the video data, specified as a numeric scalar.

Data Types: double

Timestamp of the video frame to read, specified as a numeric scalar. The timestamp is specified in seconds from the start of the video file. The value of CurrentTime can be between zero and the duration of the video.

On some platforms, when you create a VideoReader object, the 'CurrentTime' property might contain a value close to, but not exactly, zero. This variation in the value of the 'CurrentTime' property is due to differences in how each platform processes and reads videos.

Example: 5.6

Data Types: double

This property is read-only.

Length of the file in seconds, specified as a numeric scalar.

Data Types: double

This property is read-only.

Number of video frames per second, specified as a numeric scalar. For variable-frame rate video, FrameRate is the average frame rate.

Note: For OS X Yosemite (Version 10.10) and later, MPEG-4/H.264 files written using VideoWriter play correctly, but display an inexact frame rate.

Data Types: double

This property is read-only.

Height of the video frame in pixels, specified as a numeric scalar.

Data Types: double

This property is read-only.

File name, specified as a character vector or string scalar.

Data Types: char | string

This property is read-only.

Number of frames in the video stream, specified as a numeric scalar.

Note

For certain length videos, the value of the NumFrames property is not immediately available. To get the NumFrames property, type v.NumFrames in the command line.

Data Types: double

This property is read-only.

Full path to the video file associated with the reader object, specified as a character vector or string scalar.

Data Types: char | string

Generic text, specified as a character vector or string scalar.

Example: 'Experiment 109'

Data Types: char | string

User-defined data, specified as a value of any data type.

This property is read-only.

MATLAB representation of the video format, specified as a character vector or string scalar.

File types, except for Motion JPEG 2000 files, have one of these VideoFormat values.

Video Format

Value of VideoFormat

AVI or MPEG-4 files with RGB24 video

'RGB24'

AVI files with indexed video

'Indexed'

AVI files with grayscale video

'Grayscale'

Motion JPEG 2000 files, have one of the following VideoFormat values.

Format of Image Data

Value of VideoFormat

Single-band uint8'Mono8'
Single-band int8'Mono8 Signed'
Single-band uint16'Mono16'
Single-band int16'Mono16 Signed'
Three-banded uint8'RGB24'
Three-banded int8'RGB24 Signed'
Three-banded uint16'RGB48'
Three-banded int16'RGB48 Signed'

Data Types: char | string

This property is read-only.

Width of the video frame in pixels, specified as a numeric scalar.

Data Types: double

Object Functions

hasFrameDetermine if video frame is available to read
readRead one or more video frames
readFrameRead next video frame
VideoReader.getFileFormatsFile formats that VideoReader supports

Examples

collapse all

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

v = VideoReader("xylophone_video.mp4");

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

while hasFrame(v)
    frame = readFrame(v);
end

Display information about the last frame returned by readFrame.

whos frame
  Name         Size                Bytes  Class    Attributes

  frame      240x320x3            230400  uint8              

Clear the VideoReader object.

clear v

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

v = VideoReader("xylophone_video.mp4");

Specify to start reading frames at 2.5 seconds from the beginning of the video.

v.CurrentTime = 2.5;

Create an axes object to display the frame. Then continue to read and display video frames until no more frames are available to read.

currAxes = axes;
while hasFrame(v)
    vidFrame = readFrame(v);
    image(vidFrame,"Parent",currAxes)
    currAxes.Visible = "off";
    pause(1/v.FrameRate)
end

Figure contains an axes object. The axes object contains an object of type image.

Figure contains an axes object. The axes object contains an object of type image.

Clear the VideoReader object.

clear v

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

v = VideoReader("xylophone_video.mp4");

Read only the first video frame.

firstFrame = read(v,1);

Read only the last video frame.

lastFrame = read(v,Inf);

Read frames 5 through 10.

earlyFrames = read(v,[5 10]);

Read from the 50th frame to the end of the video file.

lateFrames = read(v,[50 Inf]);

Display the size and type information of the video frame variables.

whos *Frame*
  Name               Size                     Bytes  Class    Attributes

  earlyFrames      240x320x3x6              1382400  uint8              
  firstFrame       240x320x3                 230400  uint8              
  lastFrame        240x320x3                 230400  uint8              
  lateFrames       240x320x3x92            21196800  uint8              

Clear the VideoReader object.

clear v

Read a frame from a video by specifying a frame index, and then read the remaining frames of the video one frame at a time.

Create a VideoReader object and display the value of the CurrentTime property. A zero value for the CurrentTime property indicates that no frames have been read from the video.

vidObj = VideoReader("xylophone_video.mp4");
vidObj.CurrentTime
ans = 0

Read the 20th frame from the video by specifying the frame index. Then, display the value of the CurrentTime property. The read method automatically updates the CurrentTime property to reflect that the 20th frame has been read.

frame20 = read(vidObj,20);
vidObj.CurrentTime
ans = 0.6667

At this point, a call to the readFrame function would return the 21st frame.

Read the remaining frames of the video using the readFrame method. The readFrame method returns the frame corresponding to the time in the CurrentTime property. For instance, this code reads and displays the frames starting at the 21st frame and continues until there are no more frames to read.

while(hasFrame(vidObj))
    frame = readFrame(vidObj);
    imshow(frame)
    title(sprintf("Current Time = %.3f sec",vidObj.CurrentTime))
    pause(2/vidObj.FrameRate)
end

Figure contains an axes object. The axes object with title Current Time = 4.700 sec contains an object of type image.

Clear the VideoReader object.

clear vidObj

Limitations

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

  • For some AVI, MOV, or MP4 files on Windows, using the readFrame function to read all of the frames in the file can result in a different number of frames from the value returned by the NumFrames property of the VideoReader object.

Tips

  • 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.

  • The macOS platform no longer supports certain older video file formats. To read such files using VideoReader:

    • Open the video file using the QuickTime player. If the player detects the file to be of an older format, then it automatically converts the file to a newer format.

    • Save the newly converted video file.

    • Use VideoReader to read this newly converted video file.

Extended Capabilities

Version History

Introduced in R2010b

expand all