A Matlab tool for creating movies/videos with some advantages over the builtin functions:
- supports more efficient video formats (h264,webm,...)
- circumvents limitations of the builtin getFrame() function
- supports Windows/Linux/OS X (Matlab supports e.g. mpeg4 only on Windows systems)
- install ffmpeg * Windows: static build is available from ffmpeg.org (see windows builds) * OS X: install via homebrew ("brew install ffmpeg") * Linux: most distribution provie a package for ffmpeg
- download or clone the ffmpegWriter repository * The content of the repository has to be in a folder which is in the PATH of Matlab. * In Matlab File->Set Path shows all folders in the PATH and also allows adding additional folder to the PATH.
A small example should be sufficient to demonstrate how to use ffmpegWriter.
fw = ffmpegWriter(); % initialize ffmpegWriter
fw.ffmpeg = '/usr/local/bin/ffmpeg' % set path to ffmpeg on OS X (if installed using homebrew)
% fw.ffmpeg = 'C:\ffmpeg\bin\ffmpeg.exe' % set path to ffmpeg on Windows
% % (if installed to C:\ffmpeg\bin\)
% % setting the path to ffmpeg is not necessary on Linux
x = [1:0.01:pi];
for k=1:20
plot(x,sin(k*x));
fw.getFrame(); % append current figure as a frame to the video
end
fw.writeMovie('sin.mp4',5); % create h264 encoded video with a
% framerate of 5 frames/second and
% write it to sin.mp4
% fw.writeMovie('sin.webm',5); % create vp8/webm encoded video with a
% framerate of 5 frames/second and
% write it to sin.webm
Some additional examples:
fw = ffmpegWriter([640 480]); % dimensions of the final video are set to 640 x 480
fw.getFrame(h); % append current state of figure h to the video
For further information, have a look at the matlab help text supplied with ffmpegWriter (help ffmpegWriter).
The videos created by ffmpegWriter can be included in a LaTeX document with the help of the media9 package.
\usepackage{media9}
...
\includemedia[
activate=pageopen,
width=300pt,
height=200pt,
addresource=sin.mp4, % path to video file
flashvars={%
src=sin.mp4 % use same path as for addresource!
}
]{}{StrobeMediaPlayback.swf}