How to make and save video (avi) in matlab

I myself study Matlab and I made an animated plot using matlab, now I want to save it as a video file. Tell me how to convert my animation to a video file in matlab. Below is my code

x=[1:2]; for i=1:25, m=randi([3,5]); n=randi([3,5]); y=[mn]; bar(x,y) axis equal A(i) = getframe; end 

matlab version 7.8 R2009a

+6
source share
4 answers

use avifile:

 aviobj = avifile('example.avi','compression','None'); x=[1:2]; for i=1:25, m=randi([3,5]); n=randi([3,5]); y=[mn]; bar(x,y) axis equal aviobj = addframe(aviobj,gcf); drawnow end viobj = close(aviobj) 
+4
source

If Matlab avifile does not work (it may have problems with the codecs of the 64-bit OS), then use mmwrite. http://www.mathworks.com/matlabcentral/fileexchange/15881-mmwrite

It is simple and it works. I used it to create * .wmv files, simply: mmwrite(filename, frames);

Edit: sample code

 % set params fps = 25; n_samples = 5 * fps; filename = 'd:/rand.wmv'; % allocate frames struct fig = figure; f = getframe(fig); mov = struct('frames', repmat(f, n_samples, 1), ... 'times', (1 : n_samples)' / fps, ... 'width', size(f.cdata, 2), ... 'height', size(f.cdata, 1)); % generate frames for k = 1 : n_samples imagesc(rand(100), [0, 1]); drawnow; mov.frames(k) = getframe(fig); end % save (assuming mmwrite.m is in the path) mmwrite(filename, mov); 
+3
source

One way to do this is to print the image onto the image and then stitch the resulting sequence of images in the video. ffmpeg and mencoder are great tools for this. There are several useful resources for describing this if you know the right search terms. I like this one

In mencoder, you can stitch images together with the command:

 mencoder "mf://*.jpg" -mf fps=10 -o test.avi -ovc lavc -lavcopts vcodec=msmpeg4v2:vbitrate=800 
0
source
0
source

Source: https://habr.com/ru/post/923953/


All Articles