matplotlib.animation 错误 - 系统找不到指定的文件

6 投票
1 回答
9634 浏览
提问于 2025-04-17 12:41

我在尝试运行一个简单的动画示例代码时,遇到了一个错误,我不知道该怎么解决。

Traceback (most recent call last):
File "D:/CG/dynamic_image2.py", line 29, in <module>
    ani.save('dynamic_images.mp4')
File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 127, in save
    self._make_movie(filename, fps, codec, frame_prefix)
File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 164, in _make_movie
    stdout=PIPE, stderr=PIPE)
File "C:\Python27\lib\subprocess.py", line 679, in __init__
    errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 893, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

我发现了一些类似的情况(链接1链接2),但我还是不知道怎么解决我的问题……

我使用的是:
Python 2.7.2 | EPD 7.2-2(32位)|(默认,2011年9月14日,11:02:05)[MSC v.1500 32位(Intel)] 在win32上

希望有人能帮帮我!

1 个回答

3

我之前也遇到过类似的问题,但解决方法很简单,如果你只是想看动画的话。你的问题和ani.save('dynamic_images.mp4')有关,这个其实对于动画本身并不是必须的。你可以把这一行注释掉。你的代码崩溃可能是因为缺少安装的编码器(很可能是这个原因)。animation.py里有下面的代码。如果传给_make_movie的参数codec是None,那么就会使用ffmpeg(你可以去网上查一下),这需要你安装并且在你的系统路径中可用。否则,你也可以使用mencoder,但这个同样需要安装并在路径中。

def ffmpeg_cmd(self, fname, fps, codec, frame_prefix):
    # Returns the command line parameters for subprocess to use
    # ffmpeg to create a movie
    return ['ffmpeg', '-y', '-r', str(fps), '-b', '1800k', '-i',
        '%s%%04d.png' % frame_prefix, fname]

def mencoder_cmd(self, fname, fps, codec, frame_prefix):
    # Returns the command line parameters for subprocess to use
    # mencoder to create a movie
    return ['mencoder', 'mf://%s*.png' % frame_prefix, '-mf',
        'type=png:fps=%d' % fps, '-ovc', 'lavc', '-lavcopts',
        'vcodec=%s' % codec, '-oac', 'copy', '-o', fname]

def _make_movie(self, fname, fps, codec, frame_prefix, cmd_gen=None):
    # Uses subprocess to call the program for assembling frames into a
    # movie file.  *cmd_gen* is a callable that generates the sequence
    # of command line arguments from a few configuration options.
    from subprocess import Popen, PIPE
    if cmd_gen is None:
        cmd_gen = self.ffmpeg_cmd
    command = cmd_gen(fname, fps, codec, frame_prefix)
    verbose.report('Animation._make_movie running command: %s'%' '.join(command))
    proc = Popen(command, shell=False,
        stdout=PIPE, stderr=PIPE)
    proc.wait()

撰写回答