MATLAB: How to save a continuous stream of video to a series of short video files using Image Acquisition Toolbox 1.9 (R14SP3)

Image Acquisition Toolbox

I am acquiring video data from a USB camera. When the movie file size becomes large (on the order of gigabytes), the amount of available RAM is exceeded, so I would like to save the video stream to a series of smaller files and combine them at a later time.

Best Answer

The attached file, get_samples.m, grabs frames from a USB webcam and writes them to MAT files containing 15 frames each. The function initializes a video input object that continuously reads frames into a buffer until a specified number of frames have been acquired. Every 15 frames, the FramesAcquiredFcn callback is executed, saving the first 15 frames in the buffer to a new file.
The length of data that is collected is limited only by your hard drive space. Note that it is possible for the buffer to overrun if frames are read into the buffer faster than they are written to the hard drive. Creating a large number of smaller MAT files (frames per file < 60) and saving with the '-v6' option may alleviate this issue. You can also use the IMAQMEM function to set FrameMemoryLimit closer to AvailVirtual.
You can create several AVI files from the MAT files using the AVIFILE and ADDFRAME functions in MATLAB, and a third-party utility such as VirtualDub (<http://www.virtualdub.org/>) will allow you to create one large AVI file.
Note that if you plan to use the 'get_samples' function in a loop, use 'get_samples_v2' instead. This function should output files to different directories.