在Raspbian(树莓派2)上,以下是从我的脚本中剥离出来的最小示例,可以正确生成mp4文件:
现在,文件的质量非常好,文件大小也很小,但是生成一个包含170帧的电影需要15分钟,这对我的应用程序来说是不可接受的。我正在寻找一种显著的加速方法,视频文件大小增加不是问题。
我认为视频制作中的瓶颈在于以png格式临时保存帧。在处理过程中,我可以看到png文件出现在我的工作目录中,CPU负载仅为25%。
请提供一个解决方案,该方案可能基于不同的软件包,而不仅仅是matplotlib.animation,例如OpenCV(已导入到我的项目中)或moviepy。
使用的版本:
- python 2.7.3 - matplotlib 1.1.1rc2 - ffmpeg 0.8.17-6:0.8.17-1+rpi1
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文件出现在我的工作目录中,CPU负载仅为25%。
请提供一个解决方案,该方案可能基于不同的软件包,而不仅仅是matplotlib.animation,例如OpenCV(已导入到我的项目中)或moviepy。
使用的版本:
- python 2.7.3 - matplotlib 1.1.1rc2 - ffmpeg 0.8.17-6:0.8.17-1+rpi1
self._frame_sink()
的部分被简单地替换为self._proc.stdin
。你可以尝试一下这个方法。 编辑:如果有时间的话,我下周会尝试更新答案。 - Aule Mahal