A library for generating animations using Pycairo (the Python version of Cairo) and ffmpeg. Pycairo is used to generate each frame of the animation and then an ffmpeg process is called to merge all those frames into a ProRes (.mov) video file.
- pycairo -
conda install pycairo
orpip install pycairo
* Pillow -conda install pillow
orpip install Pillow
* ffmpeg -brew install ffmpeg
* To use a different version of ffmpeg (A non-Homebrew version), just specify theffmpeg_path
argument when initializing an instance ofVideoWriter
(it should be the path to the ffmpeg bin file that you want to use).
The anim directory contains two classes, Frame
and VideoWriter
.
The Frame
class manages a Cairo surface (the image data) and a Cairo context
(what Cairo uses to update the image data). The Frame
class has methods for
drawing lines, writing text, applying a blur, and clearing parts or all of the
image. A Frame
instance can be passed to a VideoWriter
instance using
video_writer.add_frame(frame)
to write that frame to disk as a PNG image file
in a temporary directory. Once all the frames have been written to disk, call
video_writer.write_video('output_path.mov')
to run the ffmpeg process that
merges all those frames into a video file.
The examples directory contains two examples
(hello_world.py
and
random_walks.py
) which demonstrate how to use the
library.
The video generated by hello_world.py
(converted
to a GIF, the actual video is smoother):
The video generated by random_walks.py
(converted
to a GIF):
Frame
class. I've
only added the methods that I've needed for my personal use cases. But you can
easily extend the class to meet your needs. If you want to extend the class,
but you're not familiar with Pycairo, here's a great tutorial that explains how
Pycairo works: Cairo Tutorial for Python
Programmers
(Note: this link is to an
archived
version of the original since the original
site is currently down).