Write video data to file
Write an RGB image to a Motion JPEG 2000 file with lossless compression.
Create an array containing data from the sample still image,
A = imread('peppers.png');
VideoWriter object for a new video file. Use the
'Archival' profile to specify a Motion JPEG 2000 file with lossless compression.
v = VideoWriter('myFile','Archival');
Verify the type of video compression for the new file.
ans = 'Motion JPEG 2000'
Open the video file for writing. Then, write the image data in
A to the file.
Close the video file.
Read image and colormap data from the sample indexed image file,
[X,map] = imread('corn.tif');
VideoWriter object for a new indexed AVI file.
v = VideoWriter('myIndexed.avi','Indexed AVI');
Assign the colormap data to the
Colormap property of
v.Colormap = map;
Open the file for writing. After you open the file, you cannot change the properties of
Write the image data in
X to the video file. Then, close the file.
Convert the example file,
to an uncompressed AVI file.
Create objects to read and write the video, and open the AVI file for writing.
reader = VideoReader('xylophone.mp4'); writer = VideoWriter('transcoded_xylophone.avi', ... 'Uncompressed AVI'); writer.FrameRate = reader.FrameRate; open(writer);
Read and write each frame.
while hasFrame(reader) img = readFrame(reader); writeVideo(writer,img); end close(writer);
Write a sequence of frames to a compressed AVI file by generating a sequence of frames, creating a video object for the file to write to, and then writing the frames to the video file.
Setup the axes and figure properties to generate frames for the video.
Z = peaks; surf(Z); axis tight manual set(gca,'nextplot','replacechildren');
Create a video writer object for the output video file and open the object for writing.
v = VideoWriter('peaks.avi'); open(v);
Generate a set of frames, get the frame from the figure, and then write each frame to the file.
for k = 1:20 surf(sin(2*pi*k/20)*Z,Z) frame = getframe(gcf); writeVideo(v,frame); end close(v);
VideoWriter object. Use
VideoWriter to create the
img— Values representing grayscale or RGB color images
Values representing grayscale or RGB color images, specified as a 2-D, 3-D, or 4-D array:
For a single grayscale, monochrome, or indexed image,
be two dimensional: height-by-width
For a single truecolor (RGB) image,
three dimensional: height-by-width-by-3.
For a sequence of grayscale images,
four dimensional:. height-by-width-by-1-by-frames. The height and
width must be consistent for all frames within a file.
For a sequence of RGB images,
four dimensional: height-by-width-by-3-by-frames. The height and width
must be consistent for all frames within a file.
When creating AVI or MPEG-4 files:
img is an array of
uint8 values representing one or more grayscale
or RGB color images, which
writeVideo writes as
one or more RGB video frames.
Data of type
be in the range
[0,1], except when writing indexed
When creating Motion JPEG 2000 files:
img is an array of
int16 values representing one or more monochrome
or RGB color images.
frame— Frame data
Farray of structures
Frame data, specified as a 1-by-1 structure array representing
a single frame, or a 1-by-
F array of structures
representing multiple frames. Each frame contains two fields:
frame array is typically returned by the
colormap is not empty, then each element
cdata should be a 2-D (height-by-width) array.
The height and width must be consistent for all frames within a file.
colormap can contain a maximum of 256 entries.
Each element of
colormap must be in the range
When you create a
VideoWriter object. the
and the size of
cdata determine how
|Size of each element of ||Behavior of |
|2-D (height-by-width)||Use frame as provided.|
|2-D (height-by-width)||Use frame as provided. |
All other profiles
|2-D (height-by-width)||Construct RGB image frames using the |
|3-D (height-by-width-by-3)||Ignore the |