@megh-computing/mp4-decoder
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

MP4 Decoder - Megh Computing

npm npm dependencies node npm type definitions

Decodes a (live) fragmented MP4 video stream into individual frames using the Media Source Extensions (MSE) API. This package cannot be used to play pre-recorded video unless the video data is appended to the decoder at the playback rate (real-time), which is not typically feasible without decoding/parsing the MP4 file and inspecting the sample timings.

Features

  • Hybrid package: CommonJS and ESM supported
  • Modern (ES2020+) browsers supported: Chrome and Edge, Firefox works with significantly reduced performance
  • First-class TypeScript support

Environment

This package only works in the browser. It works best in browsers with support for the experimental HTMLVideoElement.requestVideoFrameCallback() function. Currently, the only browsers that have support for this are Chromium-based: Chrome and Edge.

Firefox still works by using a fallback algorithm but incurs a substantial performance hit. Firefox also seems to have performance issues decoding live video using MSE.

Installation

Install the package as a dependency using npm:

$ npm install @megh-computing/mp4-decoder

or, install using yarn:

$ yarn add @megh-computing/mp4-decoder

Example Usage

import {MP4Decoder} from "@megh-computing/mp4-decoder";

...

// Create decoder and configure events
const decoder: MP4Decoder = new MP4Decoder();
decoder.on("frame", (frame: ImageFrame, time: number) => {
    // Use decoded frame and its presentation timestamp
    // (Pass it to the metadata synchronizer)
});
decoder.on("error", (type, message) => {
    console.error("Decode error:", type, message);
    // Dispose decoder with reason
    decoder.dispose("decode");
    // Close MP4 socket
    socket.close(4000, "decode error");
    // Re-connect to MP4 socket?
});

// Optional configuration (defaults provided)
decoder.setFastForwardThreshold(0.25); // How far behind live can the decoder get before playback is set to 2x
decoder.setSeekThreshold(1.0); // How far behind live can the decoder get before seeking to live
decoder.setPruneThreshold(120.0); // How old media data is before it's pruned
decoder.setPruneTimeSpan(30.0); // How much media data is pruned at a time

...

// Append MP4 data to the decoder
decoder.append(data);

...

// If the stream restarts (indicated by the socket closing),
// then dispose the decoder and create a new one when the socket reconnects
decoder.dispose();

License

Copyright (c) 2021 Megh Computing, Inc.

All rights reserved. No warranty, explicit or implied, provided. Unauthorized use, modification, or distribution is strictly prohibited. Homepage: https://megh.com/

Please contact us if you use Megh VAS and would like to be issued a license to use this package.

Readme

Keywords

none

Package Sidebar

Install

npm i @megh-computing/mp4-decoder

Homepage

megh.com/

Weekly Downloads

62

Version

0.2.0

License

UNLICENSED

Unpacked Size

22.4 kB

Total Files

7

Last publish

Collaborators

  • btodell