shlajin / webm-writer-js

JavaScript-based WebM video encoder for Google Chrome

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

WebM Writer for JavaScript

This is a JavaScript-based WebM video encoder based on the ideas from Whammy. It allows you to turn a series of Canvas frames into a WebM video.

This implementation allows you to create very large video files (exceeding the size of available memory), because it can stream chunks immediately to a file on disk using Chrome's FileWriter while the video is being constructed, instead of needing to buffer the entire video in memory before saving can begin. Video sizes in excess of 4GB can be written. The implementation currently tops out at 32GB, but this could be extended.

When a FileWriter is not available, it can instead buffer the video in memory as a series of Blobs which are eventually returned to the calling code as one composite Blob. This Blob can be displayed in a <video> element, transmitted to a server, or used for some other purpose. Note that Chrome has a Blob size limit of 500MB.

Compatibility

Because this code relies on browser support for encoding a Canvas as a WebP image (using toDataURL()), it is presently only supported in Google Chrome. It will throw an exception on other browsers.

Usage (Chrome)

Download the script from the Releases tab above. You should end up with a webm-writer-x.x.x.js file to add to your project.

Include the script in your header:

<script type="text/javascript" src="webm-writer-0.1.0.js"></script>

First construct the writer, passing in any options you want to customize:

var videoWriter = new WebMWriter({
    quality: 0.95,    // WebM image quality from 0.0 (worst) to 1.0 (best)
    fileWriter: null, // FileWriter in order to stream to a file instead of buffering to memory (optional)
    fd: null,         // Node.js file handle to write to instead of buffering to memory (optional)

    // You must supply one of:
    frameDuration: null, // Duration of frames in milliseconds
    frameRate: null,     // Number of frames per second
});

Add as many Canvas frames as you like to build your video:

videoWriter.addFrame(canvas);

When you're done, you must call complete() to finish writing the video:

videoWriter.complete();

complete() returns a Promise which resolves when writing is completed.

If you didn't supply a fileWriter or fd in the options, the Promise will resolve to Blob which represents the video. You could display this blob in an HTML5 <video> tag:

videoWriter.complete().then(function(webMBlob) {
    $("video").attr("src", URL.createObjectURL(webMBlob));
});

Usage (Electron)

The video encoder can use Node.js file APIs to write the video to disk when running under Electron. There is an example in test/electron. Run npm install in that directory to fetch required libraries, then npm start to launch Electron.

License

This project is licensed under the WTFPLv2 https://en.wikipedia.org/wiki/WTFPL

About

JavaScript-based WebM video encoder for Google Chrome


Languages

Language:JavaScript 95.8%Language:HTML 3.9%Language:Makefile 0.4%