This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

VideoReader

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 File Formats.

Creation

Syntax

v = VideoReader(filename)
v = VideoReader(filename,Name,Value)

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 paris. For example, VideoReader('myfile.mp4','CurrentTime',1.2) starts reading 1.2 seconds into the video. You can specify multiple name-value pairs. Enclose each property name in single quotes followed by its value.

Input Arguments

expand all

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

For more information, see Supported Video 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.

Note

NumberOfFrames is not recommended. Use CurrentTime instead.

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

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

readRead video frame data from file
VideoReader.getFileFormatsFile formats that VideoReader supports
readFrameRead video frame from video file
hasFrameDetermine if frame is available to read

Examples

collapse all

Create a VideoReader object for the example movie file, xylophone.mp4.

v = VideoReader('xylophone.mp4');

Read all video frames.

while hasFrame(v)
    video = readFrame(v);
end
whos video
  Name         Size                Bytes  Class    Attributes

  video      240x320x3            230400  uint8              

Create an object to read the sample file, xylophone.mp4.

v = VideoReader('xylophone.mp4');

Specify the reading to begin 2.5 seconds from the beginning of the video.

v.CurrentTime = 2.5;

Create an axes object. Then, read 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

Tips

  • 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 the VideoReader to read this newly converted video file.

Introduced in R2010b