Activate webcam at certain time while playing video

Hi all,

I want to play a video in processing and 1 minute and 34 seconds into the video I want to implement live video footage from the webcam.
This is how far I’ve come.
How can I activate the webcam 1 minute and 34 seconds into the movie and deactivate again 5 seconds later?

  
import processing.video.*;
Movie myMovie;
Capture cam;

void setup() {
  size(1920, 1080);
  myMovie = new Movie(this, "take metro to coast_final.mp4");
  myMovie.play();
  
  String[] cameras = Capture.list();
  cam = new Capture(this, width, height, "FaceTime HD Camera", 30);
  cam.start();
}

void draw() {
  image(myMovie, 0, 0);
  if (cam.available() == true) {
    cam.read();
  }
  image(cam, 400, 200, 640, 480);
}

void movieEvent(Movie m) {
  m.read();
}

Ok one step closer, the webcam is now activated after a certain time.
But now, how to de-activate it? How to make it disappear again after 5 seconds?

  
import processing.video.*;
Movie myMovie;
Capture cam;

void setup() {
  size(1920, 1080);
  myMovie = new Movie(this, "take metro to coast_final.mp4");
  myMovie.play();
  
  cam = new Capture(this, width, height, "FaceTime HD Camera", 30);
  cam.start();
}

void draw() {
  image(myMovie, 0, 0);
  if (cam.available() == true) {
    cam.read();
  }
   float mt = myMovie.time();
   if (mt > 10){
  image(cam, 400, 200, 640, 480);
   if (mt > 15){
//how to make the webcam window disappear again??
   }
   }
}

void movieEvent(Movie m) {
  m.read();
}

https://processing.org/reference/libraries/video/Capture_stop_.html

I tried this but the window will not disappear. It will stop capturing, meaning it will freeze, but how to make the webcam window disappear?

heres a class to handle the webcam.

import processing.video.*;
Webcam cam;
void setup(){
  size(700,600);
  cam = new Webcam(this);
    
    
};

void draw(){
  background(255);
  cam.display(20,20);
};

void mousePressed(){
  cam.toggle();
};

class Webcam {
  String[] cameras = Capture.list();
  boolean show = true;
  Capture cam;
  PApplet applet;

  Webcam(PApplet app) {
    applet = app;
    if (cameras.length == 0) {
      println("There are no cameras available for capture.");
      //exit();
    } else {
      println(cameras.length + " Available cameras:");
      for (int i = 0; i < cameras.length; i++) {
        println(i + " " + cameras[i]);
      }

      // The camera can be initialized directly using an 
      // element from the array returned by list():
      cam = new Capture(app, cameras[26]);
      cam.start();
    }
  };
//display the webcam
  void display() {
    if (cam.available() == true) {
      cam.read();
    }
    if (show)image(cam, 0, 0);
  };
//display and amend the position of the webcam
  void display(float x, float y) {
    if (cam.available() == true) {
      cam.read();
    }
    if (show)image(cam, x, y);
  };
//display to a canvas of your choice
  void displayC(PGraphics c) {
    if (cam.available() == true) {
      cam.read();
    }
    if (show) {
      c.beginDraw();
      c.image(cam, 0, 0);
      c.endDraw();
    }
  };
//display to a canvas of your choice with x and y positions
  void displayC(float x, float y) {
    if (cam.available() == true)cam.read();
    else println("no Cam");
    if(show)image(cam, x, y);
  };
// returns the current frame as a PImage
  PImage get() {
    return cam;
  };
// toggle function for mousePressed
  void toggle() {
    cam.stop();
    if (show)show = false;
    else show = true;

    if (show)cam.start();
    else cam.stop();
  };
};

Hi Paul,

Thanks a lot for taking the time to help me. So far I still haven’t figured it out, I’m very new to processing. Could you explain a bit further on how to use the class you provided to make the webcam image disappear again after x seconds?

Thanks again!!

You’ll need to add a counter ie

int i =0;

void draw(){
   i ++;
   if(i>someNumber)cam.stop();
}