We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Save array of images to movie? (Read 1194 times)
Save array of images to movie?
Mar 8th, 2010, 8:44am
 
Hi,

I am trying to create an example of saving an array of images to a movie (quicktime) file.  Preferably, these images would be sequentially numbered and live in a folder (somewhere).  Is this the best way to do it? I had some issues with saveFrame(pixels, width, height) where I keep getting an ArrayIndexOutOfBoundsException (is this a bug?), so I've moved to a different approach:

Code:

import processing.video.*;

MovieMaker mm;

int i = 0;
final int MAX_FRAMES = 10;
final int FRAME_W=320;
final int FRAME_H=240;

PImage frames[] = new PImage[MAX_FRAMES];

void setup()
{
 size(FRAME_W,FRAME_H);
 frameRate(1000);  // something REALLY high

 // Save uncompressed, at 15 frames per second
 mm = new MovieMaker(this, FRAME_W, FRAME_H, "drawing.mov", 30, MovieMaker.JPEG, MovieMaker.HIGH);

 // add leading "0", number from 1..MAX_FRAMES+1
 for (int f=1; f<(MAX_FRAMES+1); f++)
 {
   // leading "0" (or not)
   String index;
   if (f<10)
   {
     index = "0"+f;
   }
   else
   {
     index = ""+f;
   }

   // create a file name
   String fileName = "jellotail-0000" + index + ".tga";
   //println(fileName);

   frames[f-1] = loadImage(fileName);
 }  
}

// Or, set specific compression and frame rate options
//mm = new MovieMaker(this, width, height, "drawing.mov", 30,
//                    MovieMaker.ANIMATION, MovieMaker.HIGH);

void draw()
{
 if (i >= 10)
 {
   // we're done!

   mm.finish();
   exit();
 }
 else
 {
   
   // draw to screen, then save frame
   image(frames[i],0,0);
   mm.addFrame();

   println("frame["+i+"]:" + frames[i].pixels.length + ":" + frames[i].width*frames[i].height +" / "+ frames[i].width +" / "+  frames[i].height);

   // Add window's pixels to movie
   //mm.addFrame(frames[i].pixels, frames[i].width, frames[i].height);
   // move on to next
   i++;
 }  
}
Re: Save array of images to movie?
Reply #1 - Mar 8th, 2010, 9:56am
 
> I am trying to create an example of saving an array of images to a movie (quicktime) file.  Preferably, these images would be sequentially numbered and live in a folder (somewhere).  Is this the best way to do it?

ffmpeg or mencoder (or quicktime pro) would be better... or am i missing the point?

(look at nf() to do your zero padding as well)
Re: Save array of images to movie?
Reply #2 - Mar 8th, 2010, 10:13am
 
The saveFrame() doesn't look like "saveFrame(pixels, width, height)", and will only serve to save a single image of the current display; it is not concerned with creating a "movie" file.

It wasn't obvious from looking at your code why an ArrayIndexOutOfBoundsException would occur (assuming you are referring to the code posted, and not some other code).

I found the culprit: the exit() documentation states:

Quote:
Rather than terminating immediately, exit() will cause the sketch to exit after draw() has completed (or after setup() completes if called during the setup() method).


So, your program continues on after your call to exit for one last pass through the draw() method! To bail out of draw(), you can stick in a "return;" after the exit().

Also, there is no reason to load all of the still images in at the start of the program. You only need to load (and draw) one image then call mm.addFrame() for that frame.

Code:
// ** UNTESTED **

void draw()
{
 // 6-digit number with (up to 5) leading zeros in filename
 String fileName = "jellotail-" + nf(frameCount, 6) + ".tga";
 println("Loading: " + fileName);

 PImage still = loadImage(fileName);
 if (still == null)
 {
   println("Load fail: " + fileName);
   exit();
   return;
 }
 
 image(still, 0, 0, width, height); // stretch to fit
 mm.addFrame();
}

void exit()
{
 if (mm != null) mm.finish();
 super.exit();
}


-spxl
Page Index Toggle Pages: 1