使用Processing录制和播放多个视频

时间:2015-11-11 22:04:15

标签: video processing webcam

我正在尝试使用Processing设置以下内容:

  • 一个人按一个/任意一个键在网络摄像头前开始和结束录制。
  • 当她/他在镜头前,进行录音时,他/她可以看到他/她之前最后三个人的循环录音。
  • 无需担心声音。
  • 保存所有视频会很棒,但不是优先考虑。

所以,我到目前为止的代码是这样的:

import processing.video.*;

VideoExport videoExport;
boolean recording = false;

Capture theCap; 

Capture cam;

void setup() {
  size(400, 300);
  frameRate(30);

  String[] cameras = Capture.list();

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

    // The camera can be initialized directly using an 
    // element from the array returned by list():
    cam = new Capture(this, cameras[3]);
    cam.start();  
  }

  println("Press R to toggle recording");

  videoExport = new VideoExport(this, "video.mp4");
}

void draw() {

  if (cam.available() == true) {
    cam.read();
  }
  image(cam, 0, 0);
  // The following does the same, and is faster when just drawing the image
  // without any additional resizing, transformations, or tint.
  //set(0, 0, cam);

  if(recording) {
    videoExport.saveFrame();
  }
}

void keyPressed() {
  if(key == 'r' || key == 'R') {
    recording = !recording;
    println("Recording is " + (recording ? "ON" : "OFF"));
  }
}

我已尝试过许多不同的内容,例如videoExport,如上所示。我试过的只是通过覆盖以前的文件来保存视频,无法正确更新视频。请指教。谢谢。

1 个答案:

答案 0 :(得分:0)

您有两个主要问题:

  • 您只会写一个视频文件,因此您只需更换以前的内容。
  • 您实际上从未检查用户是否按下了&#39; A&#39;密钥,因此您永远不会增加您要保存的视频。

要解决这两个问题,您需要添加一个跟踪正在录制哪个视频索引的变量,然后使用该变量保存视频并播放以前的录制内容。

示例启动如下所示:

import processing.video.*;

VideoExport videoExport;
boolean recording = false;

Capture theCap; 

Capture cam;

int index = 0;

void setup() {
  size(400, 300);
  frameRate(30);

  String[] cameras = Capture.list();

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

    // The camera can be initialized directly using an 
    // element from the array returned by list():
    cam = new Capture(this, cameras[3]);
    cam.start();
  }

  println("Press R to toggle recording");

  videoExport = new VideoExport(this, "video" + index + ".mp4");
}

void draw() {

  if (cam.available() == true) {
    cam.read();
  }
  image(cam, 0, 0);
  // The following does the same, and is faster when just drawing the image
  // without any additional resizing, transformations, or tint.
  //set(0, 0, cam);

  if (recording) {
    videoExport.saveFrame();
  }
}

void keyPressed() {
  if (key == 'r' || key == 'R') {
    recording = !recording;
    println("Recording is " + (recording ? "ON" : "OFF"));
  } else   if (key == 'a' || key == 'A') {
    index++;
    videoExport = new VideoExport(this, "video" + index + ".mp4");
  }
}

请注意,我所做的只是添加index变量,然后在keyPressed()函数中增加该变量。然后我使用该索引创建一个新的VideoExport,因此我不会覆盖现有文件。

这样可以将每个视频保存到单独的文件中,因此,如果您还想播放前3个视频,只需播放index-3index-2index-1