9
$\begingroup$

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?

$\endgroup$
5
  • 5
    $\begingroup$ 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). $\endgroup$
    – Szabolcs
    Commented May 28, 2012 at 14:27
  • $\begingroup$ Related: mathematica.stackexchange.com/questions/5594/… $\endgroup$ Commented May 28, 2012 at 14:36
  • $\begingroup$ 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... $\endgroup$
    – Jens
    Commented May 28, 2012 at 15:42
  • $\begingroup$ Thanks. It seems that I should consider using a dedicated tool. (It seems that both parts are bottlenecks.) $\endgroup$
    – Andrew
    Commented May 28, 2012 at 15:44
  • 1
    $\begingroup$ 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). $\endgroup$
    – Jens
    Commented May 28, 2012 at 15:51

1 Answer 1

9
$\begingroup$

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:

  1. When importing GIF animation, how to find the correct list of “DisplayDurations”?
  2. Does Mathematica support variable frame rate for any video format, in analogue of GIF-style “DisplayDurations”?

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.

$\endgroup$
2
  • $\begingroup$ Thanks. Unfortunately, I am not using Mac OS X (I am on Windows XP). $\endgroup$
    – Andrew
    Commented May 28, 2012 at 15:40
  • $\begingroup$ Sir, this was a very useful answer! $\endgroup$ Commented Jun 20, 2012 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.