ValueError:使用ffmpeg对已关闭文件执行I / O操作

时间:2018-03-21 00:06:17

标签: python ffmpeg

我正试图让这个(minimal working example)代码在Anaconda的虚拟环境中编译,我在超级计算集群中设置了这个代码:

import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import matplotlib.animation as manimation

FFMpegWriter = manimation.writers['ffmpeg']
metadata = dict(title='Movie Test', artist='Matplotlib',
            comment='Movie support!')
writer = FFMpegWriter(fps=15, metadata=metadata)

fig = plt.figure()
l, = plt.plot([], [], 'k-o')

plt.xlim(-5, 5)
plt.ylim(-5, 5)

x0, y0 = 0, 0

with writer.saving(fig, "writer_test.mp4", 100):
    for i in range(100):
        x0 += 0.1 * np.random.randn()
        y0 += 0.1 * np.random.randn()
        l.set_data(x0, y0)
        writer.grab_frame()

问题是,这个代码在我的本地机器上运行得非常好(MacOSX) - Anaconda发行版; Python 2.7;相同的matplotlib和numpy版本,我在Anaconda上有ffmpeg;我也在群集上有ffmpeg,虽然与Python上的版本不同(但在我的本地机器上没有这个问题)。当我在群集上运行代码时,我得到:

Traceback (most recent call last):
  File "movie_test.py", line 25, in <module>
    writer.grab_frame()
  File "~/anaconda2/envs/test_movie/lib/python2.7/contextlib.py", line 35, in __exit__
    self.gen.throw(type, value, traceback)
  File "~/anaconda2/envs/test_movie/lib/python2.7/site-packages/matplotlib/animation.py", line 241, in saving
    self.finish()
  File "~/anaconda2/envs/test_movie/lib/python2.7/site-packages/matplotlib/animation.py", line 367, in finish
    self.cleanup()
  File "~/anaconda2/envs/test_movie/lib/python2.7/site-packages/matplotlib/animation.py", line 405, in cleanup
    out, err = self._proc.communicate()
  File "~/anaconda2/envs/test_movie/lib/python2.7/site-packages/subprocess32.py", line 927, in communicate
    stdout, stderr = self._communicate(input, endtime, timeout)
  File "~/anaconda2/envs/test_movie/lib/python2.7/site-packages/subprocess32.py", line 1713, in _communicate
    orig_timeout)
  File "~/anaconda2/envs/test_movie/lib/python2.7/site-packages/subprocess32.py", line 1769, in _communicate_with_poll
    register_and_append(self.stdout, select_POLLIN_POLLPRI)
  File "~/anaconda2/envs/test_movie/lib/python2.7/site-packages/subprocess32.py", line 1748, in register_and_append
    poller.register(file_obj.fileno(), eventmask)
ValueError: I/O operation on closed file

我所做的所有搜索都对应于相对简单的文本写入/输出操作,但不适用于视频。在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

事实证明错误非常小 - 我必须导入群集的matplotlib。我不知道为什么会这样,因为matplotlib已经整合到Python包中。