writeVideo

Class: VideoWriter

Write video data to file

Syntax

writeVideo(writerObj,img)
writeVideo(writerObj,images)
writeVideo(writerObj,frame)
writeVideo(writerObj,mov)

Description

writeVideo(writerObj,img) writes data from an image to a video file.

writeVideo(writerObj,images) writes a sequence of color images to a video file.

writeVideo(writerObj,frame) writes a frame to the video file associated with writerObj.

writeVideo(writerObj,mov) writes a MATLAB® movie to a video file. mov is an array of frame structures.

You must call open(writerObj) before calling writeVideo.

Input Arguments

writerObj

VideoWriter object created by the VideoWriter function.

img

When creating AVI or MPEG-4 files, img is an array of single, double, or uint8 values representing a grayscale or RGB color image, which writeVideo writes as an RGB video frame. Data of type single or double must be in the range [0,1], except when writing Indexed AVI files.

When creating Motion JPEG 2000 files, img is an array of uint8, int8, uint16, or int16 values representing a monochrome or RGB color image.

For grayscale, monochrome, or indexed data, img must be two-dimensional: height-by-width. For color data that is not indexed, img is three-dimensional: height-by-width-by-3. The height and width must be consistent for all frames within a file. For more information, see Image Types.

images

Four-dimensional array of grayscale (height-by-width-by-1-by-frames) or RGB (height-by-width-by-3-by-frames) images.

frame

Structure typically returned by the getframe function that contains two fields: cdata and colormap. If the colormap is not empty, writeVideo expects a two-dimensional (height-by-width) array cdata. The height and width must be consistent for all frames within a file. colormap can contain a maximum of 256 entries.

The profile of writerObj and the size of cdata determine how the writeVideo method uses frame.

profile of VideoWriter objectSize of cdataBehavior of writeVideo

'Indexed AVI' or 'Grayscale AVI'

2-dimensional (height-by-width)Use frame as provided. For 'Grayscale AVI', colormap should be empty.
3-dimensional (height-by-width-by-3)Error

All other profiles

2-dimensional (height-by-width)Construct RGB image frames using the colormap field
3-dimensional (height-by-width-by-3)Colormap field ignored. Construct RGB image frames using the cdata field

mov

1-by-F array of frame structures, where F is the number of images. Each frame structure contains fields cdata and colormap.

Examples

expand all

AVI File from Animation

Write a sequence of frames to a compressed AVI file, peaks.avi.

Prepare the new file.

writerObj = VideoWriter('peaks.avi');
open(writerObj);

Generate initial data and set axes and figure properties.

Z = peaks; surf(Z); 
axis tight
set(gca,'nextplot','replacechildren');
set(gcf,'Renderer','zbuffer');

Setting the Renderer property to zbuffer or Painters works around limitations of getframe with the OpenGL® renderer on some Windows® systems.

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(writerObj,frame);
end

close(writerObj);

MPG to AVI Conversion

Convert an example file, xylophone.mp4, to an uncompressed AVI file:

Create objects to read and write the video, and open the AVI file for writing.

readerObj = VideoReader('xylophone.mp4');
writerObj = VideoWriter('transcoded_xylophone.avi', ...
                        'Uncompressed AVI');

open(writerObj);

Read and write each frame.

for k = 1:readerObj.NumberOfFrames
   img = read(readerObj,k);
   writeVideo(writerObj,img);
end

close(writerObj);
Was this topic helpful?