Dec 28, 2011

Night #8: Rendering an image sequence

How can one render a movie from a Processing sketch? In Processing 1.5.1, there is a class called MovieMaker which generates a Quicktime file directly. However, this class uses ye old Quicktime for Java and will be removed from Processing 2.0. Instead, 2.0 is going to introduce a MovieMaker “tool” which will generate a movie file from a directory containing an image sequence. So how do you get this image sequence? Easy:

void draw() {
  saveFrame("output/frames####.png");
}

This will create a folder called “output” in your sketch folder and for each frame of draw(), it will write a file — frames0001.png, frames0002.png, etc. The ‘#’ sign tells processing to auto-number the images.

There are a couple additional tricks included in this new example. First, it uses a boolean variable to turn rendering on and off and cycles the boolean in keyPressed() allowing recording to be stopped and started by pressing ‘r’.

void draw() {
  // If we are recording call saveFrame!
  if (recording) {
    saveFrame("output/frames####.png");
  }
}

void keyPressed() {  
  // If we press r, start or stop recording!
  if (key == 'r' || key == 'R') {
    recording = !recording;
  }
}

In addition, if you draw anything after saveFrame() it won’t actually appear in the output, but will be seen on screen. This is useful for debugging information that you don’t want included in the render.

void draw() {
 
  // Draw lots of stuff to be recorded!
  
  if (recording) {
    saveFrame("output/frames####.png");
  }
   
  // Draw our debugging stuff that won't be recorded!
}

Until 2.0 is released, I recommend you use 3rd party software to take the image sequence and turn it into a movie. The old Quicktime 7 will do the trick, as well as any number of video production applications like final cut, after effects, iMovie, etc. The nice thing about using a PNG sequence is that the images are uncompressed, but aren’t as large as say TIFFs. I don’t recommend saving JPGs because then you will likely be compressing twice (once when saving the image, once when exporting the movie file).

Source: SavingFrames.zip