【问题标题】:Saving animations using ffmpeg and matplotlib使用 ffmpeg 和 matplotlib 保存动画
【发布时间】:2015-09-20 10:17:25
【问题描述】:

我是这里的新手,也是python的新手,我用matplotliib的animation.FuncAnimation做一些动画。动画效果很好,但我在保存动画时遇到问题。这是动画的部分代码。

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

fig, ax = plt.subplots()

line,  = ax.plot(range(N),sin(x[0,:]),'o-')
ax.axis([0,1,-1,1])

def animate(i):
    line.set_ydata(sin(x[i,:]))  # update the data
    return line,

def init():
    line.set_ydata(np.ma.array(x[0,:], mask=True))
    return line,

ani = animation.FuncAnimation(fig, animate, np.arange(1, 10000),
    interval=25, init_func=init, blit=True)
ani.save('2osc.mp4', writer="ffmpeg")
plt.show()

其中 x[:,:] 是先前设置的。 ani.save 将动画的每一帧保存为保存电影的 .npg 图像。我不知道这是否是它应该工作的方式,我必须用 .npg 和另一个程序来制作电影,或者我做错了什么。 Obs:我以前安装过 ffmpeg,它似乎工作得很好。

【问题讨论】:

  • 您是否安装了所需的库 (sudo apt-get install ffmpeg)?另外,你能分享你运行的确切代码吗?
  • 我没有安装所需的库,但我的代码现在可以工作,我关闭了所有内容,重新启动 pyhton 并再次运行它,现在保存电影就好了。还是谢谢!!

标签: python animation ffmpeg save


【解决方案1】:

对我来说,这段代码似乎运行良好:

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

fig, ax = plt.subplots(1,1)
x=np.linspace(np.pi,4*np.pi,100)
N=len(x)
ax.set_xlim(len(x))
ax.set_ylim(-1.5,1.5)
line,  = ax.plot([],[],'o-')

def init():
    line.set_ydata(np.ma.array(x[:], mask=True))
    return line,

def animate(i, *args, **kwargs):
    y=np.sin(x*i)
    line.set_data(np.arange(N),y)            # update the data
    return line,

ani = animation.FuncAnimation(fig, animate, init_func=init, 
     frames=100, interval=10, blit= False, repeat = False)
ani.save('2osc.mp4', writer="ffmpeg")
fig.show()

您可以使用以下方法安装 ffmpeg 库:

sudo apt-get install ffmpeg

【讨论】:

  • 你是对的,它工作正常,我不得不关闭一切重新启动 python 并再次运行代码,现在我有很多漂亮的视频。^^ 对不起,如果我让你失去你的时间。谢谢!
猜你喜欢
  • 2014-05-29
  • 2018-06-05
  • 2017-09-16
  • 2014-06-18
  • 2018-12-15
  • 1970-01-01
  • 2013-08-03
  • 2013-01-22
  • 1970-01-01
相关资源
最近更新 更多