2

I am making a video out of a sequence of plots using VideoWriter. It works mostly OK (after following advice in this SO answer). However, it seems that Matlab tries to render all 3000 frames to screen sequentially after it's done animating, which causes my window manager to freak out and the computer to freeze for a few minutes.

Is there a way to write the video frames directly to disk, bypassing screen rendering? It seems that getframe in writeVideo(vid, getframe(f)) necessarily makes the figure visible; is there a way to avoid that?

Community
  • 1
  • 1
Leo Alekseyev
  • 12,893
  • 5
  • 44
  • 44
  • 1
    Unfortunately I don't think so, though it would be nice. From http://www.mathworks.com/help/techdoc/ref/getframe.html: "Note In situations where MATLAB software is running on a virtual desktop that is not currently visible on your monitor, calls to getframe will complete, but will capture a region on your monitor that corresponds to the position occupied by the figure or axes on the hidden desktop. Therefore, make sure that the window to be captured by getframe exists on the currently active desktop." This seems like a pretty set-in-stone behavior. – tmpearce Mar 02 '12 at 04:41
  • My experience is the same, I too would welcome any undocumented features that would allow this functionality. – macduff Mar 02 '12 at 06:19

3 Answers3

2

If you have only 3000 frames, you can save them as images and make a video out of images using something like ffmpeg. Remember to use lossless format for images, such as PNG.

Daniyar
  • 1,680
  • 2
  • 15
  • 23
1

Don't use get frame, but use im2frame instead

writerObj = VideoWriter('awesomeMovie.mp4', 'MPEG-4');
open(writerObj);
masterFrame = rand(10,10,3);
f = im2frame(masterFrame);
writeVideo(writerObj,f);
ted shultz
  • 11
  • 1
0

Using avifile and addframe will allow you to create a video and not display it to the screen. This seems to be a slower way to do things though.

Here is an example based on the referred post:

mov = avifile('myPeaks2.avi','fps',15);
set(gcf, 'visible', 'off')

for k=1:20
    surf(sin(2*pi*k/20)*Z,Z);
    mov = addframe(mov, gcf);
end
mov = close(mov);

Of course, this method is deprecated, so eventually you won't be able to use it.

erichlf
  • 119
  • 3
  • Since this method is deprecated, what is the preferred way of doing things that hopefully won't sacrifice time? – hyiltiz Aug 21 '17 at 20:47