Ingredients for simple animation: Python, Matplotlib, FFmpeg
Some practical steps of a recipe
Nowadays the knowledge to create animation is necessary, especially when static images does not work well. In the field of story telling, it has ability to engage audiences, simplify complex ideas, and convey emotions makes it an invaluable tool for storytellers across various media (Mellowacademy, 2023). In this story steps to prepare the libraries for creating animation with Python, Matplotlib and FFmpeg are given.
Prerequisites and systems
- It is assumed that Python and some libraries, including Matplotlib, are already installed (Viridi, 2024), but not FFmpeg.
- Hardware used are Lenovo FM ideacentre A430–24IWL with Intel Core i5–10210U CPU @ 1.60 GHz, 4 Cores, 8 Logical Processors, 8.00 GB RAM, while the OS is Windows 11 Home, 64-bit operating system, x64-based processor, version 22H2, OS build 22621.3007.
Download
Visit https://ffmpeg.org/download.html and find the installer for Windows OS (for my case), where one is https://github.com/BtbN/FFmpeg-Builds/releases with ffmpeg-master-latest-win64-gpl.zip as the file.
Keep the installer
Install
Click the ffmpeg-master-latest-win64-gpl.zip
to see its content.
It has a folder named ffmpeg-master-latest-win64-gpl
. You can click it to see its content further.
It show a folder structure
for the binary files and
for the documentation.
Install
Copy ffmpeg-master-latest-win64-gpl
to C:\ffmpeg
.
Press Windows logo ⊞ and type environment variables.
Click Environment Variables… button.
Choose Path and click Edit… button.
Click New and type C:\ffmpeg\bin
.
Press Enter and then click OK. It will close current window and show previous one.
Click OK to close it and another previous one.
Click OK to close the first window. You can open cmd
to check the installation.
It shows that any programs can locate the installed ffmpeg.
Python library
A package name ffmpeg-python
is required to install in providing Python binding to FFmpeg.
Open cmd
, activate related virtual environment, which is tf
for TensorFlow in this case.
Perform pip install ffmpeg-python
. It shows that the package has been installed as the result of pip show ffmpeg-python
.
An example
There are straight forward examples available, e.g. with FFmpeg (agarwalkeshav8399, 2022), without FFmpeg (ujaval, 2022) and both with and without FFmpeg (Liu, 2022).
The first example is available as sine_wave.ipynb as follow
from matplotlib import pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
# initializing a figure in
# which the graph will be plotted
fig = plt.figure()
# marking the x-axis and y-axis
axis = plt.axes(xlim =(0, 4),
ylim =(-2, 2))
# initializing a line variable
line, = axis.plot([], [], lw = 3)
# data which the line will
# contain (x, y)
def init():
line.set_data([], [])
return line,
def animate(i):
x = np.linspace(0, 4, 1000)
# plots a sine graph
y = np.sin(2 * np.pi * (x - 0.01 * i))
line.set_data(x, y)
return line,
anim = FuncAnimation(fig, animate, init_func = init,
frames = 200, interval = 20, blit = True)
anim.save('continuousSineWave.mp4',
writer = 'ffmpeg', fps = 30)
where the produced GIF image is
using 'continuousSineWave.gif'
and writer='pillow'
, and the video
using 'continuousSineWave.mp4'
and writer='ffmpeg'
.
Closing
After reading this story you are able to
- download FFmpeg 64 bit with GPL for Windows,
- install FFmpeg library on computer with Windows 11,
- install
ffmpeg-python
package from PIP for Python binding to FFmpeg, - run example to produce MP4 file,
- modify example to produce GIF file.
Further study might be modification of the given example by adding time indicator, then try to understand the meaning of parameters frames
, interval
, fps
, and observation of the produced video. The time indicator should match time stamp on the video player, locally or on YouTube.
- agarwalkeshav8399, “Using Matplotlib for Animations”, GeeksforGeeks, 4 May 2022, url https://www.geeksforgeeks.org/using-matplotlib-for-animations/ [20240621].
- Mellowacademy, “The Role of Animation in Modern Storytelling”, Medium, 28 Sep 2023, url https://medium.com/p/cf9dfe66c0f6 [20240621].
- Qiaofeng Liu, “Beginners’ Guide to Animate Plots with matplotlib.animation”, Medium, 13 Apr 2022, url https://medium.com/p/3e3c69679c90 [20240621].
- Sparisoma Viridi, “Install Python (again) on Windows 11”, Medium, 9 May 2024, url https://medium.com/p/37a19f179b77 [20240621].
- ujaval, “Creating Animated Plots with Matplotlib”, Spatial Thoughts, 14 Jan 2022, url https://spatialthoughts.com/2022/01/14/animated-plots-with-matplotlib/ [20240621].