An experimental HTML5 & WebGL video composition and rendering API.
An experimental HTML5 & WebGL video composition and rendering API.
The VideoContext is an experimental HTML5/WebGL media processing and sequencing library for creating interactive and responsive videos on the web.
It consists of two main components. A graph based, shader accelerated processing pipeline, and a media playback sequencing timeline.
The design is heavily inspired by the Web Audio API, so it should feel familiar for people with experience in the Web Audio world.
Live examples can be found here
View on CodeSandbox.
…If you need to debug video context graphs or get a better insight into what is happening under the hood, there's a new browser extension for chrome, videocontext-devtools
API Documentation can be built using ESDoc by running the following commands:
yarn install
yarn docThe documentation will be generated in the "./doc" folder of the repository.
There are a number of different types of nodes which can be used in the VideoContext's processing graph. Here's a quick list of each one. Following that is a more in-depth discussion of each type.
A video source node.
View on CodeSandbox.
var videoNode = videoCtx.video("./video1.mp4");
videoNode.connect(videoCtx.destination);
videoNode.start(0);
videoNode.stop(4);For best results the video played by a VideoNode should be encoded with a fast decode profile. The following avconv line shows how this can be achieved.
avconv -i input.mp4 -tune fastdecode -strict experimental output.mp4An audio source node.
View on CodeSandbox.
var audioNode = videoCtx.audio("./audio.mp3");
audioNode.connect(videoCtx.destination);
audioNode.start(0);
audioNode.stop(4);An image source node.
View on CodeSandbox.
var imageNode = videoCtx.image("cats.png");
imageNode.connect(videoCtx.destination);
imageNode.start(0);
imageNode.stop(4);A canvas source node.
View on CodeSandbox.
var canvas = document.getElementById("input-canvas");
var canvasNode = videoCtx.canvas(canvas);
canvasNode.connect(videoCtx.destination);
canvasNode.start(0);
canvasNode.stop(4);Sometimes using the pre-built node is just not enough. You can create your own source nodes that host more logic and let you hook into the VideoContext Node API easily.
The following example shows how to create a custom source node that can play a HLS VOD:
View on CodeSandbox
…Another use case for custom node types would be to play GIFs. The custom node would be in charge of decode the GIF frames and paint them on a canvas depending on the _update calls from VideoContext.
An EffectNode is the simplest form of processing node. It's built from a definition object, which is a combination of fragment shader code, vertex shader code, input descriptions, and property descriptions. There are a number of common operations available as node descriptions accessible as static properties on the VideoContext at VideoContext.DEFINITIONS.
The vertex and shader code is GLSL code which gets compiled to produce the shader program. The input description tells the VideoContext how many ports there are to connect to and the name of the image associated with the port within the shader code. Inputs are always render-able textures (i.e images, videos, canvases). The property descriptions tell the VideoContext what controls to attached to the EffectNode and the name, type, and default value of the control within the shader code.
The following is a an example of a simple shader description used to describe a monochrome effect. It has one input (the image to be processed) and two modifiable properties to control the color RGB mix for the processing result.
View on CodeSandbox.
…Here's an example of how the above node description might be used to apply sepia like effect to a video.
…Transition nodes are a type of effect node which allow the automatic modification/tweening of properties in relation to the VideoContexts notion of time. In every respect they are the same as an effect node except they have a "transition" function which can be used to cue the transitioning of a shader property from one value to another.
You can use them to perform a video transition effect (such as cross-fades, wipes, etc) by creating a definition with two inputs and having a property which controls the mix of the two inputs in the output buffer.
The following is an example of a simple cross-fade shader.
View on CodeSandbox.
…The shader has two inputs and a mix property.
…Compositing nodes are different from regular effect nodes as they can have an infinite number of nodes connected to them. They operate by running their effect shader on each connected input in turn and rendering the output to the same texture. This makes them particularly suitable for layering inputs which have alpha channels.
When compositing nodes are run, they map each input in turn to the first input in the definition. This means compositing node definitions typically only have a single input defined. It's also worth noting that an effect node definition with a single input can also be used as a compositing shader with no additional modifications.
A common use for compositing nodes is to collect a series of source nodes which exist at distinct points on a timeline into a single connection for passing onto further processing. This effectively makes the sources into a single video track.
Here's a really simple shader which renders all the inputs to the same output.
View on CodeSandbox.
…And here's an example of how it can be used.
…Making custom effect shaders for the VideoContext is fairly simple. The best starting point is to take one of the built in effects and modify it. It's very useful to have an understanding of how shaders work and some experience writing shaders in GLSL.
var effectDefinition ={
title:"", //A title for the effect.
description: "", //A textual description of what the effect does.
vertexShader : "", //The vertex shader
fragmentShader : "", //The fragment shader
properties:{ //An object containing uniforms from the fragment shader for mapping onto the effect node.
},
inputs:["u_image"] //the names of the uniform sampler2D's in the fragment shader which represent the texture inputs to the effect.
};You can view more advanced usage examples here.
VideoContext has a pretty standard package.json
# install build and development dependencies
yarn install
# run a dev server with automatic reload
yarn dev
# watch unit and integration tests
yarn test-watch
# run the end-to-end regression tests in a headless browser
yarn cypressFor more information on writing, running and debugging the end-to-end cypress tests see ./test/cypress#readme.
For an overview of all testing see ./test#readme
VideoContext uses the gitflow branching model.
To contribute raise a pull request against the develop branch.
Releases are prepared in release branches. When the the release is ready run one of
yarn release:major
yarn release:minor
yarn release:patchthese scripts build and commit the docs, the changelog, update the package.json version number
and push to the current branch with tags.
CI will publish to npm when the release branch has been merged into master.
git checkout developgit pullgit checkout -b release-xxxyarn release:patch|minor|majorgit checkout mastergit pullgit checkout developgit merge mastergit pushThere is one housekeeping task (this will be automated at some point):
VideoContext uses the BBCs public travis account to run all tests and publish to npmjs. All tests must pass before PRs can be merged.
Other options
yarn build # build dist packages
yarn doc # create documentation
yarn build_all # do all of the aboveThe library is written in es6 and cross-compiled using babel.
No open issues yet, or sync has not completed.