# 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:** 1
**Showing post:** 4

<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);
}

```

---

_[View the full topic](https://discourse.processing.org/t/read-from-2-videos-at-the-same-time/19937)._
