Matplotlib 动画错误:请求的 MovieWriter (ffmpeg) 不可用,而安装了 ffmpeg



我正在尝试使用 Python 对参数化复杂元素的颜色映射表示进行动画处理。 我逐渐把一些东西放在一起,并检查它们是否正常工作。但是我无法保存动画。

我遇到了这个错误:

Requested MovieWriter (ffmpeg) not available

但是,ffmpeg确实安装在我的系统上,ffmpeg -version返回有关ffmpeg的各种信息。此外,我还使用 pippip install ffmpeg在 Python 脚本目录中安装了 ffmpeg,这很成功。我还在我的代码中设置了ffmepg路径:plt.rcParams['animation.ffmpeg_path'] = "C:FFmpegbinffmpeg.exe"

我的想法快用完了。

这是我的代码。 感谢您的阅读。

import numpy as np
import math
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation
pi=math.pi
plt.rcParams['animation.ffmpeg_path'] = "C:FFmpegbinffmpeg.exe"
fig = plt.figure()
def complex_array_to_rgb(X, theme='dark', rmax=None):
absmax = rmax or np.abs(X).max()
Y = np.zeros(X.shape + (3,), dtype='float')
Y[..., 0] = np.angle(X) / (2 * pi) % 1
if theme == 'light':
Y[..., 1] = np.clip(np.abs(X) / absmax, 0, 1)
Y[..., 2] = 1
elif theme == 'dark':
Y[..., 1] = 1
Y[..., 2] = np.clip(np.abs(X) / absmax, 0, 1)
Y = matplotlib.colors.hsv_to_rgb(Y)
return Y
# Set up formatting for the movie files
Writer = animation.writers['ffmpeg']
writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800)
fps = 10
nSeconds = 1
snapshots = [ complex_array_to_rgb(np.array([[3*(x + 1j*y)**(2.9+p/300) + 1/(x + 1j*y)**2 for x in np.arange(-1,1,0.05)] for y in np.arange(-1,1,0.05)])) for p in range( nSeconds * fps ) ]
fig = plt.figure( figsize=(3,3) )
Z2=snapshots[0]
im=plt.imshow(Z2, extent=(-1,1,-1,1))
def animate_func(i):
if i % fps == 0:
print( '.')
im.set_array(snapshots[i])
return [im]

anim = animation.FuncAnimation(
fig, 
animate_func, 
frames = nSeconds * fps,
interval = 1000 / fps, # in ms
)
anim.save('test_anim.mp4', writer=writer)

Python 在字符串中使用反斜杠作为转义字符,因此这些会弄乱您的文件路径。尝试使用其中之一

plt.rcParams['animation.ffmpeg_path'] = "C:/FFmpeg/bin/ffmpeg"

或者,有点混乱

plt.rcParams['animation.ffmpeg_path'] = "C:\FFmpeg\bin\ffmpeg"

如果这也不起作用,您可以尝试直接使用 ffmpeg 编写器类:

FFwriter = animation.FFMpegWriter()

使用 Windows10 和 JupyterLab

我很久以前做过一些动画,效果很好......但是几天前我使用了同一台笔记本电脑/计算机,但它失败了,给出了错误

RuntimeError: Requested MovieWriter (ffmpeg) not available

当然,FFmpeg 就在那里!搜索后,我发现不止一个,与其他软件(OBS,Shotcut等(相关联。

在尝试了很多事情之后,唯一有效的方法是下载一个构建(https://www.gyan.dev/ffmpeg/builds/(并在笔记本中添加这样的地址(包括"ffmpeg.exe"(

plt.rcParams['animation.ffmpeg_path'] = r'c:FFMPEGffmpeg-2021-10-14-git-c336c7a9d7-full_buildbinffmpeg.exe'

最新更新