diff --git a/java/libraries/video/examples/Movie/Frames/Frames.pde b/java/libraries/video/examples/Movie/Frames/Frames.pde index 511f4800a..aefbeac68 100644 --- a/java/libraries/video/examples/Movie/Frames/Frames.pde +++ b/java/libraries/video/examples/Movie/Frames/Frames.pde @@ -3,7 +3,8 @@ * by Andres Colubri * * Moves through the video one frame at the time by using the - * arrow keys. + * 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.*; @@ -38,7 +39,7 @@ void draw() { image(movie, 0, 0, width, height); fill(240, 20, 30); - text(movie.frame() + " / " + (movie.length() - 1), 10, 30); + text(getFrame() + " / " + (getLength() - 1), 10, 30); } void keyPressed() { @@ -46,11 +47,41 @@ void keyPressed() { if (keyCode == LEFT) { if (0 < newFrame) newFrame--; } else if (keyCode == RIGHT) { - if (newFrame < movie.length() - 1) newFrame++; + if (newFrame < getLength() - 1) newFrame++; } } - movie.play(); - movie.jump(newFrame); - movie.pause(); + + setFrame(newFrame); } + +int getFrame() { + return ceil(movie.time() * movie.getSourceFrameRate()) - 1; +} + +void setFrame(int n) { + movie.play(); + + float srcFramerate = movie.getSourceFrameRate(); + + // The duration of a single frame: + float frameDuration = 1.0 / srcFramerate; + + // 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 = movie.duration() - where; + if (diff < 0) { + where += diff - 0.25 * frameDuration; + } + + movie.jump(where); + + movie.pause(); +} + +int getLength() { + return int(movie.duration() * movie.getSourceFrameRate()); +} + diff --git a/java/libraries/video/src/processing/video/Movie.java b/java/libraries/video/src/processing/video/Movie.java index b61664270..5ed5a5a9f 100644 --- a/java/libraries/video/src/processing/video/Movie.java +++ b/java/libraries/video/src/processing/video/Movie.java @@ -294,25 +294,6 @@ public class Movie extends PImage implements PConstants { return sec + Video.nanoSecToSecFrac(nanosec); } - /** - * Get the full length of this movie (in frames). - * - * @return float - */ - public long length() { - return (int)(duration() * getSourceFrameRate()); - } - - /** - * Return the current frame. - * - * @return int - */ - public int frame() { - double sec = gplayer.queryPosition().toSeconds() + gplayer.queryPosition().getNanoSeconds() * 1E-9; - return (int)(Math.ceil(sec * getSourceFrameRate())) - 1; - } - /** * ( begin auto-generated from Movie_jump.xml ) * @@ -349,30 +330,7 @@ public class Movie extends PImage implements PConstants { gplayer.getState(); seeking = false; } - - /** - * Jump to a specific frame. - * - * @param frame ??? - */ - public void jump(int frame) { - float srcFramerate = getSourceFrameRate(); - - // The duration of a single frame: - float frameDuration = 1.0f / srcFramerate; - - // We move to the middle of the frame by adding 0.5: - float where = (frame + 0.5f) * frameDuration; - - // Taking into account border effects: - float diff = duration() - where; - if (diff < 0) { - where += diff - 0.25f * frameDuration; - } - - jump(where); - } - + /** * Returns true if the stream is already producing frames. *