使用matplotlib保存视频会导致WinError 5

时间:2015-07-08 06:45:06

标签: python video matplotlib ffmpeg

使用matplotlib可以为视频制作动画并将其另存为mpeg。我在网上找到了一些提示和技巧,但我无法在运行Python 3.4的Windows 7机器上运行它。以下是我在网络上发现的两个例子,它们都给了我一个例外PermissionError: [WinError 5] Permission denied

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
plt.rcParams['animation.ffmpeg_path'] = r'C:\Users\me\Desktop\ffmpeg-latest-win64-static\ffmpeg-20150702-git-03b2b40-win64-static\bin'

metadata = dict(title='Movie Test', artist='Matplotlib',
        comment='Movie support!')
writer = animation.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()

这一个抛出相同的例外:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
plt.rcParams['animation.ffmpeg_path'] = r'C:\Users\wiesmeyrc\Desktop\ffmpeg-latest-win64-static\ffmpeg-20150702-git-03b2b40-win64-static\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'])

[编辑]:这是完整的堆栈跟踪:

Traceback (most recent call last):
  File ".\matplotlib_animation.py", line 37, in <module>
    with writer.saving(fig, r'C:\Users\wiesmeyrc\Documents\Python Scripts\basic_animation.mp4', 100):
  File "C:\Anaconda3\lib\contextlib.py", line 59, in __enter__
    return next(self.gen)
  File "C:\Anaconda3\lib\site-packages\matplotlib\animation.py", line 186, in saving
    self.setup(*args)
  File "C:\Anaconda3\lib\site-packages\matplotlib\animation.py", line 176, in setup
    self._run()
  File "C:\Anaconda3\lib\site-packages\matplotlib\animation.py", line 204, in _run
    creationflags=subprocess_creation_flags)
  File "C:\Anaconda3\lib\subprocess.py", line 858, in __init__
    restore_signals, start_new_session)
  File "C:\Anaconda3\lib\subprocess.py", line 1111, in _execute_child
    startupinfo)
PermissionError: [WinError 5] Zugriff verweigert

0 个答案:

没有答案