helper script to combine images together into an animation with ffmpeg
output.mp4
You need a recent version of the rust compiler. Install instructions can be found here
cargo install --git https://github.com/fluid-Dynamics-Group/animate --force
You must also have ffmpeg
in your $PATH
. It can be installed on debian based
systems with:
sudo apt install ffmpeg
Say you have a folder that contains a bunch of files that you want to animate together.
They are distinguished by a zero-padded index of the file. animate
is a handy
cli tool to parse the correct input arguments to ffmpeg
to animate the files together
into a single output video:
static
├── plot_anim_00001.png
├── plot_anim_00002.png
├── plot_anim_00003.png
├── plot_anim_00004.png
├── plot_anim_00005.png
├── plot_anim_00006.png
├── plot_anim_00007.png
├── plot_anim_00008.png
├── plot_anim_00009.png
├── plot_anim_00010.png
├── plot_anim_00011.png
├── plot_anim_00012.png
...
in the above case, you could animate all the files in the child folder ./static
at 30 fps into output.mp4
with this command:
animate 30 ./output.mp4 folder ./static
be caseful, though. This command expects that only files matching the general pattern will be present. If your folder contains
static
├── plot_anim_00001.png
├── plot_anim_00002.png
├── plot_anim_00003.png
├── plot_anim_00004.png
├── another_file.txt
├── plot_anim_00005.png
├── plot_anim_00006.png
this command will fail to run, since it cannot find a parsing method that works for all files.
to handle the presence of additional files in a directory (such as the above ./another_file.txt
you
can instead use xargs on linux to pass in a list of all files you wish to parse.
animate 30 ./output.mp4 pattern ./static/plot_anim_*.png
if run from a unix shell, this will animate all the plot_anim_xxxxx.png
files together
into ./output.mp4
at 30 fps. Note that the list of files passed in must all belong to the
same base directory, so the following will not work:
animate 30 ./output.mp4 pattern ./static/plot_anim_*.png ./another_dir/plot_anim_*.png
$ animate --help
Animate pictures together into videos with ffmpeg
Usage: animate [OPTIONS] <FRAMERATE> <OUTPUT_PATH> <COMMAND>
Commands:
folder animate all files in a folder together
pattern animate a list of files (usually generated by xargs) together
help Print this message or the help of the given subcommand(s)
Arguments:
<FRAMERATE>
<OUTPUT_PATH>
Options:
-v, --verbose print output of ffmpeg
-h, --help Print help information
-V, --version Print version information
Since this utility only work with zero-padded file names, its worthwhile to mention how to generate these filenames
instead of generating a file name like this:
import matplotlib.pyplot as plt
for i in range(100):
name = f"file_name{i}.png"
plt.savefig(name)
do:
name = f"file_name{i:05}.png"
This way, loop iteration i=1
gets exported as file_name00001.png
There are two ways to zero pad in julia:
instead of
for i in 1:100
name = "file_name_" * string(i) * ".png"
end
do:
name = "file_name_" * lpad(i, 5, "0") * ".png"
or:
using Printf
name = @sprintf "file_name_%05i.png" i
which will generate a file name file_name_00001.png
.