mediarecorder-utils
v1.0.0
Published
Zero-dependency WebM & fragmented MP4 duration header patcher, binary duration parser, and lossless stream-copy slicer for browser MediaRecorder blobs.
Maintainers
Readme
mediarecorder-utils
Zero-dependency utilities for fixing, inspecting, and losslessly trimming WebM and fragmented MP4 (fMP4) blobs produced by browser MediaRecorder.
Features
patchWebmDuration(blob, durationMs)— Patches or inserts the EBMLDuration(0x4489) element in ChromiumMediaRecorderWebM files so players display duration and allow seeking.patchMp4Duration(blob, durationMs)— Updates ISO BMFFmvhd,tkhd, andmdhdduration headers in fragmented MP4 files.parseWebmDuration(blob)/parseMp4Duration(blob)/parseVideoDuration(blob, filename?)— Fast binary duration inspection (<5 ms) without decoding or<video>element loading.sliceWebmLossless(blob, segments, totalKeptSec)/sliceMp4Lossless(...)/sliceVideoLossless(...)— Zero-re-encode binary stream-copy trimming (EBML clusters / fMP4moof+mdatpairs) with timestamp rewriting.- Zero dependencies — Works in browsers, Web Workers, and Node.js 18+ (
ESM,CJS, and<script>UMD bundles + TypeScript definitions).
Install
npm install mediarecorder-utilsUsage
import {
patchWebmDuration,
patchMp4Duration,
parseVideoDuration,
sliceVideoLossless
} from 'mediarecorder-utils';
// 1. Fix missing duration on a MediaRecorder WebM blob
const fixedWebm = await patchWebmDuration(rawBlob, elapsedMs);
// 2. Inspect duration directly from binary headers
const durationSec = await parseVideoDuration(fixedWebm, 'recording.webm');
// 3. Losslessly trim segments [0s..5s] and [10s..15s]
const trimmed = await sliceVideoLossless(
fixedWebm,
[
{ start: 0, end: 5 },
{ start: 10, end: 15 }
],
10,
'webm'
);Subpath Exports
mediarecorder-utils— All functionsmediarecorder-utils/webm-duration—patchWebmDurationmediarecorder-utils/mp4-duration—patchMp4Durationmediarecorder-utils/lossless— Slicers & duration parsersmediarecorder-utils/browser/fix-webm-duration— Browser UMD<script>bundlemediarecorder-utils/browser/fix-mp4-duration— Browser UMD<script>bundlemediarecorder-utils/browser/lossless-slicer— Browser UMD<script>bundle
License
MIT
