VideoReader - Create object to read video files - MATLAB (original) (raw)

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 theVideoReader 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

Syntax

Description

`v` = VideoReader([filename](#d126e1981145)) creates object v to read video data from the file namedfilename.

example

`v` = VideoReader([filename](#d126e1981145),`Name,Value`) sets the properties CurrentTime, Tag, andUserData 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.

example

Input Arguments

expand all

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

Depending on the location of your file, filename can take one of these forms.

Location Form
Current folder Specify the name of the file infilename.Example: "myFile.mp4"
Other folders If the file is not in the current folder or in a folder on the MATLAB® path, then specify the full or relative path infilename.Example: "C:\myFolder\myFile.mp4"Example: "myFolder\myFile.mp4"
URL If the file is located by an internet URL, then filename must contain the protocol type such as,http://.Example: 'http://hostname/path\_to\_file/myFile.mp4'
Remote location If the file is stored at a remote location, then filename must contain the full path of the file specified as a uniform resource locator (URL) of the form:scheme_name://path_to_file/_filename_Based on your remote location,scheme_name can be one of the values in this table. Remote Location_scheme_name_Amazon S3™s3Windows Azure® Blob Storagewasb,wasbsHDFS™hdfsFor more information, see Work with Remote Data.Example: "s3://myBucket/myFolder/myFile.avi" NoteVideoReader does not support reading Motion JPEG 2000 (.mj2) files from remote location.

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

Data Types: char | string

Properties

expand all

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

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 theNumFrames property is not immediately available. To get the NumFrames property, typev.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 theseVideoFormat values.

Video Format Value ofVideoFormat
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 followingVideoFormat values.

Format of Image Data Value ofVideoFormat
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

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.

Name Size Bytes Class Attributes

frame 240x320x3 230400 uint8

Clear the VideoReader object.

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.

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 hidden axes object contains an object of type image.

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

Clear the VideoReader object.

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

v = VideoReader("xylophone_video.mp4");

Read only the first video frame.

Read only the last video frame.

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.

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.

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

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

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 hidden axes object with title Current Time = 4.700 sec contains an object of type image.

Clear the VideoReader object.

Limitations

Tips

Extended Capabilities

expand all

Usage notes and limitations:

Code generation for VideoReader supports most formats, syntaxes, methods, and functions with the following limitations.

Usage notes and limitations:

With MATLAB Coder™ Support Package for NVIDIA® Jetson™ and NVIDIA DRIVE™ Platforms, you can generate CUDA® code for the MATLABVideoReader object to read files containing video data on the NVIDIA target hardware.

To learn how to generate CUDA code for reading video files on the NVIDIA target by using the VideoReader function, see Read Video Files on NVIDIA Hardware (MATLAB Coder).

Usage notes and limitations:

This function does not support thread-based environments on Windows platforms when reading data in MPEG-1 format (.mpg), Windows Media Video format (.wmv), or other formats that use Microsoft® DirectShow® for decoding.

For more information, see Run MATLAB Functions in Thread-Based Environment.

Version History

Introduced in R2010b

expand all

You can run VideoReader in the background using MATLABbackgroundPool.

Pixel value differences might exist between JPEG 2000 images in R2021b and previous releases of MATLAB.

The VideoReader object supports interchangeable access to video frames using frame index or time. Therefore, you can use read andreadFrame interchangeably. Previously, you could use only one type of access at a time. Attempting to read frames interchangeably usingread and readFrame resulted in an error.

For large video files, the generated code for the VideoReader object with a row-major layout option shows improved performance. For example, thetimingTest function shows about a 4x speed-up on aH.264 video file with a resolution of1280x720.

function [t, data] = timingTest(fileName) vidObj = VideoReader(fileName); data = cell(20,1); tic; for cnt = 1:20 data{cnt} = readFrame(vidObj); end t = toc; end

Generate code for the timingTest function with the row-major flag. The codegen command creates a functiontimingTest_mex with the C and C++ generated code.

codegen timingTest-args{coder.typeof('', [1 inf])}-rowmajor

For a H.264 video file with a resolution of1280x720, the execution times are:

R2019a: 4.04s

R2019b: 0.95s

The code was timed on a Windows 10, Intel® Xeon® CPU W-2133 @ 3.6 GHz test system by calling the functiontimingTest_mex. The higher the video file resolution (measured by frame size), the greater the performance improvement becomes.

The NumberOfFrames property is not recommended. Use theNumFrames property instead. There are no plans to remove the NumberOfFrames property.

You can no longer create an array of VideoReader objects. Update your code to remove arrays of VideoReader objects.