This plugin enables Python ffmpegio package to output matplotlib's Figure.
To enable it, install along with ffmpegio-core or ffmpegio package:
pip install ffmpegio-core # or ffmpegio if also performing media I/O
pip install ffmpegio-plugin-mpl
The plugin will be automatically loaded whenever ffmpegio package is imported.
Create an MP4 video of Matplotlib's animation example.
import ffmpegio
from matplotlib import pyplot as plt
import numpy as np
fig, ax = plt.subplots()
x = np.arange(0, 2*np.pi, 0.01)
line, = ax.plot(x, np.sin(x))
interval=20 # delay in milliseconds
save_count=50 # number of frames
def animate(i):
line.set_ydata(np.sin(x + i / 50)) # update the data.
return line
with ffmpegio.open(
"output.mp4", # output file name
"wv", # open file in write-video mode
1e3/interval, # framerate in frames/second
pix_fmt="yuv420p", # specify the pixel format (default is yuv444p)
) as f:
for n in range(save_count):
animate(n) # update figure
f.write(fig) # write new frame