Play two videos at the same time in HTML

Viewed 28

I'm trying to play two videos side by side in HTML, at the same time, preferrably by one control, as if they are a single video (one is a filtered version of the other, that's why).

How can I achieve that?

Here is the simple code I have so far that plays them separately, but side by side:

<center>
<video height="350" controls>
  <source src="video1.mp4" type="video/mp4">
</video>
<video height="350" controls>
  <source src="video2.mp4" type="video/mp4">
</video>
</center>
1 Answers

You can stream from one media element to another. It should be pretty instantaneous. But then again, it would be the same source.

/*
 *  Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree.
 */
'use strict';

const leftVideo = document.getElementById('leftVideo');
const rightVideo = document.getElementById('rightVideo');

leftVideo.addEventListener('play', () => {
  let stream;
  const fps = 0;
  if (leftVideo.captureStream) {
    stream = leftVideo.captureStream(fps);
  } else if (leftVideo.mozCaptureStream) {
    stream = leftVideo.mozCaptureStream(fps);
  } else {
    console.error('Stream capture is not supported');
    stream = null;
  }
  rightVideo.srcObject = stream;
  rightVideo.play()
});
video {
  width: 200px;
  float: left;
}
<video id="leftVideo" height="350" controls crossorigin="anonymous">
  <source  src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" type="video/mp4"></source>
</video>
<video id="rightVideo" height="350" controls crossorigin="anonymous">

</video>

Related