ValueError:制作动画时对关闭的文件进行I / O操作

时间:2018-06-28 21:30:27

标签: python animation matplotlib ffmpeg

我正在使用matplotlib和ffmpeg做一些动画。我通常在远程服务器上编写代码,因为代码运行速度更快。我们在远程服务器上制作动画时遇到一些问题。这是一个代码示例,可以在我的本地Mac上完美运行,但不能远程运行。

import matplotlib as mpl
mpl.use('agg')
import matplotlib as mpl 
from matplotlib import animation
import pylab

def init():
    pylab.plot(pylab.arange(10), [0]*10)

def redraw(frame):
    pylab.plot(pylab.arange(10), pylab.arange(10) * frame)

fig = pylab.figure()
ani = animation.FuncAnimation(fig, redraw, frames=10, interval=1000, init_func=init)
ani.save('animation.mp4')

我在本地计算机(macOS Sierra)上获得了想要的动画。当我在远程主机(Debian GNU / Linux 8(jessie))上运行它时,在5帧后我收到以下错误消息

Traceback (most recent call last):
  File "animation.py", line 14, in <module>
    ani.save('animation.mp4')
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/animation.py", line 1200, in save
    writer.grab_frame(**savefig_kwargs)
  File "/usr/lib/python2.7/contextlib.py", line 35, in __exit__
    self.gen.throw(type, value, traceback)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/animation.py", line 241, in saving
    self.finish()
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/animation.py", line 367, in finish
    self.cleanup()
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/animation.py", line 405, in cleanup
    out, err = self._proc.communicate()
  File "/usr/local/lib/python2.7/dist-packages/subprocess32.py", line 724, in communicate
    stdout, stderr = self._communicate(input, endtime, timeout)
  File "/usr/local/lib/python2.7/dist-packages/subprocess32.py", line 1535, in _communicate
    orig_timeout)
  File "/usr/local/lib/python2.7/dist-packages/subprocess32.py", line 1591, in _communicate_with_poll
    register_and_append(self.stdout, select_POLLIN_POLLPRI)
  File "/usr/local/lib/python2.7/dist-packages/subprocess32.py", line 1570, in register_and_append
    poller.register(file_obj.fileno(), eventmask)
ValueError: I/O operation on closed file

我的本​​地计算机使用matplotlib版本2.0.0;远程计算机使用matplotlib版本2.2.2

在我的本地计算机上,我有ffmpeg版本3.2.4

$ ffmpeg -version
ffmpeg version 3.2.4 Copyright (c) 2000-2017 the FFmpeg developers
built with Apple LLVM version 8.0.0 (clang-800.0.42.1)
configuration: --prefix=/usr/local/Cellar/ffmpeg/3.2.4 --enable-shared - 
-enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables 
--enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable libmp3lame --enable-libx264 --enable-libxvid --enable-opencl --disable-lzma --enable-vda
libavutil      55. 34.101 / 55. 34.101
libavcodec     57. 64.101 / 57. 64.101
libavformat    57. 56.101 / 57. 56.101
libavdevice    57.  1.100 / 57.  1.100
libavfilter     6. 65.100 /  6. 65.100
libavresample   3.  1.  0 /  3.  1.  0
libswscale      4.  2.100 /  4.  2.100
libswresample   2.  3.100 /  2.  3.100
libpostproc    54.  1.100 / 54.  1.100 

在远程主机上,我具有ffmpeg版本4.0.1

ffmpeg -version
ffmpeg version 4.0.1 Copyright (c) 2000-2018 the FFmpeg developers
built with gcc 4.9.2 (Debian 4.9.2-10+deb8u1)
configuration: --prefix=/usr/local
libavutil      56. 14.100 / 56. 14.100
libavcodec     58. 18.100 / 58. 18.100
libavformat    58. 12.100 / 58. 12.100
libavdevice    58.  3.100 / 58.  3.100
libavfilter     7. 16.100 /  7. 16.100
libswscale      5.  1.100 /  5.  1.100
libswresample   3.  1.100 /  3.  1.100

如果我没记错的话,我是通过自制软件在本地安装ffmpeg的;我有蟒蛇的蟒蛇分布。在远程计算机上,我们拥有Jessie随附的python的默认版本。我不确定sysadmin如何安装ffmpeg。

我绝不是ffmpeg方面的专家,但是我通常在本地计算机上使用matplotlib制作动画时从未遇到过问题,我非常希望能够在远程计算机上更快地制作视频。任何帮助将不胜感激!

修改 在远程计算机上,如果我使用avconv而不是ffmpeg作为编写器,则动画可以工作。我在本地安装了avconv ...导致我在本地遇到相同的ffmpeg问题(可能是由于更新了共享依赖项)。但是,我卸载了ffmpeg并使用x264编解码器将其重新安装,从而启用了Animations in ipython (jupyter) notebook - ValueError: I/O operation on closed file

0 个答案:

没有答案