How to make scientific movies with ffmpeg

What is ffmpeg?

ffmpeg was designed as a sort of universal converter for various forms of media. Although not its primary purpose, it includes the capability to quickly sew togteher a sequence of images into a movie, a common task in scientific applications. Its flexibility and the number of formats it can handle, as well as its speed, make it (in my opinion) a better choice than mpeg_encode, which is in any case no longer supported by Berkeley on any site that I can find. For a full description of ffmpeg's capabilities see the project website. You can also download the latest source code there or a version snapshot through subversion; they have not yet made a formal release but the movie-from-images bit is fairly stable.

How do I make a movie from a set of image files?

Because this function isn't really the primary purpose of the software, it's not well documented at the website. The following command will make an mpeg4 movie out of gif files (the order of the flags is important):

ffmpeg -f image2 -r 10 -i ./img%d.gif -b 600k ./out.mp4

The flags are as follows:

-f format

indicates the input format. Since we have a series of images the correct input format is "image2", not "gif" (which apparently means an animated gif file, at least to my best guess). If you're sewing together stills into a movie, the -f flag should always be image2. The image type is inferred from the filename extension.

-r fps

indicates how many frames per second in the final movie. This is not supported for some output formats, as discussed below.

-i infiles

the files to be converted. The wildcards are C formatting strings, so you can specify the filename pattern in any form C can spit out (useful since computer output is often the basis for the images). If you have filenames padded with zeros to keep the filename length constant, for example, you can use the same C formatting strings to indicate that; e.g. %03d.

ffmpeg will accept various kinds of image files; running ffmpeg -formats will supposedly list them, but the list is really geared towards video formats, so a lot of the image formats it will take are grouped under "image2". The most efficient way to see if it will take the format you have is just to try it and see if it works. I've used .gif, .pbm, .ppm, and .png images to make movies with success; some formats make better movies than others and sometimes adjusting the frame rate by a few clicks will improve the quality.

The files should be numbered consecutively; otherwise the program will assume, at the first gap in the sequence, that the sequence is over and terminate the encoding.

-b bitrate

the bitrate for the final movie. Apparently since the bit rate is an output option it has to follow the input file flag. Don't ask me why the frame rate is not an output option...

outfile

the file name to write to (the only thing that doesn't have a flag associated with it). The program infers the format of the output from the extension on the filename. The most flexible choices are the mpeg class; for more portability use .mpg instead of .mp4 and remove the frame rate and bitrate flags (basic MPEG doesn't support them). There is a discussion of movie formats compatible with various players and platforms on the ffmpeg website. If your movie just has images without sound, you can ignore specifications about the audio in the commands on this page.