0

Using matplotlib it should be possible to animate videos and save them as mpeg. I have found a few tips and tricks on the web but I was not able to get it to work on my Windows 7 machine running Python 3.4. Here are two examples that I found on the web that both give me an exeption 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()

And this one throws the same exeption:

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'])

[EDIT]: Here is the full stack trace:

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
Chris
  • 899
  • 2
  • 17
  • 25
  • 1
    At what line does the error get raised? Have you tried giving your output filename a full path? Error 5 can be related to write permissions. – Martin Evans Jul 08 '15 at 06:52
  • Are you logged in as `wiesmeyrc`? Do you have read permissions on the ffmpeg folder? – SiHa Jul 08 '15 at 06:57
  • @MartinEvans: The error gets gets raised at line 23 in the second example where `anim.save` is called. – Chris Jul 08 '15 at 07:06
  • @SiHa: Yes, i am logged in as `wiesmeyrc` and should therefore have all the permissions for the `ffmpeg` folder. – Chris Jul 08 '15 at 07:07
  • I'd go with Martin Evans' suggestion then - give the output file a full path, you're obviously trying to write it to a location that you don't have write permission to. – SiHa Jul 08 '15 at 07:10
  • I have tried to give a full path... no luck unfortunately. I will include the full stack trace to the original post, it seems to have something to do with subprocesses. I have also tried to run the script as administrator, which should solve the permission issues once and for all as far as I know. – Chris Jul 08 '15 at 07:21
  • Wow... it worked. :) The problem was indeed that i had to give the full path to the ffmpeg.exe file and not just point to where the executable sits. Thanks a lot! – Chris Jul 09 '15 at 07:58

0 Answers0