# Efficiently exporting many images as a video

I have many .gif images and I would like to export them as a single video file (I prefer .mov, but any video format is fine). But what if I have several tens of megabytes of .gif images? Is it possible to export them efficiently, without running out of memory?

For example, suppose I have many .gif files in the current directory. One way to compile them into a .mov video is to import all the images at once, in a single table, and then call Export:

files = FileNames["*.gif"];
images = Map[Import, files];
Export["video.mov", images]


But, for several tens of megabytes of .gif images, pre-importing all the images in a list (i.e., images) may be impossible because of insufficient memory. Is there any way to work around this?

-
Sorry to say this, but I really think using a dedicated tool like ffmpeg or mencoder will be a better solution ... (it might still be painful to set the options but at least you won't run out of memory). –  Szabolcs May 28 '12 at 14:27
–  Sjoerd C. de Vries May 28 '12 at 14:36
Maybe one could break this problem up by identifying which is the real bottleneck: the import stage where you're trying to read in the gifs, or the output stage where you're creating the movie? Of course it could be that both are equally bad... –  Jens May 28 '12 at 15:42
Thanks. It seems that I should consider using a dedicated tool. (It seems that both parts are bottlenecks.) –  Andrew May 28 '12 at 15:44
Yes, I think so too. I added a link (to my answer) with some stuff I wrote earlier that may help (in particular ImageMagick convert). –  Jens May 28 '12 at 15:51

In addition to the memory (and slowness) problem, this question combines two other issues that I've thought about before. The problems only arise if the gif files are animations, but the solution could be similar:

In the second post, I answered my own question by writing an export function that assembles the movie from a sequence of frames, but outside of Mathematica. This by itself could already help with the memory problem:

Assuming you're on Mac OS X and you manage to get all the frames into a Mathematica list called images, you could use the function exportMov I defined in my answer

exportMov["video.mov", images]


On that page I also have a link to a Python utility that can be used to achieve the same thing from the command line completely without Mathematica and relying only on built in Mac OS X functionality - but that's off-topic for this forum.

If you want to use an external tool, you may want to look at this page where I describe how to use convert.

Edit

One more thing that could make or break any attempt to even import the images into Mathematica is this (your question already does this, but I think it bears repeating): Always end your Import statement with a semicolon to suppress the display of the image frames. Otherwise, with a large number of images, the notebook interface will be busy drawing them for what feels like an eternity.

-
Thanks. Unfortunately, I am not using Mac OS X (I am on Windows XP). –  Andrew May 28 '12 at 15:40
Sir, this was a very useful answer! –  El Developer Jun 20 '12 at 16:47