【问题标题】:Can't save matplotlib animation无法保存 matplotlib 动画
【发布时间】:2014-07-14 10:50:57
【问题描述】:

我正在尝试使用 ffmpeg 保存一个简单的动画。我按照教程安装 ffmpeg,现在可以从命令提示符访问它。

现在我运行这段代码:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

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)

mywriter = animation.FFMpegWriter()
anim.save('mymovie.mp4',writer=mywriter)

plt.show()

我收到此错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
   File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 523, in runfile
    execfile(filename, namespace)
  File "C:\Users\Renger\.xy\startups\b.py", line 23, in <module>
    anim.save('mymovie.mp4',writer=mywriter)
  File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 609, in save
    with writer.saving(self._fig, filename, dpi):
  File "C:\Python27\lib\contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 166, in saving
    self.setup(*args)
  File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 156, in setup
    self._run()
  File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 183, in _run
    stdin=subprocess.PIPE)
  File "C:\Python27\lib\subprocess.py", line 711, in __init__
    errread, errwrite)
  File "C:\Python27\lib\subprocess.py", line 948, in _execute_child
    startupinfo)
WindowsError: [Error 2] Het systeem kan het opgegeven bestand niet vinden

最后一句荷兰语的意思是这样的:系统找不到指定的文件。

这些错误是什么意思,我该如何解决?

【问题讨论】:

    标签: python animation matplotlib ffmpeg save


    【解决方案1】:

    在 Windows 上,我必须使用 plt.rcParams['animation.ffmpeg_path'] = 'C:\\mypath' 路径中包含所有双 '\' 且此行必须在之前提及

    from matplotlib import animation
    

    而mypath = ffmpeg.exe在本地磁盘的路径。

    另外,我还在环境变量中添加了ffmpeg路径。

    我花了两天时间,经过大量建议后,我能够解决保存动画的问题。

    谢谢大家的回答。

    【讨论】:

      【解决方案2】:

      对于我在 Windows 上使用的一些动画“动画”:

      plt.rcParams['animation.ffmpeg_path'] ='E:\\Media\\ffmpeg\\bin\\ffmpeg.exe'
      FFwriter = animation.FFMpegWriter()
      anim.save('basic_animation.mp4', writer = FFwriter, fps=30)
      

      文件夹之间的路径应该是\而不是/或\

      【讨论】:

      • 纯代码答案会自动标记为低质量,因此不鼓励在 stackoverflow 上使用。以后请用细节修饰你的答案,并解释为什么它是问题的解决方案。
      • 这可行,但我必须在 FFMpegWriter() 构造函数中传递 fps = 100 参数:FFwriter = animation.FFMpegWriter(fps = 100)
      【解决方案3】:

      你需要指定ffmpeg的路径:

      在我使用的 linux 上:

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

      你显然必须指向你的 windows .exe 而不是 '/usr/bin/ffmpeg'

      如果你没有安装ffmpeg,你可以得到它here

      【讨论】:

      • 起初它仍然无法以这种方式工作,但事实证明我不得不在任何地方使用双斜杠。不知道为什么。
      • 使用 / 斜线或原始字符串将起作用 r'C:...' \ 在 python 中转义字符。
      • 我必须在导入 matplotlib.animation 之前设置 rcParam。可能与 Matplotlib (__version__ == '1.5.2') 的变化或我的 Windows 安装的特殊性有关。
      • 或者您可以将 ffmpeg 可执行文件的路径添加到您的 PATH 变量中。
      • 我在使用 Anaconda 时遇到了这个问题。我跑了which ffmpeg,看到它使用的是Anaconda的ffmpeg。
      【解决方案4】:

      animation.py 中的第 183 行是 subprocess.Popenffmpeg 的调用。 ffmpeg exe 似乎不是 matplotlib 所期望的。

      我的第一次尝试是将ffmpeg 的安装路径(目录)放入windows Path 环境变量中。我猜 animation.py 期望它可以在全球范围内使用(就像在 Linux 下一样)。

      如果这不起作用,我会检查 animation.py 中的 subprocess.Popen 调用,看看它在做什么。您可以断点或调整 matplotlibrc 文件中的 verbose.report 变量以将其吐出。第179行有:

      verbose.report('MovieWriter.run: running command: %s' %
                         ' '.join(command))
      

      【讨论】:

      • 我正在打印 verbose.report,但它只是说“假”。如何获取更多详细信息?
      猜你喜欢
      • 2020-07-12
      • 2014-05-29
      • 2018-06-05
      • 2013-08-03
      • 1970-01-01
      • 2015-07-23
      • 2018-12-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多