Outputting video/gif

Hi

I have an animation which run in Processing.py. How can I output a video of the, lets say, first n seconds of that animation? I would prefer a gif, but any format would do.

Hi avatrin!

well, i’m not very sure you can output video format of animation in processing, but you can save every frame you get and combine it into video or gif.
see: https://processing.org/reference/saveFrame_.html

for combining, i’m usually using ffmpeg
see: https://stackoverflow.com/questions/24961127/how-to-create-a-video-from-images-with-ffmpeg

probably you will need frameCount to limit frames elapsed of the animation
https://processing.org/reference/frameCount.html

2 Likes

The Video Export library essentially automates the steps @humayung has described.

You can install it via the library menu (Sketch > Import Library > Add Library). Then, you’ll need to download FFmpeg:
https://ffmpeg.org/download.html

Here’s some code to try out. When it runs, Processing will prompt you to locate/browse-to your ffmpeg executable:

add_library('VideoExport')

def setup():
    size(600, 600)
    global videoExport
    videoExport = VideoExport(this)
    videoExport.startMovie()

x = 0

def draw():
    global x
    background(0)
    rect(x, 10, 50, 50)
    videoExport.saveFrame()
    x += 1

def keyPressed():
  if (key == 'q'):
    videoExport.endMovie()
    exit()

For animated GIFs, it’s probably easiest to go the saveFrame() route, then compile the images into a GIF using whatever (i.e. https://ezgif.com/maker). There are also Python libraries for creating animated GIFs, should you wish to automate the process.

4 Likes