2.2.0 • Published 3 months ago

video-worker v2.2.0

Weekly downloads
8,169
License
MIT
Repository
github
Last release
3 months ago

Video Worker

video-worker.min.js

API wrapper for Youtube, Vimeo and Self-Hosted videos

Table of Contents

Import VideoWorker

Use one of the following examples to import script.

ESM

We provide a version of VideoWorker built as ESM (video-worker.esm.js and video-worker.esm.min.js) which allows you to use VideoWorker as a module in your browser, if your targeted browsers support it.

<script type="module">
  import VideoWorker from "video-worker.esm.min.js";
</script>

ESM CDN

<script type="module">
  import VideoWorker from "https://cdn.jsdelivr.net/npm/video-worker@2/+esm";
</script>

UMD

VideoWorker may be also used in a traditional way by including script in HTML and using library by accessing window.VideoWorker.

<script src="video-worker.min.js"></script>

UMD CDN

<script src="https://cdn.jsdelivr.net/npm/video-worker@2/dist/video-worker.min.js"></script>

CJS (Bundlers like Webpack)

Install VideoWorker as a Node.js module using npm

npm install video-worker

Import VideoWorker by adding this line to your app's entry point (usually index.js or app.js):

import VideoWorker from 'video-worker';

Use VideoWorker

import VideoWorker from 'video-worker';

const videoObject = new VideoWorker('https://www.youtube.com/watch?v=ab0TSkLe-E0');

if (videoObject.isValid()) {
  // retrieve iframe/video dom element.
  videoObject.getVideo((video) => {
    const $parent = video.parentNode;

    // insert video in the body.
    document.body.appendChild(video);

    // remove temporary parent video element (created by VideoWorker).
    $parent.parentNode.removeChild($parent);
  });
}

Video URLs examples:

  • YouTube https://www.youtube.com/watch?v=ab0TSkLe-E0
  • Vimeo https://vimeo.com/110138539
  • Self Hosted mp4:./self-hosted-video.mp4,webm:./self-hosted-video.webm,ogv:./self-hosted-video.ogv

Note: for self-hosted videos required only 1 video type, not necessary use all mp4, webm and ogv. This need only for maximum compatibility with all browsers.

Options

NameTypeDefaultDescription
autoplayboolfalseVideo autoplay.
loopboolfalseVideo playing loop.
showControlsbooltrueVideo controls.
accessibilityHiddenboolfalseAdd accessibility attributes for videos used on backgrounds.
muteboolfalseMute sound.
volumeint100Volume level from 0 to 100.
startTimefloat0Start time in seconds when video will be started (this value will be applied also after loop).
endTimefloat0End time in seconds when video will be ended.

Example

new VideoWorker('<URL_TO_YOUR_VIDEO>', {
  autoplay: true,
  loop: true,
  startTime: 10,
});

Events

NameParametersDescription
readyeventFires only once, when the video is ready to play.
volumechangeeventFires when video volume changed.
timeupdateeventFires when video current time changed.
startedeventFires only once, when the video is started playing.
playeventFires on video play start.
pauseeventFires on video paused.
endedeventFires on video ended.
errorerrorFires on video error

Example

videoObject.on('ready', (event) => {
  console.log('video ready', event);
});

Methods

NameResultDescription
isValidboolCheck if the video is successfully determined and ready to use.
play-Play video.
pause-Pause video.
mute-Mute sound.
unmute-Unmute sound.
getMutedintGet mute state. videoObject.getMuted((muted) => { ... })
setVolume-Set volume level (takes integer value from 0 to 100). videoObject.setVolume(40);
getVolumeintGet volume level. videoObject.getVolume((volume) => { ... })
setCurrentTime-Set current time in seconds. videoObject.setCurrentTime(40);
getCurrentTimeintGet current time in seconds. videoObject.getCurrentTime((currentTime) => { ... })
getImageURLstringRetrieves Youtube/Vimeo video poster image URL. videoObject.getImageURL((url) => { ... })
getVideodomRetrieves iframe/video dom element. videoObject.getVideo((video) => { ... })

Example

videoObject.mute();

Video Providers

By default VideoWorker provides support for Youtube, Vimeo and Self-Hosted videos. There is a possibility to extend providers list and add support for different platform.

Example:

VideoWorker.providers.MyVideoProvider = class MyVideoProvider extends VideoWorker.BaseClass {
  type = 'my-video-provider';
  ...
}

All available methods for custom provider class you can find in existing providers - https://github.com/nk-o/video-worker/tree/master/src/providers

For Developers

Installation

  • Run npm install in the command line. Or if you need to update some dependencies, run npm update

Building

  • npm run build to run build

Linting

  • npm run js-lint to show eslint errors
  • npm run js-lint-fix to automatically fix some of the eslint errors
2.2.0

3 months ago

2.1.2

1 year ago

2.1.1

1 year ago

2.1.4

1 year ago

2.1.3

1 year ago

2.1.5

1 year ago

2.1.0

1 year ago

2.0.0

2 years ago

1.1.17

3 years ago

1.1.16

3 years ago

1.1.15

3 years ago

1.1.14

3 years ago

1.1.13

3 years ago

1.1.12

3 years ago

1.1.11

3 years ago

1.1.10

3 years ago

1.1.9

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

5 years ago

1.1.5

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago