What is FFMPEG?

FFMPEG is an open-source, free piece of software that is very widely used in the audio-visual world to convert digital content from one format to another. It usually sits in the background on your hard drive and gets "called" by other software to do the hard work behind the scenes. This is what we can do from Stata and R: once we have assembled all the individual frames we want for our animation, we will issue an order to FFMPEG to pull them all together into a video file. We can specify the size of the video (although it is better to do that when you save the graphs as image files, to keep the sharpest images), we can set the bitrate to balance better quality against a smaller video file, and we can choose the video format.

Let's take a closer look at an instruction sent from Stata to FFMPEG:

winexec "C:\Program Files\ffmpeg\bin\ffmpeg.exe" ///
-report -i "C:\animation\graph%d.png" -b:v 2048k ///

This command called winexec first looks up the FFMPEG executable file (note that you might well confuse Windows PCs if you use forward slashes), then there are series of options which are passed to FFMPEG that tell it what you want it to do:

-report asks for a log file to be saved, which is handy in case things don't work out the way you expected

-i is followed by the input files (the images); here we have saved the graphs as graph0.png, graph1.png, graph2.png, and so on. That is what the %d tells FFMPEG to look for, and it will combine all those files into one video until it doesn't find the next graph in the sequaence of numbers (so make sure there are no breaks in the numbering)

-b:v means that you want the output to be a video

2048k means you want 2 megabit per second quality, which is reasonably pleasant to look at on screen

...then the instructions finish with the address and file name that you want to save.

The R code is very similar:

shell("C:\Program Files\ffmpeg\bin\ffmpeg.exe -report -i graph%d.png -b:v 2048k new_video.mpg",mustWork=FALSE)

FFMPEG works out from the file extensions (in this case, .png and .mpg) what input and output formats you want to use.

How do I get it?

Head over to the FFMPEG Download pages which will point you in the right direction. If you have Linux, you might already have FFMPEG installed with the OS. For Mac OS X, take a look at this helpful website. If you are using Windows, you can just click here to get the December 2015 Windows 32-bit files as a 7Zip compressed file or click here to get the Dec 2015 64-bit version. You should unzip these into somewhere sensible like Program Files\ffmpeg but if you don't have administrator privileges to save in that folder, you can run FFMPEG from anywhere - even a memory stick - you just have to point your data analysis software to the right place.