Documentation Center

  • Trial Software
  • Product Updates

VideoReader class

Read video files

Description

Use the VideoReader function with the read method to read video data from a file into the MATLAB® workspace.

The file formats that VideoReader supports vary by platform, as follows (with no restrictions on file extensions):

All Platforms

AVI, including uncompressed, indexed, grayscale, and Motion JPEG-encoded video (.avi)
Motion JPEG 2000 (.mj2)

All Windows®

MPEG-1 (.mpg)
Windows Media® Video (.wmv, .asf, .asx)
Any format supported by Microsoft® DirectShow®

Windows 7 or later

MPEG-4, including H.264 encoded video (.mp4, .m4v)
Apple QuickTime Movie (.mov)
Any format supported by Microsoft Media Foundation

Macintosh

Most formats supported by QuickTime Player, including:
MPEG-1 (.mpg)
MPEG-4, including H.264 encoded video (.mp4, .m4v)
Apple QuickTime Movie (.mov)
3GPP
3GPP2
AVCHD
DV

Linux®

Any format supported by your installed plug-ins for GStreamer 0.10 or above, as listed on http://gstreamer.freedesktop.org/documentation/plugins.html, including Ogg Theora (.ogg).

Construction

obj = VideoReader(filename) constructs obj to read video data from the file named filename. If it cannot construct the object for any reason, VideoReader generates an error.

obj = VideoReader(filename,Name,Value) constructs the object with additional options specified by one or more Name,Value pair arguments. Name is 'Tag' or 'UserData' and Value is the corresponding value. You can specify two name and value pair arguments in any order as Name1,Value1,Name2,Value2.

Input Arguments

filename

String in single quotation marks that specifies the video file to read. The VideoReader constructor searches for the file on the MATLAB path.

Name-Value Pair Arguments

Optional comma-separated pairs of Name,Value arguments, where Name is 'Tag' or 'UserData' and Value is the corresponding value. You can specify two name and value pair arguments in any order as Name1,Value1,Name2,Value2.

'Tag'

String that identifies the object.

Default: ''

'UserData'

Generic field for data of any class that you want to add to the object.

Default: []

Properties

All properties are read-only except Tag and UserData.

BitsPerPixel

Bits per pixel of the video data.

Duration

Total length of the file in seconds.

FrameRate

Frame rate of the video in frames per second.

Height

Height of the video frame in pixels.

Name

Name of the file associated with the object.

NumberOfFrames

Total number of frames in the video stream.

Some files store video at a variable frame rate, including many Windows Media Video files. For these files, VideoReader cannot determine the number of frames until you read the last frame. When you construct the object, VideoReader returns a warning and does not set the NumberOfFrames property.

To count the number of frames in a variable frame rate file, use the read method to read the last frame of the file. For example:

vidObj = VideoReader('varFrameRateFile.wmv');
lastFrame = read(vidObj, inf);
numFrames = vidObj.NumberOfFrames;

For more information, see Read Variable Frame Rate Video in the MATLAB Data Import and Export documentation..

Path

String containing the full path to the file associated with the reader.

Tag

String that identifies the object.

Default: ''

Type

Class name of the object: 'VideoReader'.

UserData

Generic field for data of any class that you want to add to the object.

Default: []

VideoFormat

String indicating the MATLAB representation of the video format.

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'

For Motion JPEG 2000 files, VideoFormat is one of the following.

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'

Width

Width of the video frame in pixels.

Methods

getQuery property values for video reader object
getFileFormatsFile formats that VideoReader supports
readRead video frame data from file
setSet property values for video reader object

Copy Semantics

Handle. To learn how handle classes affect copy operations, see Copying Objects in the MATLAB documentation.

Examples

expand all

Construct VideoReader Object

Construct a VideoReader object for the example movie file xylophone.mp4 and view its properties.

xyloObj = VideoReader('xylophone.mp4','Tag','My reader object');
get(xyloObj)
  General Settings:
    Duration = 4.7000
    Name = xylophone.mp4
    Path = matlabroot\toolbox\matlab\audiovideo
    Tag = My reader object
    Type = VideoReader
    UserData = []

  Video Settings:
    BitsPerPixel = 24
    FrameRate = 30
    Height = 240
    NumberOfFrames = 141
    VideoFormat = RGB24
    Width = 320

Read and Play Back Movie File

Read and play back the movie file xylophone.mp4.

xyloObj = VideoReader('xylophone.mp4');

nFrames = xyloObj.NumberOfFrames;
vidHeight = xyloObj.Height;
vidWidth = xyloObj.Width;

Preallocate the movie structure.

mov(1:nFrames) = ...
    struct('cdata',zeros(vidHeight,vidWidth, 3,'uint8'),...
           'colormap',[]);

Read one frame at a time.

for k = 1 : nFrames
    mov(k).cdata = read(xyloObj,k);
end

Size a figure based on the video's width and height.

hf = figure;
set(hf, 'position', [150 150 vidWidth vidHeight])

Play back the movie once at the video's frame rate.

movie(hf, mov, 1, xyloObj.FrameRate);

See Also

|

How To

Was this topic helpful?