• Stars
    star
    272
  • Rank 151,235 (Top 3 %)
  • Language
    JavaScript
  • Created over 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

JavaScript-based WebM video encoder for Google Chrome

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 when running in a privileged context like a Chrome extension or Electron app, 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 some browsers size limits on Blobs, particularly mobile browsers, check out the Blob size limits.

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, or a similar environment like Electron. It will throw an exception on other browsers or on vanilla Node.

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.3.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 0.99999 (best), 1.00 (VP8L lossless) is not supported
    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

    transparent: false,      // True if an alpha channel should be included in the video
    alphaQuality: undefined, // Allows you to set the quality level of the alpha channel separately.
                             // If not specified this defaults to the same value as `quality`.
});

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

videoWriter.addFrame(canvas);

You can override the duration of a specific frame in milliseconds like so:

videoWriter.addFrame(canvas, 100);

Note that if the canvas' dimensions change between frames, the resulting WebM video may not be compatible with all players, because the frame dimensions will differ from the overall track's dimensions. This could be improved in the future.

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.

Transparent WebM support

Transparent WebM files are supported, check out the example in test/transparent. However, because I'm re-using Chrome's WebP encoder to create the alpha channel, and the alpha channel is taken from the Y channel of a YUV-encoded WebP frame, and Y values are clamped by Chrome to be in the range 22-240 instead of the full 0-255 range, the encoded video can neither be fully opaque or fully transparent :(.

Sorry, I wasn't able to find a workaround to get that to work.

License

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

More Repositories

1

wacom-driver-fix

Fixes the Wacom Bamboo, Graphire, Intuos 1+2+3 and Cintiq 1st gen tablet drivers for macOS Catalina, Big Sur, Monterey (including M1 macs)
Makefile
1,409
star
2

chickenpaint

An HTML5 Port of the ChibiPaint multi-layer Oekaki painting tool
JavaScript
116
star
3

snap-to-s3

Upload EBS volume snapshots to Amazon S3/Glacier
JavaScript
114
star
4

steal-chads-password

Code to brute-force a specific incomplete QR code
Java
104
star
5

PlanC

Restore local CrashPlan Home backups even after Code42 shut it down in October 2018, now with Small Business support!
C++
81
star
6

pve-edk2-firmware

Fork of Proxmox's OVMF firmware package for fixing macOS support
Makefile
40
star
7

lofipi-mpv

Automatically stream lo-fi hiphop at system startup on the Lofipi
JavaScript
39
star
8

InnoCallback

Export Inno Setup routines as stdcall callbacks to DLLs such as the Windows API
Pascal
30
star
9

asyncfatfs

Asynchronous FAT16/32 filesystem for embedded devices
C
29
star
10

proxmox-on-ec2

A guide to installing Proxmox VE 7 on AWS EC2
28
star
11

edk2

Fork of EDK2 for macOS compatibility
C
19
star
12

InnoTools-Downloader

Plugin for InnoSetup that allows files to be downloaded during installation
Pascal
18
star
13

PetzA

Plugin for Petz (Dogz and Catz) and Babyz games
Pascal
17
star
14

pve-qemu

Personal fork of Proxmox 6's QEMU for testing patches
Python
13
star
15

petz-file-formats

Details on decoding files from the Petz/Dogz/Catz/Babyz set of games
JavaScript
12
star
16

petz-common

Common routines for Petz utilities
Pascal
8
star
17

c42-adbtool

Read and modify Code42/CrashPlan UDB and ADB databases
C++
7
star
18

chibipaint

Chicken Smoothie's modified version of Marc Schefer's ChibiPaint
Java
6
star
19

qemu-server

A fork for testing patches on Proxmox's VM management package
Perl
6
star
20

wacom-qemu

Emulated Wacom tablet devices for QEMU
C
5
star
21

fifo-split

Split a stream into multiple chunks
C++
4
star
22

chickenpaint-example

Example of including ChickenPaint using NPM
HTML
3
star
23

pve-qemu-kvm

Personal branch of Proxmox 4.4's QEMU version for testing patches
Makefile
2
star
24

ss-delphi-components

Random Delphi components
Pascal
1
star
25

stubby

In-memory code patching tool for Delphi (unmaintained)
Pascal
1
star
26

synapse

Git clone of the Ararat Synapse library's SVN repository
Pascal
1
star