在Raspbian(树莓派2),下面的小例子,从我的脚本剥离正确生成的MP4文件:提速matplotlib动画视频文件
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
def anim_lift(x, y):
#set up the figure
fig = plt.figure(figsize=(15, 9))
def animate(i):
# update plot
pointplot.set_data(x[i], y[i])
return pointplot
# First frame
ax0 = plt.plot(x,y)
pointplot, = ax0.plot(x[0], y[0], 'or')
anim = animation.FuncAnimation(fig, animate, repeat = False,
frames=range(1,len(x)),
interval=200,
blit=True, repeat_delay=1000)
anim.save('out.mp4')
plt.close(fig)
# Number of frames
nframes = 200
# Generate data
x = np.linspace(0, 100, num=nframes)
y = np.random.random_sample(np.size(x))
anim_lift(x, y)
现在,生产以优良的品质和非常小的文件文件大小,但制作170帧电影需要15分钟,这对于我的应用程序来说是不可接受的。我正在寻找一个显着的加速,视频文件大小的增加不是问题。
我相信视频制作中的瓶颈是暂时保存png格式的帧。在处理过程中,我可以看到png文件在我的工作目录中,只有25%的CPU负载。
请提出一个解决方案,那也可能基于不同的包,而不是简单地matplotlib.animation
,像OpenCV
或moviepy
(这无论如何已经在我的项目进口)。
版本中使用:
- 蟒蛇2.7.3
- matplotlib 1.1.1rc2
- 的ffmpeg 0.8.17-6:0.8.17-1 + rpi1
感谢您的纠正,实际上我的版本运行得益于'pointplot [0]',但您的版本更干净,我将相应地编辑原始版本。 – gaggio
无论如何,速度的问题仍然存在......您是否注意到您的程序在工作目录中生成临时png文件?我认为你的版本也运行在raspberry pi 2,raspbian发行版上?也许这在我原来的问题中还不够清楚。 – gaggio