Skip to content

buzz/mediainfo.js

Repository files navigation

mediainfo.js

This is a JavaScript port of the excellent MediaInfoLib and can run directly in a browser or in Node.js. It is transpiled from C++ source code using Emscripten.

Demo

Try mediainfo.js in your browser: https://mediainfo.js.org

Usage

Browser

You can either use a CDN to include the script file directly in your page or use a JavaScript bundler like webpack.

  • CDN: <script type="text/javascript" src="https://unpkg.com/mediainfo.js/dist/mediainfo.min.js"></script>
  • Bundler: npm install mediainfo.js
    Note: When using a bundler you need to make sure mediainfo.wasm can be loaded by the library. Check the React/webpack and Angular examples.

Node.js

Install mediainfo.js from NPM.

$ npm install -g mediainfo.js

You can use it directly from the shell.

$ mediainfo.js /path/to/media.avi

Or use it as a library.

require('mediainfo.js')().then((mediainfo) => {
  // mediainfo ready…
})

Examples

API

MediaInfo(opts, cb)

Create an instance of mediainfo.

Defaults: opts = { chunkSize: 1024*1024, format: 'object' }

  • As output format you can try object, JSON, XML, HTML or text. The chunk size is used by analyzeData and set to 1 MiB.
  • Returns a Promise if no callback is given.
const MediaInfo = require('mediainfo.js')
MediaInfo(opts, cb)

Media files can be several gigabytes in size. The preferred way is to load data in chunks to prevent memory exhaustion. analyzeData is a helper method that facilitates this somewhat cumbersome process.

mediainfo.analyzeData(getSize, readChunk, cb)

Convenient method for analyzing a buffer chunk by chunk.

  • You need to provide two callback functions. They can either return a Promise or directly the value.
    • getSize() - Return total buffer size.
    • readChunk(size, offset) - Read data chunk of size with offset and return an Uint8Array.
  • Returns a Promise if no callback is given.

Low-level methods

The mediainfo object also exposes a number of low-level methods analogous to the MediaInfoLib buffer methods.

close(), inform(), openBufferContinue(data, size), openBufferContinueGotoGet(), openBufferFinalize(), openBufferInit(size, offset)

Build

Install Emscripten preferably using Emscripten SDK.

$ git clone https://github.com/emscripten-core/emsdk.git
$ cd emsdk
$ ./emsdk install 1.39.15
$ ./emsdk activate 1.39.15
$ source ./emsdk_env.sh
$ export PATH=$PATH:$(pwd)/upstream/bin # for wasm-opt

Note: Versions 1.39.16 and later of Emscripten give compile errors.

In the project root of mediainfo.js run the following to build.

$ npm install
$ npm run build

Find the resulting files mediainfo.js, mediainfo.min.js and mediainfo.wasm in the dist directory.

Tests

You can run a test suite against the dist build.

$ npm run test

License

This program is freeware under BSD-2-Clause license conditions: MediaInfo(Lib) License

This product uses MediaInfo library, Copyright (c) 2002-2020 MediaArea.net SARL.