无法保存与ffmpeg的matplotlib动画

Str*_*tch 15 python animation matplotlib

我试图从Jake Vanderplas中保存一个简单的matplotlib动画,但我一直在努力OSError: [Errno 13] Permission denied.

我应该注意到,我对Jake Vanderplas的例子进行了两次小修改.我安装FFMPEG从MacPorts的,所以我增加了行plt.rcParams['animation.ffmpeg_path'] = '/opt/local/bin'和我跑进(所讨论的问题使用的FFmpeg和IPython的),所以我加入FFwriter = animation.FFMpegWriter().

这是代码:

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

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()
anim.save('basic_animation.mp4', writer = FFwriter, fps=30, extra_args=['-vcodec', 'libx264'])
Run Code Online (Sandbox Code Playgroud)

这是追溯:

File "ani_debug.py", line 34, in <module>
  anim.save('basic_animation.mp4', writer = FFwriter, fps=30, extra_args=['-vcodec', 'libx264'])
File "/Users/Ben/Library/Enthought/Canopy_64bit/User/lib/python2.7/site- packages/matplotlib/animation.py", line 712, in save
  with writer.saving(self._fig, filename, dpi):
File "/Applications/Canopy.app/appdata/canopy-1.3.0.1715.macosx-x86_64/Canopy.app/Contents/lib/python2.7/contextlib.py", line 17, in __enter__
  return self.gen.next()
File "/Users/Ben/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/animation.py", line 169, in saving
  self.setup(*args)
File "/Users/Ben/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/animation.py", line 159, in setup
  self._run()
File "/Users/Ben/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/animation.py", line 186, in _run
  stdin=subprocess.PIPE)
File "/Applications/Canopy.app/appdata/canopy-1.3.0.1715.macosx-x86_64/Canopy.app/Contents/lib/python2.7/subprocess.py", line 709, in __init__
  errread, errwrite)
File "/Applications/Canopy.app/appdata/canopy-1.3.0.1715.macosx-x86_64/Canopy.app/Contents/lib/python2.7/subprocess.py", line 1326, in _execute_child
  raise child_exception
OSError: [Errno 13] Permission denied
Run Code Online (Sandbox Code Playgroud)

我一直在使用Spyder的内置Python也尝试,并收到了类似的回溯.有什么建议?


编辑:我意识到,我没有给ffmpeg的正确路径.显然,plt.rcParams['animation.ffmpeg_path']不起作用PYTHONPATH.您必须告诉动画模块ffmpeg的确切位置plt.rcParams['animation.ffmpeg_path'] = '/opt/local/bin/ffmpeg'.

现在,我得到一个电影文件,将发挥,但内容完全是乱码.我不能告诉我在看.

这是追溯:

Exception in Tkinter callback
Traceback (most recent call last):
  File "Tkinter.pyc", line 1470, in __call__
  File "Tkinter.pyc", line 531, in callit
  File "/Applications/Spyder.app/Contents/Resources/lib/python2.7/matplotlib/backends/backend_tkagg.py", line 141, in _on_timer
    TimerBase._on_timer(self)
  File "/Applications/Spyder.app/Contents/Resources/lib/python2.7/matplotlib/backend_bases.py", line 1203, in _on_timer
    ret = func(*args, **kwargs)
  File "/Applications/Spyder.app/Contents/Resources/lib/python2.7/matplotlib/animation.py", line 876, in _step
    still_going = Animation._step(self, *args)
  File "/Applications/Spyder.app/Contents/Resources/lib/python2.7/matplotlib/animation.py", line 735, in _step
    self._draw_next_frame(framedata, self._blit)
  File "/Applications/Spyder.app/Contents/Resources/lib/python2.7/matplotlib/animation.py", line 753, in _draw_next_frame
    self._pre_draw(framedata, blit)
  File "/Applications/Spyder.app/Contents/Resources/lib/python2.7/matplotlib/animation.py", line 766, in _pre_draw
    self._blit_clear(self._drawn_artists, self._blit_cache)
  File "/Applications/Spyder.app/Contents/Resources/lib/python2.7/matplotlib/animation.py", line 806, in _blit_clear
    a.figure.canvas.restore_region(bg_cache[a])
KeyError: <matplotlib.axes.AxesSubplot object at 0x104cfb150>
Run Code Online (Sandbox Code Playgroud)

编辑:出于某种原因,一切正在工作的罚款.我已经尽了家里的电脑和我的工作电脑上的东西,并没有一个可以重新创建乱码的视频文件我后,我固定的ffmpeg的路径问题.


编辑:Aaaahaaa!我跟踪这个傻逼了.有时我会导入模块,有plt.rcParams['savefig.bbox'] = 'tight'在里面.(我永远不会使用这个模块,但rcParams坚持,直到你重新启动你的Python解释器.)该设置会导致视频出来全是乱码.下面我会后我的解决方案.

Str*_*tch 16

事实证明,有两个问题.

问题#1:ffmpeg的路径是错误的.我以为我需要提供ffmpeg所在目录的路径,但是我需要提供一直到ffmpeg二进制文件的路径.

问题2:在测试我的代码以生成视频之前,我有时会导入一个带有该设置的模块plt.rcParams['savefig.bbox'] = 'tight'.(我没有想太多,因为我没有使用该模块,但是rcParams会一直存在,直到你重新启动python解释器.)这plt.rcParams['savefig.bbox'] = 'tight'会导致视频文件保存而没有任何错误,但是当你尝试播放时帧都是乱码该视频.虽然我整晚都在跟踪它,但事实证明这是一个众所周知的问题.

这是更新的解决方案,为我创建一个视频文件,具有漂亮的翻译正弦波.

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()
anim.save('basic_animation.mp4', writer = FFwriter, fps=30, extra_args=['-vcodec', 'libx264'])
Run Code Online (Sandbox Code Playgroud)