2

I have been studying the 1D wave equations and making the animation of the equation. But there are some problems when using the anim.save of animation to save the video file. I have already installed ffmpeg on my computer (a Windows machine) and set the environment variables. But it keeps telling me this:

UserWarning: MovieWriter ffmpeg unavailable
...
RuntimeError: Error creating movie, return code: 4 Try running with --verbose-debug

enter image description here

Here is my code:

from numpy import zeros,linspace,sin,pi
import matplotlib.pyplot as mpl
from matplotlib import animation
mpl.rcParams['animation.ffmpeg_path'] = 'C:\\ffmpeg\\bin\\ffmpeg.exe'

def I(x):
    return sin(2*x*pi/L)

def f(x,t):
    return sin(0.5*x*t)

def solver0(I,f,c,L,n,dt,t):
    # f is a function of x and t, I is a function of x
    x = linspace(0,L,n+1)
    dx = L/float(n)
    if dt <= 0:
        dt = dx/float(c)
    C2 = (c*dt/dx)**2
    dt2 = dt*dt
    up = zeros(n+1)
    u = up.copy()
    um = up.copy()
    for i in range(0,n):
        u[i] = I(x[i])
    for i in range(1,n-1):
        um[i] = u[i]+0.5*C2*(u[i-1] - 2*u[i] + u[i+1]) + dt2*f(x[i],t)
    um[0] = 0
    um[n] = 0    
    while t <= tstop:
        t_old = t
        t += dt
        #update all inner points:
        for i in range(1,n-1):
            up[i] = -um[i] + 2*u[i] + C2*(u[i-1] - 2*u[i] + u[i+1]) + dt2*f(x[i],t_old)    
        #insert boundary conditions:
        up[0] = 0
        up[n] = 0
        #update data structures for next step
        um = u.copy()
        u = up.copy()   
    return u

c = 1.0 
L = 10
n = 100
dt = 0
tstop = 40

fig = mpl.figure()
ax = mpl.axes(xlim=(0,10),ylim=(-1.0,1.0))
line, = ax.plot([],[],lw=2)

def init():
    line.set_data([],[])
    return line,

def animate(t):
    x = linspace(0,L,n+1)
    y = solver0(I, f, c, L, n, dt, t)
    line.set_data(x,y)
    return line,

anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=200, interval=20, blit=True)

anim.save('seawave_1d_ani.mp4',writer='ffmpeg',fps=30)
mpl.show()

I believe the problem is in the part of the animation instead of the three functions above. Please help me find what mistake I have made.

tmdavison
  • 64,360
  • 12
  • 187
  • 165
Richard.L
  • 139
  • 3
  • 14
  • `#FFFFFF`, `1000001` are _codes_, but the script you've posted is __code__. – ForceBru Jan 18 '16 at 16:58
  • Sorry,what do you mean? I don't understand. – Richard.L Jan 18 '16 at 18:56
  • 2
    @Richard.L: I think that was a comment about your use of English - you wrote "here are my codes", where "here is my code" is more correct. I edited it for you. – tmdavison Jan 18 '16 at 19:33
  • @tom:Oh,I see.Thank you very much.My English is so poor. – Richard.L Jan 19 '16 at 11:47
  • 1
    No problem, we all have to start somewhere :) – tmdavison Jan 19 '16 at 11:48
  • This code works correctly on linux and creates the video as expected. I think you may have a problem with the ffmpeg path due to the double slash, e.g. see this question: http://stackoverflow.com/questions/22567785/error-with-double-slash-in-windows-path-in-python – Ed Smith Jan 19 '16 at 18:41
  • @EdSmith: I have tried to replace '\\' with '/',but it didn't work.It shows the 'Runtime Error: Error writing to file' – Richard.L Jan 20 '16 at 08:42

1 Answers1

1

I finally worked it out.It seemed that I missed two important lines. mpl.rcParams['animation.ffmpeg_path'] = 'C:\\ffmpeg\\bin\\ffmpeg.exe' This line is correct.Both '\\' and '/' work in Windows,as far as I've tried. I add mywriter = animation.FFMpegWriter()at the bottom and modify anim.save('seawave_1d_ani.mp4',writer='ffmpeg',fps=30) by anim.save('seawave_1d_ani.mp4',writer=mywriter,fps=30).It worked at last,thanks to Using FFmpeg and IPython. Also grateful to those friends who helped me with this problem.

Community
  • 1
  • 1
Richard.L
  • 139
  • 3
  • 14
  • Setting `writer` of `anim.save()` equal to `animation.FFMpegWriter()` also did the trick for me; i.e. I'm now facing another hurdle. Thanks! – tommy.carstensen Oct 22 '17 at 19:23