Skip to content

Instantly share code, notes, and snippets.

@sivieri
Created August 19, 2011 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sivieri/1156542 to your computer and use it in GitHub Desktop.
Save sivieri/1156542 to your computer and use it in GitHub Desktop.
Processing.org and GSVideo: capture, frame difference and dump
import codeanticode.gsvideo.*;
int numPixels;
int[] previousFrame;
GSCapture video;
GSMovieMaker mm;
int fps = 15;
void setup() {
size(640, 480);
frameRate(fps);
video = new GSCapture(this, width, height);
video.play();
numPixels = video.width * video.height;
previousFrame = new int[numPixels];
loadPixels();
mm = new GSMovieMaker(this, width, height, "drawing.ogg", GSMovieMaker.THEORA, GSMovieMaker.MEDIUM, fps);
mm.setQueueSize(50, 10);
mm.start();
}
void draw() {
if (video.available()) {
video.read();
video.loadPixels();
int movementSum = 0;
for (int i = 0; i < numPixels; i++) {
color currColor = video.pixels[i];
color prevColor = previousFrame[i];
int currR = (currColor >> 16) & 0xFF;
int currG = (currColor >> 8) & 0xFF;
int currB = currColor & 0xFF;
int prevR = (prevColor >> 16) & 0xFF;
int prevG = (prevColor >> 8) & 0xFF;
int prevB = prevColor & 0xFF;
int diffR = abs(currR - prevR);
int diffG = abs(currG - prevG);
int diffB = abs(currB - prevB);
movementSum += diffR + diffG + diffB;
pixels[i] = 0xff000000 | (diffR << 16) | (diffG << 8) | diffB;
previousFrame[i] = currColor;
if (movementSum > 0) {
updatePixels();
mm.addFrame(pixels);
}
}
}
void keyPressed() {
if (key == ' ') {
mm.finish();
exit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment