Write video files
VideoWriter
objects allow you to
create video files from arrays or MATLAB® movies.
You can use the VideoWriter
function with
a predefined profile to create a VideoWriter
object
with properties tailored to a specific format, such as 'Uncompressed
AVI'
or 'MPEG-4'
. After you create the
object, you can use dot notation to view or modify a particular property:
v = VideoWriter('newfile.mj2','Motion JPEG 2000'); v.CompressionRatio = 3;
Modify property values before opening the video file for writing.
After you call the open
function, you cannot
change the value of a property.
Create VideoWriter
objects using the VideoWriter
function.
ColorChannels
— Number of color channelsThis property is read only.
Number of color channels in each output video frame, specified as a positive integer:
Uncompressed AVI, Motion JPEG AVI, and MPEG-4 files have three color channels.
Indexed and grayscale AVI files have one color channel.
For Motion JPEG 2000 files, the number of channels
depends on the input data to the writeVideo
function:
one for monochrome image data, three for color data.
Data Types: double
Colormap
— Color information for video fileP
-by-3 numeric matrixColor information for video file, specified as a numeric matrix
with three columns and a maximum of 256 rows. Each row in the matrix
defines one color using an RGB triplet. An RGB triplet is a three-element
row vector whose elements specify the intensities of the red, green,
and blue components of the color. The intensities must be in the range [0,1]
.
You can set the colormap explicitly before the call to open
,
or by using the colormap
field of a movie frame
structure at the time of writing the first frame.
The Colormap
property only applies to objects
used for writing indexed AVI files.
Example: colormap(summer(256))
Data Types: double
| uint8
CompressionRatio
— Target compression ratio10
(default) | integer greater than 1Target compression ratio, specified as an integer greater than 1. The compression ratio is the ratio between the number of bytes in the input image and the number of bytes in the compressed image. The video data is compressed as much as possible, up to the specified target.
CompressionRatio
is available only for objects
used for writing Motion JPEG 2000 files. After you call open
,
you cannot change the CompressionRatio
value. If
you previously set LosslessCompression
to true
,
setting CompressionRatio
generates an error.
Example: 5
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Duration
— Duration of output fileThis property is read only.
Duration of the output file in seconds, specified as a scalar value.
Data Types: double
FileFormat
— Type of file to write'avi'
| 'mp4'
| 'mj2'
This property is read only.
Type of file to write, specified as 'avi'
, 'mp4'
,
or 'mj2'
.
Filename
— Name of fileThis property is read only.
Name of file, specified as a character vector.
Data Types: char
FrameCount
— Number of framesThis property is read only.
Number of frames written to the video file, specified as an integer.
Data Types: double
FrameRate
— Rate of video playback30
(default) | positive numberRate of video playback in frames per second, specified as a positive number.
After you call open
, you cannot change the FrameRate
value.
Example: 10
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Height
— Height of each video frameThis property is read only.
Height of each video frame in pixels, specified as a scalar.
The writeVideo
method sets values for Height
and Width
based
on the dimensions of the first frame.
MPEG-4 files require frame dimensions that are divisible by
two. If the input frame height for an MPEG-4 file is not an even number, VideoWriter
pads
the frame with a row of black pixels at the bottom. For MPEG-4 files
on Windows® systems, the allowed value depends on the version
of Windows. For example, on Windows 10 the allowed Height
is
in the range [34,2160]
.
Data Types: double
LosslessCompression
— Lossless compressiontrue
| false
Lossless compression, specified as true
or false
.
The LosslessCompression
property is only available
for objects used for writing Motion JPEG 2000 files.
If LosslessCompression
is true
,
then:
The writeVideo
function writes
data so that the decompressed data is identical to the input data.
VideoWriter
ignores any specified
value for CompressionRatio
.
After you call open
, you cannot change the LosslessCompression
value.
By default, LosslessCompression
is false
for
the 'Motion JPEG 2000'
profile and true
for
the 'Archival'
profile.
Data Types: logical
MJ2BitDepth
— Bit depth for Motion JPEG 2000 files[1,16]
Bit depth for Motion JPEG 2000 files, specified as an integer
in the range [1,16]
. The bit depth is the number
of least-significant bits in the input image data
MJ2BitDepth
is available only for objects
used for writing Motion JPEG 2000 files. If you do not specify a value
before calling the open
method, VideoWriter
sets
the bit depth based on the input data type. For example, if the input
data to writeVideo
is an array of uint8
or int8
values,
then MJ2BitDepth
is 8
.
Example: 8
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Path
— Full path to video fileThis property is read only.
Full path to the video file, specified as a character vector.
Data Types: char
Quality
— Video quality75
(default) | integer in the range [0,100]
Video quality, specified as an integer in the range, [0,100]
.
Higher quality numbers result in higher video quality and larger file
sizes. Lower quality numbers result in lower video quality and smaller
file sizes.
Quality
is available only for objects associated
with the MPEG-4
or Motion JPEG AVI
profile.
After you call open
, you cannot change the Quality
value.
Example: 50
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
VideoBitsPerPixel
— Number of bits per pixelThis property is read only.
Number of bits per pixel in each output video frame, specified as a numeric scalar.
AVI files with truecolor video, Motion JPEG AVI, and MPEG-4 files have 24 bits per pixel (8 bits for each of three color bands).
Indexed and grayscale AVI files have 8 bits per pixel.
For Motion JPEG 2000 files, the number of bits per pixel depends
on the value of MJ2BitDepth
and the number of bands
of image data. For example, if the input data to writeVideo
is
a three-dimensional array of uint16
or int16
values,
the default value of MJ2BitDepth
is 16
,
and VideoBitsPerPixel
is 48
(three
times the bit depth).
Data Types: double
VideoCompressionMethod
— Type of video compression'None'
| 'H.264'
| 'Motion JPEG'
| 'Motion JPEG 2000'
This property is read only.
Type of video compression, specified as 'None'
, 'H.264'
, 'Motion
JPEG'
, or 'Motion JPEG 2000'
.
Data Types: char
VideoFormat
— MATLAB representation of video formatThis property is read only.
MATLAB representation of the video format, specified as a character vector.
For file types other than Motion JPEG 2000 files, VideoFormat
is
one of the following.
Video Format | Value of |
---|---|
Uncompressed AVI, Motion JPEG AVI, or MPEG-4 files | 'RGB24' |
AVI files with indexed video | 'Indexed' |
AVI files with grayscale video | 'Grayscale' |
For Motion JPEG 2000 files, VideoFormat
depends
on the value of MJ2BitDepth
and the format of the
input image data to the writeVideo
method. For
example, if you do not specify the MJ2BitDepth
property, VideoWriter
sets
the format as shown in this table.
Format of Image Data | Value of |
---|---|
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 each video frameThis property is read only.
Width of each video frame in pixels, specified as a numeric
scalar. The writeVideo
function sets values for Height
and Width
based
on the dimensions of the first frame.
MPEG-4 files require frame dimensions that are divisible by
two. If the input frame width for an MPEG-4 file is not an even number,
then VideoWriter
pads the frame with a column of
black pixels along the right side. For MPEG-4 files on Windows systems,
the allowed value depends on the version of Windows. For example,
on Windows 10 the allowed Width
is in the
range [34,3872]
.
Data Types: double
open |
Open file for writing video data |
close |
Close file after writing video data |
writeVideo |
Write video data to file |
VideoWriter.getProfiles |
Profiles and file formats that VideoWriter supports |
Create a VideoWriter
object to write a Motion JPEG AVI file named newfile.avi
.
v = VideoWriter('newfile.avi');
Open the file for writing and write a 300-by-300 matrix of data to the file.
open(v) writeVideo(v,rand(300))
Close the file.
close(v)
Create an array containing data from the sample still image, peppers.png
.
A = imread('peppers.png');
Create a VideoWriter
object for a new uncompressed AVI file for RGB24 video.
v = VideoWriter('newfile.avi','Uncompressed AVI');
Open the file for writing.
open(v)
Write the image in A
to the video file.
writeVideo(v,A)
Close the file.
close(v)
Write a sequence of frames to a compressed
AVI file named peaks.avi
.
Prepare the new file.
v = VideoWriter('peaks.avi');
open(v);
Generate initial data and set axes and figure properties.
Z = peaks; surf(Z); axis tight manual set(gca,'nextplot','replacechildren');
Create a set of frames and write each frame to the file.
for k = 1:20 surf(sin(2*pi*k/20)*Z,Z) frame = getframe; writeVideo(v,frame); end close(v);