Cannot Save Matplotlib Animation With Ffmpeg

Cannot save matplotlib animation with ffmpeg

So it turns out there were two issues.

Issue #1: The path to ffmpeg was wrong. I thought I needed to provide the path to the directory that ffmpeg resides in, but I needed to provide the path all the way to the ffmpeg binary.

Issue #2: Prior to testing out my code to generate videos, I sometimes would import a module with the setting plt.rcParams['savefig.bbox'] = 'tight'. (I did not think much of it, because I did not use the module, but rcParams persist until you restart the python interpreter.) This plt.rcParams['savefig.bbox'] = 'tight' causes the video file to save without any errors, but the frames are all garbled when you try to play the video. Although it took me all evening to track this down, it turns out this is a known issue.

Here is the updated solution that creates a video file for me with a nice, translating, sine wave.

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
plt.rcParams['animation.ffmpeg_path'] = '/opt/local/bin/ffmpeg'

fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))
line, = ax.plot([], [], lw=2)

def init():
line.set_data([], [])
return line,

def animate(i):
x = np.linspace(0, 2, 1000)
y = np.sin(2 * np.pi * (x - 0.01 * i))
line.set_data(x, y)
return line,

anim = animation.FuncAnimation(fig, animate, init_func=init, frames=200, interval=20, blit=True)

FFwriter = animation.FFMpegWriter(fps=30, extra_args=['-vcodec', 'libx264'])
anim.save('basic_animation.mp4', writer=FFwriter)

Can't save matplotlib animation

You need to specify your path to ffmpeg:

On linux I use:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
plt.rcParams['animation.ffmpeg_path'] = '/usr/bin/ffmpeg'

You will obviously have to point to your windows .exe instead of '/usr/bin/ffmpeg'

If you don't have ffmpeg installed, you can get it here

Trouble saving matplotlib animation with ffmpeg

Providing my comment as an answer:

I think you should specify the arguments to FFMpegWriter directly in the initialization of that instance instead of supplying some of them to the animation save method.

FFwriter = animation.FFMpegWriter(fps=30, extra_args=['-vcodec', 'libx264'])
ani.save('basic_animation.mp4', writer = FFwriter)

Matplotlib can not save animation

I figured it out, oddly enough I needed to do this before all my import statements.

import matplotlib
matplotlib.use("Agg")

If I did not have that, it wouldn't work. Also, ffmpeg started taking a while, so I modified the save function to be like this:

anim.save('animation.mp4', progress_callback=lambda i, n: print(f'Saving frame {i} of {n}'))

Kind of a nice hidden feature in the docs. Hope no one else has this issue!

How do I properly enable ffmpeg for matplotlib.animation?

I see no ffmpeg specific info in the output you provided; however you are attempting to use libx264, but your ffmpeg configure is missing --enable-gpl --enable-libx264 which are required to enable encoding with this encoder. You can download a ffmpeg binary for Linux, macOS, or Windows that does support libx264 then point your script to it.

Alternatively, compile ffmpeg using the mentioned configure options.



Related Topics



Leave a reply



Submit