1

I installed ffmpeg and would like to save an animation.

My code is

#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'])

The animation runs and it looks good, but I just can't get it to save. The error message (at this stage) is

error

I am not sure what's going wrong. Any help is appreciated.

EDIT: Following Can't save matplotlib animation I added

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

Which returns Warning: Cannot change to a different GUI toolkit: qt. Using qt4 instead. ERROR: execution aborted

Community
  • 1
  • 1
kevinkayaks
  • 2,636
  • 1
  • 14
  • 30
  • The kernel seems to crash with the revision following "EDIT:" – kevinkayaks Apr 19 '17 at 17:13
  • 1
    I don't know if this is the cause of the problem, but you should use `=` for arguments. There is also a lot of space in your ffmpeg path, which shouldn't be there. Finally, I think you should specify the arguments to `FFMpegWriter` directly: `FFwriter = animation.FFMpegWriter(fps=30, extra_args=['-vcodec', 'libx264']); ani.save('basic_animation.mp4', writer = FFwriter)`. – ImportanceOfBeingErnest Apr 19 '17 at 17:45
  • Thanks @ImportanceOfBeingErnest. That was just a typo made copying between python and stack. It isn't the source of the problem :( – kevinkayaks Apr 20 '17 at 00:42
  • What about the other two suggestions I made in my comment? – ImportanceOfBeingErnest Apr 20 '17 at 06:42
  • Oh sorry @ImportanceOfBeingErnest I missed that you moved arguments into FFMpegWriter-- I did not understand. That fixed it! Thank you. – kevinkayaks Apr 21 '17 at 17:02

2 Answers2

4

Providing my comment as an answer:

I think you should specify the arguments to FFMpegWriter directly in the initialization of that instance instead of supplying some of them to the animation save method.

FFwriter = animation.FFMpegWriter(fps=30, extra_args=['-vcodec', 'libx264'])
ani.save('basic_animation.mp4', writer = FFwriter)
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
0

As an alternate format, I've had success with using ogg/theora an alternate video format for compatibility with Wikipedia per https://en.wikipedia.org/wiki/Help:Creation_and_usage_of_media_files#Video

Try this:

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

You have to match the codec with the filename-derived format

Dave X
  • 4,831
  • 4
  • 31
  • 42