How to speed up a script writing frames using a 'VideoWriter' object?

10 views (last 30 days)
I am writing a script involving capturing frames of a figure window using the 'getframe' function, and writing each frame using a 'VideoWriter' object to a file.
However, this is quite a slow process if I have many frames to acquire and write. How can I speed it up?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 15 Mar 2023
If many calls to 'getframe' are occurring in succession, it is more efficient to avoid calling the 'writeVideo' function every time a new frame is to be written.
Instead, try using the 'repmat' function to pre-initialize a data structure containing a series of video frame structs returned by 'getframe', and write them all to the file at once using 'writeVideo', like so:
 
>> frameArray = repmat(getframe(figure), 100, 1)
frameArray =
100×1 struct array with fields:
cdata
colormap
This will minimize the amount of calls to the 'writeVideo' function, as well pre-allocate as much space as is needed to store the frames.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!