# Read from 2 videos at the same time

**URL:** https://discourse.processing.org/t/read-from-2-videos-at-the-same-time/19937
**Category:** Libraries
**Created:** [April 19, 2020, 6:32pm UTC](https://discourse.processing.org/t/read-from-2-videos-at-the-same-time/19937 "2020-04-19T18:32:00Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![costachemadalin](https://avatars.discourse-cdn.com/v4/letter/c/58956e/32.png) [@costachemadalin](https://discourse.processing.org/u/costachemadalin)
#### Post date: [April 19, 2020, 6:32pm UTC](https://discourse.processing.org/t/read-from-2-videos-at-the-same-time/19937/1 "2020-04-19T18:32:00Z")

</div>

Hi, for my algorithm i want to process first frame from `videoA` and first frame from `videoB` at the same time and so on until the last frame.

In my implementation i have a problem,in theory it should read alternately like this frame 1 from `videoA`, frame 1 from `videoB`, frame 2 from `video A`, frame 2 from `videoB` and so on, but in reality the second `videoB` it starts to read after `video A` already read 6-8 frames, i don’t know why videoB it starts to read with so much delay.

Is it possible to read at the same time from 2 different videos without any delay?

[link videos](https://easyupload.io/77cupd)

```auto
import processing.video.*;
Movie videoA, videoB;

void setup() {
  size(1600, 600);
  videoA = new Movie(this, "videoL1.avi");
  videoB = new Movie(this, "videoR1.avi");

  videoA.play();
  videoB.play();
}

void draw() {

  image(videoA, 0, 0, 800, 600);
  image(videoB, 800, 0, 800, 600);

 
}

int counterL=0;
int counterR=0;
boolean a=true;
void movieEvent(Movie c) {
  if (c==videoA) {  
    println("left image: "+counterL);
    counterL++;
    videoA.read();
  } else if (c==videoB) {
    println("right image: "+counterR);
    counterR++;
    videoB.read();
  }
}
boolean stop=false;
void keyPressed() {

  if (key=='s')
  {
    if (stop==false)
    {
      videoA.pause();
      videoB.pause();
      stop=true;
    } else
    {
      stop=false;
      videoA.play();
      videoB.play();
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)
#### Post date: [April 19, 2020, 10:57pm UTC](https://discourse.processing.org/t/read-from-2-videos-at-the-same-time/19937/2 "2020-04-19T22:57:12Z")

</div>

This is just a guess, but maybe the time it takes to `play()` the videos in the beginning causes the delay. Try to create them both on seperate threads and check when those finished:

```auto
boolean videoAPlayed, videoBPlayed;

void setup() {
  // ...

  // Instead of directly playing them:
  thread("playVideoA");
  thread("playVideoB");
}

void playVideoA() {
  videoA.play();
  videoAPlayed = true;
}

void playVideoB() {
  videoB.play();
  videoBPlayed = true;
}

void draw() {
  if (videoAPlayed)
    image(videoA, 0, 0, 800, 600);
  if (videoBPlayed)
    image(videoB, 800, 0, 800, 600);
}

```

---

<div class="post-metadata">

### Author: ![costachemadalin](https://avatars.discourse-cdn.com/v4/letter/c/58956e/32.png) [@costachemadalin](https://discourse.processing.org/u/costachemadalin)
#### Post date: [April 20, 2020, 8:59am UTC](https://discourse.processing.org/t/read-from-2-videos-at-the-same-time/19937/3 "2020-04-20T08:59:44Z")

</div>

thank you for your help,  
your idea solved my problem.

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [April 20, 2020, 9:25am UTC](https://discourse.processing.org/t/read-from-2-videos-at-the-same-time/19937/4 "2020-04-20T09:25:50Z")

</div>

I’ve just come up w/ a FiFo Queue solution using 2 ArrayDeque containers to store previous video frames of each Movie:

- [Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Queue.html](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Queue.html)
- [Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/ArrayDeque.html](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/ArrayDeque.html)

The trick is to only **remove()** a PImage frame of each container as a pair when none **isEmpty()**:

- [Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Queue.html#remove()](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Queue.html#remove())
- [Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Collection.html#isEmpty()](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Collection.html#isEmpty())

This way the 2 videos are displayed synched to their corresponding frame number.

The sketch can be downloaded on the link below:

- [GitHub.com/GoToLoop/FiFo-Sync-Frame-Pair/releases](http://GitHub.com/GoToLoop/FiFo-Sync-Frame-Pair/releases)

And its source code can be viewed on this link here:

> <https://github.com/GoToLoop/FiFo-Sync-Frame-Pair/blob/master/FiFo_Sync_Frame_Pair/FiFo_Sync_Frame_Pair.pde>

And in this forum as well:

```auto
/**
 * FiFo Sync Frame Pair (v1.0.1)
 * GoToLoop (2020/Apr/20)
 *
 * Discourse.Processing.org/t/read-from-2-videos-at-the-same-time/19937/4
 * GitHub.com/GoToLoop/FiFo-Sync-Frame-Pair
 */

import processing.video.Movie;

import java.util.Queue;
import java.util.ArrayDeque;

static final String VID_1 = "videoL1.avi", VID_2 = "videoR1.avi";
static final int W = 1024, H = 768, WW = W >> 1, HH = H >> 1;

final Queue<PImage> framesA = new ArrayDeque<PImage>();
final Queue<PImage> framesB = new ArrayDeque<PImage>();

Movie videoA, videoB;

void settings() {
  size(W, HH);
}

void setup() {
  videoA = new Movie(this, VID_1);
  videoB = new Movie(this, VID_2);

  videoA.play();
  videoB.play();
}

void draw() {
  if (framesB.isEmpty() || framesA.isEmpty()) return;

  final PImage frameA = framesA.remove();
  final PImage frameB = framesB.remove();

  set(0, 0, frameA);
  set(WW, 0, frameB);
}

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

  final PImage frame = m.get();
  frame.resize(WW, HH);

  final Queue<PImage> frames = m == videoA? framesA : framesB;
  frames.add(frame);
}

```

---

<div class="post-metadata">

### Author: ![costachemadalin](https://avatars.discourse-cdn.com/v4/letter/c/58956e/32.png) [@costachemadalin](https://discourse.processing.org/u/costachemadalin)
#### Post date: [April 20, 2020, 9:51am UTC](https://discourse.processing.org/t/read-from-2-videos-at-the-same-time/19937/5 "2020-04-20T09:51:11Z")

</div>

thank you,your implementation is great
