COVID-19 visualizations with Stata Part 6: Animations
By: Asjad Naqvi
Tags: Color Scheme, Animation, Covid 19, Stata, Automation

Task 3: Making a video

In the next step, we need to combine the images to make a video. For this step, download the open-source software FFmpeg and click on the relevant operating system icon. The logos are marked below in red, since here the tendency is to click on the large green source code button:

The documentation of FFmpeg is given here including all the commands that be used. FFmpeg is a syntax based software, without a GUI, so understanding all the options and possibilities also requires a good understanding of the commands available.

For Windows, clicking on the link above downloads a zipped file. This file can be extracted anywhere, for example, in some software folder, or even in the COVID19 folder defined above. I just copied it on my D: drive. What is really important is that you know the path of this folder for the next step:

In Stata, we need define two globals to making the scripting easier. One for the directory path of FFmpeg which has to refer to the bin folder:

global ffmpegdir "D:/Software/ffmpeg/bin" global videodir "D:/Programs/Dropbox/Dropbox/PROJECT COVID - MEDIUM/graphs/guide6"

The second global says where the video should be kept. Here I just keep the video folder same as the map images folder.

We can now use the following syntax to generate the video:

! "$ffmpegdir/ffmpeg.exe" -y -framerate 10 -start_number 21915 -i "$videodir/HCI_%02d.png" -c:v libx264 -crf 15 -r 30 "$videodir/Video.mp4"

In this code, several FFmpeg options are used. While we won’t discuss all of these, the key ones are as follows:

! tells Stata to load the Windows command shell

-y is the default DOS command for replacing files (if you learned programming in the 1980s, this would be obvious :))

-framerate 10 says how many frames per second. If this is reduced then the video is slower.

-start_number 21915 says, which is the file number to start with. This should be the number given at the end of the first file.

-i says to read all the files which match the criteria HCI_%02d.png

The remaining options are codecs that generates the video. For example -crf is constant rate factor. Here a lower crf value will give a higher quality. 0 is for lossless animation, but it will also produce a larger file. Usually values in the range of 5–15 are good options.

-r is the output framerate option, which is different from the -framerate command defined earlier. Both can be used to control the speed of the animation.

If the above syntax is correctly specified, and both FFmmpeg and image paths are correct, then a screen should pop up which should show an output like this:

If something is wrong with the syntax, then nothing will happen. At the end of the execution, a file named video.mp4 will be created in the specified folder. Here I will go an additional step and filter it again:

! "$ffmpegdir/ffmpeg.exe" -y -i "$videodir/Video.mp4" -filter "minterpolate='mi_mode=blend:fps=120'" "$videodir/Video_smooth.mp4"

The step above, creates a smooth transition between frames so it looks less jerky. Again this is something to experiment with, but here we get the final video_smooth.mp4 file which looks like this:

The steps explained above, can be applied to any Stata animation, and not just maps. As long as there is a good control over colors, axes, legends, file naming conventions etc., anything can be animated. For other examples, you can also visit my YouTube channel to check out various sample Stata animations.

NOTE: the guide was updated in Dec 2020 to fix some links and make the code a bit more efficient. Therefore the figures were updated but the video file on YouTube remains the same.

Exercise

Try generating a map with another policy variable, try other color schemes, and experiment with different FFmpeg options. Try and also animate a line graph or distributions over time.

Other Stata guides

Part 1: An introduction to data setup and customized graphs

Part 2: Customizing colors schemes

Part 3: Heat plots

Part 4: Maps

Part 5: Stacked area graphs

Part 6: Animations

Part 7: Doubling time graphs I

Part 8: Ridge-line plots (Joy plots)

Part 9: Customized bar graphs

Part 10: Stream graphs

If you enjoy these guides and find them useful, then please like and follow The Stata Guide. Also, please share your visualizations if you use these guides!

About the author

I am an economist by profession and I have been using Stata since 2003. I am currently based in Vienna, Austria where I work at the Vienna University of Economics and Business (WU) and at the International Institute for Applied Systems Analysis (IIASA). You can find my research work on ResearchGate and Google Scholar, and Stata code repository on GitHub. You can follow my COVID-19 related Stata visualizations on my Twitter. I am also featured on the Stata COVID-19 webpage in the visualization and graphics section.

You can connect with me via Medium, Twitter, LinkedIn or simply via email: asjadnaqvi@gmail.com.

My Medium blog for Stata stuff here: The Stata Guide where new awesome content is released regularly. Clap, and/or follow if you like these guides!