【问题标题】:Trouble saving matplotlib animation with ffmpeg使用 ffmpeg 保存 matplotlib 动画时遇到问题
【发布时间】:2017-09-16 00:59:48
【问题描述】:

我安装了 ffmpeg 并想保存动画。

我的代码是

#evo is the dataset composed of sequence of images

evo = np.load('bed_evolution_3000iter_2.npy')
fig = plt.figure(figsize=(15,15*2*width/length))
# make axesimage object
# the vmin and vmax here are very important to get the color map correct
im = plt.imshow(np.transpose(evo[0]), cmap=plt.get_cmap('jet'), vmin=0, vmax=1300)
cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7])
fig.colorbar(im, cax=cbar_ax)
fig.subplots_adjust(right=0.8)

def updatefig(j):    
    # set the data in the axesimage object
    im.set_array(np.transpose(evo[j]))
    # return the artists set
    return im,
# kick off the animation
ani = animation.FuncAnimation(fig, updatefig, frames=range(len(evo)), 
                          interval=100, blit=True)

#now just need to get the ability to save... this uses 

FFwriter = animation.FFMpegWriter()
ani.save('basic_animation.mp4', writer = FFwriter, fps=30, extra_args =([vcodec', 'libx264'])

动画运行并且看起来不错,但我无法保存它。错误信息(在这个阶段)是

我不确定出了什么问题。任何帮助表示赞赏。

编辑: 关注Can't save matplotlib animation我加了

plt.rcParams['animation.ffmpeg_path'] ='C:\\Program Files\\ffmpeg  \\bin\\ffmpeg.exe'

返回 警告:无法更改为不同的 GUI 工具包:qt。改用 qt4。 错误:执行中止

【问题讨论】:

  • 内核似乎因“编辑:”之后的修订版而崩溃
  • 我不知道这是否是问题的原因,但您应该使用= 作为参数。您的 ffmpeg 路径中​​还有很多空间,不应该存在。最后,我认为你应该直接指定FFMpegWriter的参数:FFwriter = animation.FFMpegWriter(fps=30, extra_args=['-vcodec', 'libx264']); ani.save('basic_animation.mp4', writer = FFwriter)
  • 感谢@ImportanceOfBeingErnest。这只是在 python 和堆栈之间复制的一个错字。这不是问题的根源:(
  • 我在评论中提出的另外两个建议呢?
  • 哦,抱歉@ImportanceOfBeingErnest 我错过了您将参数移到 FFMpegWriter 中——我不明白。那解决了它!谢谢。

标签: python matplotlib ffmpeg


【解决方案1】:

提供我的评论作为答案:

我认为您应该直接在该实例的初始化中指定 FFMpegWriter 的参数,而不是将其中一些参数提供给动画 save 方法。

FFwriter = animation.FFMpegWriter(fps=30, extra_args=['-vcodec', 'libx264'])
ani.save('basic_animation.mp4', writer = FFwriter)

【讨论】:

    【解决方案2】:

    作为替代格式,我已经成功地使用了 ogg/theora 替代视频格式,以便与 https://en.wikipedia.org/wiki/Help:Creation_and_usage_of_media_files#Video 的维基百科兼容

    试试这个:

    writer = animation.FFMpegWriter(fps=30,codec='libtheora')
    ani.save("basic_animation.ogg", writer=writer)
    

    您必须将编解码器与文件名派生格式匹配

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-20
      • 2014-05-29
      • 2018-06-05
      • 1970-01-01
      • 2014-06-18
      • 1970-01-01
      • 2018-12-15
      • 1970-01-01
      相关资源
      最近更新 更多