ArrayIndexOutOfBoundsException error, using movie

Hello! I am trying to make this Daniel Shiffman’s code : MotionPixels | Learning Processing 2nd Edition
not with the Capture datatype, but with the Movie so that it reads a video file instead of a device on my computer. The code is meant to capture the motion of a video.

The problem is that this error appears : ArrayIndexOutOfBoundsException: 0
The problem is the loc i think.
can anyone help ? thank you :slight_smile:

code :

import processing.video.*;

boolean start = false;

Movie video;
PImage prevFrame;
float threshold = 50;

void setup() {
  size(1280, 720);

  video = new Movie(this, "macron.mp4");
  video.play();
  prevFrame = createImage(video.width, video.height, RGB);
}

void movieEvent(Movie video) {
  prevFrame.copy(video, 0, 0, video.width, video.height, 0, 0, video.width, video.height);
  prevFrame.updatePixels();
  video.read();
}

void draw() {

  image(video, 0, 0);
  
  if (start) {
    loadPixels();
    video.loadPixels();
    prevFrame.loadPixels();
    for (int x = 0; x < video.width; x ++ ) {
      for (int y = 0; y < video.height; y ++ ) {
        int loc = x + y*video.width;            
        color current = video.pixels[loc];      
        color previous = prevFrame.pixels[loc];   //where the problem is
        float r1 = red(current); 
        float g1 = green(current); 
        float b1 = blue(current);
        float r2 = red(previous); 
        float g2 = green(previous); 
        float b2 = blue(previous);
        float diff = dist(r1, g1, b1, r2, g2, b2);
        if (diff > threshold) { 
          pixels[loc] = color(0);
        } else {
          pixels[loc] = color(255);
        }
      }
    }
  }
  updatePixels();
}

void keyPressed() {
  if (start == false) {
    start = true;
  } else {
    start = false;
  }
}