From ab9e6502ba0cb289816b8e43c20f034cae31c89c Mon Sep 17 00:00:00 2001 From: Casey Reas Date: Fri, 31 Aug 2012 21:23:20 +0000 Subject: [PATCH] Updated Movie Examples --- .../video/examples/Movie/Frames/Frames.pde | 79 +++++++++++++++++++ .../video/examples/Movie/Loop/Loop.pde | 30 +++++++ .../examples/Movie/Pixelate/Pixelate.pde | 51 ++++++++++++ .../video/examples/Movie/Reverse/Reverse.pde | 51 ++++++++++++ .../video/examples/Movie/Scratch/Scratch.pde | 40 ++++++++++ .../video/examples/Movie/Speed/Speed.pde | 35 ++++++++ 6 files changed, 286 insertions(+) create mode 100644 java/libraries/video/examples/Movie/Frames/Frames.pde create mode 100644 java/libraries/video/examples/Movie/Loop/Loop.pde create mode 100644 java/libraries/video/examples/Movie/Pixelate/Pixelate.pde create mode 100644 java/libraries/video/examples/Movie/Reverse/Reverse.pde create mode 100644 java/libraries/video/examples/Movie/Scratch/Scratch.pde create mode 100644 java/libraries/video/examples/Movie/Speed/Speed.pde diff --git a/java/libraries/video/examples/Movie/Frames/Frames.pde b/java/libraries/video/examples/Movie/Frames/Frames.pde new file mode 100644 index 000000000..98fcf8328 --- /dev/null +++ b/java/libraries/video/examples/Movie/Frames/Frames.pde @@ -0,0 +1,79 @@ +/** + * Frames. + * by Andres Colubri + * + * Moves through the video one frame at the time by using the + * arrow keys. It estimates the frame counts using the framerate + * of the movie file, so it might not be exact in some cases. + */ + +import processing.video.*; + +Movie mov; +int newFrame = 0; +int movFrameRate = 30; + +void setup() { + size(640, 360, P2D); + background(0); + // Load and set the video to play. Setting the video + // in play mode is needed so at least one frame is read + // and we can get duration, size and other information from + // the video stream. + mov = new Movie(this, "transit.mov"); + + // Pausing the video at the first frame. + mov.play(); + mov.jump(0); + mov.pause(); +} + +void movieEvent(Movie m) { + m.read(); +} + +void draw() { + background(0); + image(mov, 0, 0, width, height); + fill(255); + text(getFrame() + " / " + (getLength() - 1), 10, 30); +} + +void keyPressed() { + if (key == CODED) { + if (keyCode == LEFT) { + if (0 < newFrame) newFrame--; + } else if (keyCode == RIGHT) { + if (newFrame < getLength() - 1) newFrame++; + } + } + setFrame(newFrame); +} + +int getFrame() { + return ceil(mov.time() * 30) - 1; +} + +void setFrame(int n) { + mov.play(); + + // The duration of a single frame: + float frameDuration = 1.0 / movFrameRate; + + // We move to the middle of the frame by adding 0.5: + float where = (n + 0.5) * frameDuration; + + // Taking into account border effects: + float diff = mov.duration() - where; + if (diff < 0) { + where += diff - 0.25 * frameDuration; + } + + mov.jump(where); + mov.pause(); +} + +int getLength() { + return int(mov.duration() * movFrameRate); +} + diff --git a/java/libraries/video/examples/Movie/Loop/Loop.pde b/java/libraries/video/examples/Movie/Loop/Loop.pde new file mode 100644 index 000000000..468034a30 --- /dev/null +++ b/java/libraries/video/examples/Movie/Loop/Loop.pde @@ -0,0 +1,30 @@ +/** + * Loop. + * + * Move the cursor across the screen to draw. + * Shows how to load and play a QuickTime movie file. + * + */ + +import processing.video.*; + +Movie movie; + +void setup() { + size(640, 360); + background(0); + // Load and play the video in a loop + movie = new Movie(this, "transit.mov"); + movie.loop(); +} + +void movieEvent(Movie m) { + m.read(); +} + +void draw() { + //if (movie.available() == true) { + // movie.read(); + //} + image(movie, 0, 0, width, height); +} diff --git a/java/libraries/video/examples/Movie/Pixelate/Pixelate.pde b/java/libraries/video/examples/Movie/Pixelate/Pixelate.pde new file mode 100644 index 000000000..60f534053 --- /dev/null +++ b/java/libraries/video/examples/Movie/Pixelate/Pixelate.pde @@ -0,0 +1,51 @@ +/** + * Pixelate + * by Hernando Barragan. + * + * Load a QuickTime file and display the video signal + * using rectangles as pixels by reading the values stored + * in the current video frame pixels array. + */ + +import processing.video.*; + +int numPixelsWide, numPixelsHigh; +int blockSize = 10; +Movie mov; +color movColors[]; + +void setup() { + size(640, 360); + noStroke(); + mov = new Movie(this, "transit.mov"); + mov.loop(); + numPixelsWide = width / blockSize; + numPixelsHigh = height / blockSize; + println(numPixelsWide); + movColors = new color[numPixelsWide * numPixelsHigh]; +} + +// Display values from movie +void draw() { + if (mov.available() == true) { + mov.read(); + mov.loadPixels(); + int count = 0; + for (int j = 0; j < numPixelsHigh; j++) { + for (int i = 0; i < numPixelsWide; i++) { + movColors[count] = mov.get(i*blockSize, j*blockSize); + count++; + } + } + } + + background(255); + for (int j = 0; j < numPixelsHigh; j++) { + for (int i = 0; i < numPixelsWide; i++) { + fill(movColors[j*numPixelsWide + i]); + rect(i*blockSize, j*blockSize, blockSize, blockSize); + } + } + //image(mov, 0, 0); +} + diff --git a/java/libraries/video/examples/Movie/Reverse/Reverse.pde b/java/libraries/video/examples/Movie/Reverse/Reverse.pde new file mode 100644 index 000000000..b86279744 --- /dev/null +++ b/java/libraries/video/examples/Movie/Reverse/Reverse.pde @@ -0,0 +1,51 @@ +/** + * Reverse playback example. + * + * The Movie.speed() method allows to + * change the playback speed. Use negative + * values for backwards playback. Note that + * not all video formats support backwards + * playback. This depends on the underlying + * gstreamer plugins used by gsvideo. For + * example, the theora codec does support + * backward playback, but not so the H264 + * codec, at least in its current version. + * + */ + +import processing.video.*; + +Movie mov; +boolean speedSet = false; +boolean once = true; + +void setup() { + size(640, 360); + background(0); + mov = new Movie(this, "transit.mov"); + mov.play(); +} + +void movieEvent(Movie m) { + m.read(); + if (speedSet == true) { + speedSet = false; + } +} + +void draw() { + if (speedSet == false && once == true) { + // Setting the speed should be done only once, + // this is the reason for the if statement. + speedSet = true; + once = false; + mov.jump(mov.duration()); + // -1 means backward playback at normal speed. + mov.speed(-1.0); + // Setting to play again, since the movie stop + // playback once it reached the end. + mov.play(); + } + image(mov, 0, 0, width, height); +} + diff --git a/java/libraries/video/examples/Movie/Scratch/Scratch.pde b/java/libraries/video/examples/Movie/Scratch/Scratch.pde new file mode 100644 index 000000000..066312eb8 --- /dev/null +++ b/java/libraries/video/examples/Movie/Scratch/Scratch.pde @@ -0,0 +1,40 @@ +/** + * Scratch. + * by Andres Colubri + * + * Move the cursor horizontally across the screen to set + * the position in the movie file. + */ + +import processing.video.*; + +Movie mov; + +void setup() { + size(640, 360); + background(0); + + mov = new Movie(this, "transit.mov"); + + // Pausing the video at the first frame. + mov.play(); + mov.jump(0); + mov.pause(); +} + +void movieEvent(Movie m) { + m.read(); +} + +void draw() { + // A new time position is calculated using the current mouse location: + float f = constrain((float)mouseX / width, 0, 1); + float t = mov.duration() * f; + if (mov.ready()) { + mov.play(); + mov.jump(t); + mov.pause(); + } + image(mov, 0, 0); +} + diff --git a/java/libraries/video/examples/Movie/Speed/Speed.pde b/java/libraries/video/examples/Movie/Speed/Speed.pde new file mode 100644 index 000000000..c42b3cdfb --- /dev/null +++ b/java/libraries/video/examples/Movie/Speed/Speed.pde @@ -0,0 +1,35 @@ +/** + * GSVideo movie speed example. + * + * Use the Movie.speed() method to change + * the playback speed. + * + */ + +import processing.video.*; + +Movie mov; + +void setup() { + size(640, 360); + background(0); + mov = new Movie(this, "transit.mov"); + mov.loop(); + + textSize(18); +} + +void movieEvent(Movie movie) { + mov.read(); +} + +void draw() { + image(mov, 0, 0); + + float newSpeed = map(mouseX, 0, width, 0.1, 2); + mov.speed(newSpeed); + + fill(255); + text(nfc(newSpeed, 2) + "X", 10, 30); +} +