python - 安装 ffmpeg 时 Matplotlib 动画错误 : Requested MovieWriter (ffmpeg) not available,

标签 python matplotlib animation ffmpeg

我正在尝试使用 Python 对参数复杂函数的颜色映射表示进行动画处理。
我逐渐将一些东西放在一起,并检查它们是否正常工作。但我无法保存动画。
我遇到了这个错误:Requested MovieWriter (ffmpeg) not available但是,我的系统上确实安装了ffmpeg,
在 Windows 控制台 ffmpeg -version返回有关 ffmpeg 的各种信息。另外,我还使用 pip pip install ffmpeg 在 Python 脚本目录中安装了 ffmpeg ,这是成功的。我还在我的代码中设置了 ffmepg 路径:plt.rcParams['animation.ffmpeg_path'] = "C:\FFmpeg\bin\ffmpeg.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:\FFmpeg\bin\ffmpeg.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 writer 类:
FFwriter = animation.FFMpegWriter()

关于python - 安装 ffmpeg 时 Matplotlib 动画错误 : Requested MovieWriter (ffmpeg) not available,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62699992/

相关文章:

python - 用html文档中的元素替换多个字符串

matplotlib - 使用wx.SplitterWindow时如何在wxPython中显示光标坐标

jquery - 使用 css3 进行图像缩放和变换

python - matplotlib 中的动画标题

ios - 在堆栈 View 中动画 subview ?

python - 无法使用 pip 安装 lxml

python - 我们如何使用 BigQueryCreateEmptyTableOperator 创建包含 "Clustered by"列的表?

python - Pandas - 写入 to_csv 时的十进制格式而不是科学格式

python /Matplotlib : colorbar with contourf does not respect the tick labels of custom cmap

python - matplotlib.pyplot.hist 错误的规范属性