7

I am trying to get a simple animation saved using ffmpeg. I followed a tutorial to install ffmpeg, and I can now access it from the command prompt.

Now I run this piece of code:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

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)

mywriter = animation.FFMpegWriter()
anim.save('mymovie.mp4',writer=mywriter)

plt.show()

I get this error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
   File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 523, in runfile
    execfile(filename, namespace)
  File "C:\Users\Renger\.xy\startups\b.py", line 23, in <module>
    anim.save('mymovie.mp4',writer=mywriter)
  File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 609, in save
    with writer.saving(self._fig, filename, dpi):
  File "C:\Python27\lib\contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 166, in saving
    self.setup(*args)
  File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 156, in setup
    self._run()
  File "C:\Python27\lib\site-packages\matplotlib\animation.py", line 183, in _run
    stdin=subprocess.PIPE)
  File "C:\Python27\lib\subprocess.py", line 711, in __init__
    errread, errwrite)
  File "C:\Python27\lib\subprocess.py", line 948, in _execute_child
    startupinfo)
WindowsError: [Error 2] Het systeem kan het opgegeven bestand niet vinden

The last dutch sentence does mean something like: The system can't find the specified file.

What do these errors mean, and how can I solve them?

Bach
  • 6,145
  • 7
  • 36
  • 61
renger
  • 775
  • 1
  • 7
  • 11

4 Answers4

15

You need to specify your path to ffmpeg:

On linux I use:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
plt.rcParams['animation.ffmpeg_path'] = '/usr/bin/ffmpeg'

You will obviously have to point to your windows .exe instead of '/usr/bin/ffmpeg'

If you don't have ffmpeg installed, you can get it here

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • At first it still didn't work this way, but it turns out I had to use a double slash everywhere. Don't know why. – renger May 25 '14 at 18:05
  • 2
    using / slash or raw string will work r'C:...' \ escapes characters in python. – Padraic Cunningham May 25 '14 at 18:07
  • 2
    I had to set the `rcParam` before importing `matplotlib.animation`. May be related to changes in Matplotlib (`__version__ == '1.5.2'`) or to peculiarities of my Windows installation. – MB-F Jan 24 '17 at 10:25
  • Or your can add the path of the ffmpeg executable to your PATH variable. – gauteh Mar 21 '17 at 22:02
  • I had this problem using Anaconda. I ran `which ffmpeg` and saw that it was using Anaconda's ffmpeg. – crypdick Jul 19 '18 at 18:00
7

for some animation "anim" i use on windows:

plt.rcParams['animation.ffmpeg_path'] ='E:\\Media\\ffmpeg\\bin\\ffmpeg.exe'
FFwriter = animation.FFMpegWriter()
anim.save('basic_animation.mp4', writer = FFwriter, fps=30)

where path should be with \ \ and not with / or \ between folders

  • 1
    Code-only answers are auto-flagged as low quality and as such are discouraged on stackoverflow. In the future please embellish your answer with details and explain why it is a solution to the question. – tom redfern Nov 19 '14 at 13:33
  • 1
    This works but I had to pass the `fps = 100` argument in `FFMpegWriter()` constructor: `FFwriter = animation.FFMpegWriter(fps = 100)`. – Chupo_cro Aug 02 '18 at 00:35
0

Line 183 in animation.py is the subprocess.Popen call to ffmpeg. It seems that the ffmpeg exe is not where matplotlib expects it to be.

My first attempt would be to put the install path (directory) to ffmpeg into the windows Path environmental variable. I'm guessing that animation.py is expecting it to be available globally (as it would be under Linux).

If that doesn't work, I'd inspect the subprocess.Popen call in animation.py to see exactly what it is doing. You could break point it or adjust the verbose.report variable in your matplotlibrc file to spit it out. Line 179 there is:

verbose.report('MovieWriter.run: running command: %s' %
                   ' '.join(command))
Mark
  • 106,305
  • 20
  • 172
  • 230
0

On Windows, I had to use plt.rcParams['animation.ffmpeg_path'] = 'C:\\mypath' with all double '\' in the path and this line has to be mentioned before

from matplotlib import animation

and mypath = path of ffmpeg.exe on the local disk.

Additionally, I also added the ffmpeg path in the environment variables.

It took me two days and after going through tons of advice I was able to solve this issue of saving animation.

Thank you everyone for all the answers.

CFDace
  • 1