HTML5 does not support alpha channeled video (such as MOV 32-bit etc.).
The only option is to use matte composited like your video combined with a canvas element. However, canvas only composite already existing alpha channel, and as a matte is a solid layer and not an alpha, you have to convert it to alpha.
This means you have to draw and iterate each frame to a canvas, and convert the grey scale to transparency levels. However, this is very CPU intensive and will affect the computer performance/resources. The video must also fulfill CORS requirement in order to not taint the canvas (in which case you can't extract the pixels).
Here is how
For each frame:
- grab a level from any channel for each pixel, we'll use red here
- subtract the value from 255 since matte white here will be the target (reversed, will reveal)
- clear image data which is the matte itself or you will get the white matte leaking into the data
- set the calculated value as new alpha channel value
Example loop (assuming video is created dynamically, has its source(s) set and has triggered event for starting - canvas is placed where you want to mask and context ctx
is initialized):
ctx.globalCompositeOperation = "copy"; // will clear previous alpha
function unmask() {
ctx.drawImage(video, 0, 0); // draw current video frame
var idata = ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height),
data = idata.data,
len = data.length, i = 0; // ..image data from canvas
while(i < len) {
var alpha = 255 - data[i]; // in this matte, white = fully transparent
data[i] = data[i+1] = data[i+2] = 0; // clear matte to black
data[i+3] = alpha; // set alpha
i += 4; // next pixel
}
ctx.putImageData(idata, 0, 0); // update canvas
requestAnimationFrame(unmask); // todo: add criteria to stop this
}
Speed-up tip: use blacks (or pre-invert the matte video), then use Uint32Array view on the data, right-shift a pixel long-word (<<24) back in place.
There is a slight possibility that you will be able to do this with the new CSS custom filters (aka CSS shaders), but this is not widely supported at the time of this writing. It will also require the use of shader language (GLSL). But I found it worth mentioning at least.