npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@wetheredge/libavjs-webcodecs-polyfill

v0.4.0

Published

A WebCodecs polyfill (ponyfill, really), using libav.js

Downloads

1

Readme

LibAVJS-WebCodecs-Polyfill

The main difference between this version and upstream is that this is built for ES6, not ES5, which reduces filesize somewhat. It also provides a native ES Modules build by default, with a UMD version at the /umd export. Major & minor version numbers will track upstream.


This is a polyfill for the WebCodecs API.

No, really.

It supports the VideoEncoder, AudioEncoder, VideoDecoder, and AudioDecoder classes, VideoFrame-specific versions of CanvasRenderingContext2D.drawImage and createImageBitmap, and all the classes and interfaces required by these. There are no plans to implement image formats, only video and audio.

It implements WebCodecs through libav.js, which is a port of FFmpeg's library interface to WebAssembly and asm.js.

To use it, simply include libav.js then this library, and then call and await LibAVWebCodecs.load(). load takes an optional options parameter, which is an object:

options: {
    /* Polyfill: If the WebCodecs API is not provided by the browser in the
     * global object, link it to this library */
    polyfill?: boolean,

    /* Options to pass to LibAV.LibAV while constructing a LibAV instance */
    libavOptions?: any
}

Use it either by the WebCodecs API specification (if you used polyfill), or as a ponyfill, with the API under the global LibAVWebCodecs object.

If you don't bring your own libav.js, LibAVJS-WebCodecs-Polyfill will load its own, but it is highly recommended that you do not use this option, because libav.js is designed to use Web Workers, and Web Workers cannot be loaded from a different origin. This will hurt both performance and responsiveness.

You can use LibAVJS-WebCodecs-Polyfill along with a browser implementation of WebCodecs, but you cannot mix and match raw data objects from each (e.g., VideoFrames from a browser implementation of WebCodecs cannot be used in LibAV-WebCodecs-Polyfill and vice-versa). To make this practical, LibAVWebCodecs.getXY(config) (where X = Video or Audio and Y = Encoder or Decoder) are implemented, and will return a promise for an object with, e.g. VideoEncoder, EncodedVideoChunk, and VideoFrame set to either WebCodecs' or LibAVJS-WebCodecs-Polyfill's version. The promise is rejected if the configuration is unsupported.

For rendering, it is highly recommended that you use LibAVWebCodecs.createImageBitmap and draw the result on a canvas, rather than LibAVWebCodecs.canvasDrawImage, which is synchronous. LibAVWebCodecs.createImageBitmap only accepts the resizeWidth and resizeHeight options, so only the overload LibAVWebCodecs.createImageBitmap(frame, options) is supported, with options optional.

If you need the synchronous API, use LibAVWebCodecs.canvasDrawImage(ctx, ...). The first argument is the context, and the remaining arguments are as in CanvasRenderingContext2D.drawImage. It is safe to use canvasDrawImage with any image type, not just a VideoFrame; it will fall through to the original drawImage as needed. If you used the polyfill option while loading LibAVJS-WebCodecs-Polyfill, then drawImage itself will also support VideoFrames.

Compatibility

LibAVJS-WebCodecs-Polyfill should be up to date with the 2023-02-09 working draft of the WebCodecs specification: https://www.w3.org/TR/2023/WD-webcodecs-20230209/

Depending on the libav.js version used, LibAVJS-WebCodecs-Polyfill supports the audio codecs FLAC ("flac"), Opus ("opus"), and Vorbis ("vorbis"), and the video codecs AV1 ("av01"), VP9 ("vp09"), and VP8 ("vp8"). The webm-opus-flac variant, which LibAVJS-WebCodecs-Polyfill uses if no libav.js is loaded, supports FLAC, Opus, and VP8.

FFmpeg supports many codecs, and it's generally easy to add new codecs to libav.js and LibAVJS-WebCodecs-Polyfill. However, there are no plans to add any codecs by the Misanthropic Patent Extortion Gang (MPEG), so all useful codecs in the WebCodecs codec registry are supported.

LibAVJS-WebCodecs-Polyfill also supports bypassing the codec registry entirely and using any codec FFmpeg is capable of, by using the LibAVJSCodec interface (see src/libav.ts) instead of a string for the codec. For instance, VideoEncoder can be configured to use H.263+ like so:

const enc = new LibAVJS.VideoEncoder(...);
enc.configure({
    codec: {libavjs: {
        codec: "h263p",
        ctx: {
            pix_fmt: 0,
            width: settings.width,
            height: settings.height,
            framerate_num: settings.frameRate,
            framerate_den: 1
        }
    }},
    ...
});

This is useful because VP8, even in realtime mode, is really too slow to encode/decode in software in WebAssembly on many modern systems, but a simpler codec like H.263+ works in software nearly anywhere.

Limitations

The createImageBitmap polyfill is quite limited in the arguments it accepts.

libav.js is surprisingly fast for what it is, but it ain't fast. All audio codecs work fine, but video struggles. This is why support for codecs outside the codec registry was added.

libav.js also currently doesn't support multithreading, so every encoder/decoder is single-threaded. But, multiple libav.js threads can themselves be loaded, so multithreading can still be achieved by using multiple encoders/decoders simultaneously.

VideoFrame is fairly incomplete. In particular, nothing to do with color spaces is actually implemented, and nor is cropping. The initialization of frames from canvas sources has many caveats in the spec, and none in LibAVJS-WebCodecs-Polyfill, and as a consequence, timestamp is always a mandatory field of VideoFrameInit.

VideoEncoder assumes that VideoFrames passed to it are fairly sane (i.e., the planes are lain out in the obvious way).

Every time the spec specifies a sequence, LibAVJS-WebCodecs-Polyfill only works with an array.

Certain events are supposed to eagerly halt the event queue, but LibAVJS-WebCodecs-Polyfill always lets the event queue finish.

The framerate reported to video codecs is the nearest whole number to the input framerate. This should usually only affect bitrate and latency calculations, as each frame is individually timestamped.

All queues are shared per each encoder/decoder, and the ondequeue event is not supported.