QTWriter

Export QuickTime Movies with Matlab

Version 1.1, 5-14-14

View the Project on GitHub horchler/QTWriter

QTWriter

Export QuickTime Movies with Matlab

OBJ = QTWriter(FILENAME) constructs a QTWriter object to write QuickTime movie data to a “.mov” file using lossless Photo PNG compression. FILENAME is a string enclosed in single quotation marks that specifies the name of the file to create. If FILENAME does not include an extension the QTWriter constructor appends the “.mov” extension. FILENAME can include an absolute or relative path.

OBJ = QTWriter(...,'PropertyName','PropertyValue',...) specifies options via property name-value pairs. In addition to the default 'Photo PNG' compression, 'Photo TIFF' and 'Photo JPEG' formats can be specified via the 'MovieFormat' property. Lossless Photo TIFF compression yields larger file sizes than Photo PNG, but is faster (using 'LZW' or the default 'PackBits' for the 'CompressionType' property). Photo JPEG is a lossy format and the 'Quality' property can be used to specify the level of compression to use. The 'ColorSpace' property specifies if the movie is to be output as 24-bit RGB truecolor or 8-bit grayscale. The 'Transparency' property indicates if the movie is to be output with an alpha channel (Photo PNG format only).

Frames are written via the writeMovie(FRAME) method of the QTWriter object. The frame rate of the movie can be continuously varied via the 'FrameRate' property of the QTWriter object (see strpenddemo). The looping behavior of the output movie can be specified via the 'Loop' property of the QTWriter object: 'none', 'loop', or 'backandforth'. Finally, the output movie can be forced to play every frame via the 'PlayAllFrames' property of the QTWriter object. The close() method of the QTWriter object is called to finish writing the movie and clean up associated data.

Please refer to the detailed help included within QTWriter for further details and options.

Example:

% Prepare new movie file using the default PNG compression
movObj = QTWriter('peaks.mov');

% Create an animation
hf = figure; Z = peaks; surfc(Z); frames = 100;
axis tight; set(hf,'DoubleBuffer','on');
set(gca,'nextplot','replacechildren');
     
% Animate plot and write movie
for k = 0:frames
    hs = surfc(sin(2*pi*k/frames)*Z,Z);
    set(hs,'FaceColor','interp','FaceLighting','phong');
    light('Position',[0 0 4]);
    
    % Vary the frame-rate
    movObj.FrameRate = k;
    
    % Write each frame to the file
    writeMovie(movObj,getframe(hf));
end

% Set palindromic looping flag
movObj.Loop = 'backandforth';

% Finish writing movie and close file
close(movObj);

Example output:

(Download Movie, 5.1 MB QuickTime)

The included demo, strpenddemo, provides a more-detailed usage example. The M-file simulates and animates a string pendulum hanging between two cylinders and generates this variable frame-rate movie using QTWriter:


(Download Movie, 1.5 MB QuickTime)


Also included is getframebg, a faster version of Matlab's getframe. It captures figures in the background without rendering content to the screen, making it faster, avoiding capture of overlapping windows, and allowing the use of screensavers.



Copyright © Andrew D. Horchler, horchler @ gmail . com
Created: 10-3-11, Revision: 1.1, 5-14-14

QTWriter is inspired by MakeQTMovie by Malcolm Slaney (Interval Research, March 1999) and parts are based on the VideoWriter class in Matlab R2011b.

This version tested with Matlab 8.1.0.604 (R2016a)
Mac OS X 10.11.4 Build: 15E65, Java 1.7.0_75-b13
QuickTime Player Pro 7.6.6 (1709), QuickTime Version 7.7.3 (2943.3), and QuickTime Player X 10.4 (855)
Compatibility maintained back through Matlab 7.4 (R2007a)

Copyright © 2012–2017, Andrew D. Horchler
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANDREW D. HORCHLER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.