You must call open(writerObj) before calling writeVideo.
VideoWriter object created by the VideoWriter function.
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.
Four-dimensional array of grayscale (height-by-width-by-1-by-frames) or RGB (height-by-width-by-3-by-frames) images.
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.
1-by-F array of frame structures, where F is the number of images. Each frame structure contains fields cdata and colormap.
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);
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);