Giter Site home page Giter Site logo

tjenkinson / media-element-syncer Goto Github PK

View Code? Open in Web Editor NEW
20.0 3.0 1.0 1.55 MB

Synchronise two or more HTML5 media elements.

Home Page: https://clever-pike-bb0ab3.netlify.com/demo.html

License: MIT License

HTML 24.69% JavaScript 75.31%
mediacontroller mediaelement synchronisation sync

media-element-syncer's Introduction

npm version

Media Element Syncer

Synchronise two or more HTML5 media elements. Similar to the unimplemented MediaController api.

How?

This works by continuously adjusing the playbackRate of the media elements, or performing a seek operation if the difference is too big.

Installation

npm install --save media-element-syncer

Usage

All media elements passed to addChild will remain synchronised with the source element.

MediaElementSyncer is eligible for garbage collection when there are no children.

import { MediaElementSyncer } from 'media-element-syncer';

const source = document.getElementById('source');
const destination1 = document.getElementById('destination1');
const destination2 = document.getElementById('destination2');
const syncer = new MediaElementSyncer(source);
syncer.addChild(destination1);
syncer.addChild(destination2, { offset: -1000 });

// syncer.removeChild(destination1);

Configuration

The optional second param to MediaElementSyncer takes an object which has the following optional properties:

  • refreshInterval: how often to resync the elements. (default: 200ms)
  • correctionTime: how many milliseconds into the future to aim for being in sync. (default: 500ms)
  • seekThreshold: if the time is out by more than this amount, a seek will be performed instead of adjusting the playbackRate. (default: 1000ms)

Demo

The code for the demo is here and there is a hosted version here.

media-element-syncer's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar github-actions[bot] avatar tjenkinson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

josepowera

media-element-syncer's Issues

Audio off-sync with video on mobile

The audio and video is synced with each other perfectly in desktop, but kinda off in mobile. Maybe it is performance problem? Because my phone is old, Haven't tried in newer phone yet.

playbackRate computation for slowdown seems to be wrong

I think the playbackRate computation is wrong when the media element needs to slow down. It's at least not symmetric when compared to the speed up but maybe that's intentional.

Let's imagine the media element has a currentTime of 8.8 but it should be at the targetTime of 8.2. The diff computed as targetTime - currentTime is then -0.6. That means the following code ...

const rate = Math.max(
0,
((diff + this._correctionTime) / this._correctionTime) *
sourcePlaybackRate
);

... evaluates like this:

const rate = Math.max(0, ((-0,6 + 0,5) / 0,5) * sourcePlaybackRate);
const rate = Math.max(0, -0,2 * sourcePlaybackRate);
const rate = 0;

I guess when the media element has to slow down it should instead be something like the following:

const rate = sourcePlaybackRate / ((Math.abs(-0,6) + 0,5) / 0,5);
const rate = sourcePlaybackRate / 2.2;

What about changing the rate computation to something like this?

const rate = diff > 0
    ? ((diff + this._correctionTime) / this._correctionTime) * sourcePlaybackRate
    : sourcePlaybackRate / ((this._correctionTime - diff) / this._correctionTime);

External clock

Hi,
i am working on a solution to sync multiple video across different devices.
I am playing with ircam-ismm/sync repo (esbuild-ed to run in a webpage)
that gives a simple yet efficient way to get a common clock on each client.

Now, a suggestion to your library would be to allow an external clock, I.E. by provinding a sourceTime callaback provider instead of a source video element. That wouldn't change the logic much but it woule allow to feed the target time with other data source than an actual running player.

I'll try to implement that on top of your code and if it fits the needs, i'll submit a PR later if you feels it's a good idea.

Best,
Thomas

doesn't work in safari

It doesn't work properly in safari because changing the playback rate there causes a stutter.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.