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

kontainer-js

v2.4.0

Published

A media file format generator/parser that exposes a React-like API.

Downloads

22

Readme

Kontainer

A media file format generator/parser that exposes a React-like API. Build Status

logo

Kontainer aims to fully support the MP4 (ISO Base Media file format: ISO/IEC 14496-12) and WebM file format. The library can be used on Node.js and in the browser.

Install

NPM

Overview

logo

API

A media file like MP4 and WebM is composed of nested objects. In Kontainer, each object, e.g. MP4 Box, is represented as a KontainerElement which is similar to the ReactElement.

The actual media data (audio and video chunks) and the metadata are represented as a props object and passed to the KontainerElement as its attributes.

import Kontainer from 'kontainer-js';

export default class MP4 {

  constructor(width, height) {
    Kontainer.use('mp4'); // format (mp4|webm) needs to be set before any API call
    this.width = width;
    this.height = height;
  }

  render() {
    return (
    <file>
      <ftyp majorBrand="isom" />
      <moov>
        <mvhd creationTime={new Date()} modificationTime={new Date()} timeScale={1} nextTrackId={4} />
        <trak>
          <tkhd creationTime={new Date()} modificationTime={new Date()} trackId={1} width={this.width} height={this.height} />
          <mdia>
            ...
          </mdia>
        </trak>
      </moov>
    </file>
    );
  }
}

The above code can be transpiled into the calls to createElement() using babel and a dedicated plugin.

    // Kontainer.createElement()
    //   Accepts: type, props, children...
    //   Returns: KontainerElement
    return Kontainer.createElement('file', null,
      Kontainer.createElement('ftyp', {majorBrand: 'isom'}),
      Kontainer.createElement('moov', null,
        Kontainer.createElement('mvhd', {creationTime: new Date(0), modificationTime: new Date(), timeScale: 1, nextTrackId: 4}),
        Kontainer.createElement('trak', null,
          Kontainer.createElement('tkhd', {creationTime: new Date(0), modificationTime: new Date(), trackId: 1, width: 640, height: 480}),
          Kontainer.createElement('mdia', null,
            ...
            // KontainerElement can be a child of other elements to compose a large nested tree.
          )
        )
      )
    );

Once an element is obtained, it can be serialized into a byte stream using render().

    // Kontainer.render()
    //   Accepts: KontainerElement
    //   Returns: Buffer (in node) or ArrayBuffer (in browser) that contains a media stream
    buffer = Kontainer.render(element);

On the other hand, you can create a KontainerElement from a byte stream.

    // Kontainer.createElementFromBuffer()
    //   Accepts: Buffer (in node) or ArrayBuffer (in browser) that contains a media stream [, offset=0]
    //   Returns: KontainerElement.
    element = Kontainer.createElementFromBuffer(buffer, offset);

You can also create your hook and process a byte stream progressively. (This works only for Node.js)

  const input = fs.createReadStream('./test.mp4');
  const transform = Kontainer.transform((type, props, children) => {
    if (type === 'tkhd') {
      // Change video dimensions
      props.width /= 2;
      props.height /= 2;
    }
  });
  input.pipe(transform).pipe(process.stdout);

JSX

To transpile JSX into createElement() calls together with your ES.next code, you need to install babel and its plugins.

$ npm install babel-cli
$ npm install babel-preset-es2015
$ npm install babel-plugin-transform-kontainer-js

Put a .babelrc file in the source directories that contain JSX.

{
  "presets": ["es2015"],
  "plugins": ["transform-kontainer-js"]
}

Then use the babel command to transpile the code.

$ babel src/ -d dist/

See the plugin code for the details.

CLI

A simple parser for displaying the structure of a media file.

Usage:
    kontainer filePath [options]

Example:
    kontainer /path/to/file
    cat /path/to/file | kontainer

Options:
  -h, --help    Print help
  -v, --version Print version

Development

// Install
$ git clone [email protected]:kuu/Kontainer.git
$ cd Kontainer
$ npm install

// Test
$ npm test

// Build client libs
$ npm run build
// --> ./lib/kontainer.deb.js (uncompressed with debug messages)
// --> ./lib/kontainer.js (uncompressed)
// --> ./lib/kontainer.min.js (compressed)
// --> ./lib/kontainer.map.js (source map)