2014-09-22 138 views
2

我目前正在试验matplotbib FuncAnimation并尝试一些exmaples。 Everthing运行良好,但是,我通过带有anim.save(...)的ffmpeg制作视频,并且我没有让我的动画播放速度更快/更慢。无论是改变matplotlib FuncAnimation帧间隔

FuncAnimation(...,interval=x,...) 

也不

anim.save(...,fps=x.) 

对视频输出有任何影响。两者之间有什么区别('帧'/'fps'应该是'间隔',否?)?这里我简化代码:

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

class Ball: 

    def __init__(self,initpos,initvel,radius,M): 
     self.pos=initpos 
     self.vel=initvel 
     self.radius=radius 
     self.M=M 


    def step(self,dt): 

     self.pos += self.vel*dt 
     self.vel[2] += -9.81*dt*self.M 

initpos=np.array([5.0,5.0,10.0]) 
initvel=np.array([0.0,0.0,0.0]) 
radius=0.25 
M=1 


test_ball = Ball(initpos,initvel,radius,M) 
dt=1./10 

fig = plt.figure() 
ax = fig.add_axes([0, 0, 1, 1], projection='3d') 

pts = [] 
pts += ax.plot([], [], [], 'bo', c="blue") 

def init(): 
    for pt in pts: 
    pt.set_data([], []) 
    pt.set_3d_properties([]) 
    return pts 

def animate(i): 
test_ball.step(dt) 
for pt in pts: 
    pt.set_data(test_ball.pos[0],test_ball.pos[1]) 
    pt.set_3d_properties(test_ball.pos[2]) 
    pt.set_markersize(10) 
    return pts 

anim = animation.FuncAnimation(fig, animate,init_func=init,frames=50,interval=1) 


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

希望有人能帮助我。非常感谢。

PS:顺便说一下,我不知道还有

  1. 初始化funcion做什么,因为代码也完全不运行它,但它是在大多数INET-例子。
  2. 什么标记是一个ax.plot(...,'o',...)点。单位的半径?

回答

0

如果您想减慢动画速度,可以在动画函数中使用time.sleep()。我也有一些间歇参数的问题。动画的速度应该取决于间隔参数,而不是fps。

anim.save(...,fps=x.) 

这将决定要保存的视频的质量,即帧在视频中的渲染速率。

1

这是一个工作示例。

from mpl_toolkits.mplot3d import Axes3D 
import matplotlib.animation as animation 
import matplotlib.pyplot as plt 
import numpy as np 


class Ball: 

    def __init__(self, initpos, initvel, radius, M): 
     self.pos = initpos 
     self.vel = initvel 
     self.radius = radius 
     self.M = M 

    def step(self, dt): 

     self.pos += self.vel * dt 
     self.vel[2] += -9.81 * dt * self.M 

initpos = np.array([5., 5., 10.]) 
initvel = np.array([0., 0., 0.]) 
radius = .25 
M, dt = 1, .1 


test_ball = Ball(initpos, initvel, radius, M) 

fig, ax = plt.subplots(subplot_kw=dict(projection='3d')) 

pts = ax.plot([], [], [], 'bo', c='blue') 


def init(): 
    for pt in pts: 
     pt.set_data([], []) 
     pt.set_3d_properties([]) 
    ax.set_xlim3d(0, 1.5 * initpos[0]) 
    ax.set_ylim3d(0, 1.5 * initpos[1]) 
    ax.set_zlim3d(0, 1.5 * initpos[2]) 
    return pts 


def animate(i): 
    test_ball.step(dt) 
    for pt in pts: 
     pt.set_data(test_ball.pos[0], test_ball.pos[1]) 
     pt.set_3d_properties(test_ball.pos[2]) 
     pt.set_markersize(10) 

    return pts 

anim = animation.FuncAnimation(fig, animate, init_func=init, frames=50) 
mywriter = animation.FFMpegWriter(fps=10) 
anim.save('mymovie.mp4', writer=mywriter) 

当使用自定义写入器时,您需要在创建写入器时指定每秒的帧数。

您可以检查与下面的命令行正确的FPS

$ ffprobe mymovie.mp4 
相关问题