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 CurrentTime, Tag and UserData.

BitsPerPixel

Bits per pixel of the video data.

CurrentTime

Timestamp of the video frame to read, specified in seconds from the start of the video file. CurrentTime can be a double value between zero and the duration of the video.

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

    Note:   NumberOfFrames will be removed in a future release.

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;

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
hasFrameDetermine if frame available to read
readRead video frame data from file
readFrameRead video frame from video 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');
get(xyloObj)
obj = 

  VideoReader with properties:

   General Properties:
           Name: 'xylophone.mp4'
           Path: 'matlabroot\toolbox\matlab\audiovideo'
       Duration: 4.7000
    CurrentTime: 0
            Tag: ''
       UserData: []

   Video Properties:
           Width: 320
          Height: 240
       FrameRate: 30
    BitsPerPixel: 24
     VideoFormat: 'RGB24'

Read and Play Back Movie File

Read and play back the sample movie file, xylophone.mp4.

Construct a VideoReader object to read data from the sample file. Then, determine the width and height of the video.

xyloObj = VideoReader('xylophone.mp4');

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

Create a movie structure array, mov.

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

Read one frame at a time until the end of the video is reached.

k = 1;
while hasFrame(xyloObj)
    mov(k).cdata = readFrame(xyloObj);
    k = k+1;
end

Size a figure based on the video's width and height. Then, play back the movie once at the video's frame rate.

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

movie(hf,mov,1,xyloObj.FrameRate);
Was this topic helpful?