mediarecorder-utils 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +58 -2
- package/dist/browser/fix-mp4-duration.js +118 -0
- package/dist/browser/fix-webm-duration.js +113 -0
- package/dist/browser/lossless-slicer.js +696 -0
- package/dist/index.cjs +26 -0
- package/dist/index.mjs +12 -0
- package/dist/lossless-slicer.cjs +680 -0
- package/dist/lossless-slicer.mjs +670 -0
- package/dist/mp4-duration.cjs +108 -0
- package/dist/mp4-duration.mjs +103 -0
- package/dist/webm-duration.cjs +103 -0
- package/dist/webm-duration.mjs +98 -0
- package/package.json +63 -4
- package/src/index.js +12 -0
- package/src/lossless-slicer.js +670 -0
- package/src/mp4-duration.js +103 -0
- package/src/webm-duration.js +98 -0
- package/types/index.d.ts +72 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Recorder Studio Pro Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,59 @@
|
|
|
1
|
-
#
|
|
1
|
+
# mediarecorder-utils
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Zero-dependency utilities for fixing, inspecting, and losslessly trimming **WebM** and **fragmented MP4 (fMP4)** blobs produced by browser `MediaRecorder`.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **`patchWebmDuration(blob, durationMs)`** — Patches or inserts the EBML `Duration` (`0x4489`) element in Chromium `MediaRecorder` WebM files so players display duration and allow seeking.
|
|
8
|
+
- **`patchMp4Duration(blob, durationMs)`** — Updates ISO BMFF `mvhd`, `tkhd`, and `mdhd` duration headers in fragmented MP4 files.
|
|
9
|
+
- **`parseWebmDuration(blob)` / `parseMp4Duration(blob)` / `parseVideoDuration(blob, filename?)`** — Fast binary duration inspection (<5 ms) without decoding or `<video>` element loading.
|
|
10
|
+
- **`sliceWebmLossless(blob, segments, totalKeptSec)` / `sliceMp4Lossless(...)` / `sliceVideoLossless(...)`** — Zero-re-encode binary stream-copy trimming (EBML clusters / fMP4 `moof`+`mdat` pairs) with timestamp rewriting.
|
|
11
|
+
- **Zero dependencies** — Works in browsers, Web Workers, and Node.js 18+ (`ESM`, `CJS`, and `<script>` UMD bundles + TypeScript definitions).
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install mediarecorder-utils
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
import {
|
|
23
|
+
patchWebmDuration,
|
|
24
|
+
patchMp4Duration,
|
|
25
|
+
parseVideoDuration,
|
|
26
|
+
sliceVideoLossless
|
|
27
|
+
} from 'mediarecorder-utils';
|
|
28
|
+
|
|
29
|
+
// 1. Fix missing duration on a MediaRecorder WebM blob
|
|
30
|
+
const fixedWebm = await patchWebmDuration(rawBlob, elapsedMs);
|
|
31
|
+
|
|
32
|
+
// 2. Inspect duration directly from binary headers
|
|
33
|
+
const durationSec = await parseVideoDuration(fixedWebm, 'recording.webm');
|
|
34
|
+
|
|
35
|
+
// 3. Losslessly trim segments [0s..5s] and [10s..15s]
|
|
36
|
+
const trimmed = await sliceVideoLossless(
|
|
37
|
+
fixedWebm,
|
|
38
|
+
[
|
|
39
|
+
{ start: 0, end: 5 },
|
|
40
|
+
{ start: 10, end: 15 }
|
|
41
|
+
],
|
|
42
|
+
10,
|
|
43
|
+
'webm'
|
|
44
|
+
);
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Subpath Exports
|
|
48
|
+
|
|
49
|
+
- `mediarecorder-utils` — All functions
|
|
50
|
+
- `mediarecorder-utils/webm-duration` — `patchWebmDuration`
|
|
51
|
+
- `mediarecorder-utils/mp4-duration` — `patchMp4Duration`
|
|
52
|
+
- `mediarecorder-utils/lossless` — Slicers & duration parsers
|
|
53
|
+
- `mediarecorder-utils/browser/fix-webm-duration` — Browser UMD `<script>` bundle
|
|
54
|
+
- `mediarecorder-utils/browser/fix-mp4-duration` — Browser UMD `<script>` bundle
|
|
55
|
+
- `mediarecorder-utils/browser/lossless-slicer` — Browser UMD `<script>` bundle
|
|
56
|
+
|
|
57
|
+
## License
|
|
58
|
+
|
|
59
|
+
MIT
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
// Generated by mediarecorder-utils — Client-Side MP4 Duration Header Patcher
|
|
2
|
+
(function (global) {
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Client-Side MP4 Duration Header Patcher.
|
|
7
|
+
* Parses ISO BMFF box structure and patches duration fields in mvhd, tkhd, and mdhd boxes.
|
|
8
|
+
* @param {Blob} blob - Raw MP4 Blob from MediaRecorder.
|
|
9
|
+
* @param {number} durationMs - Actual recorded session duration in milliseconds.
|
|
10
|
+
* @returns {Promise<Blob>} - Patched, seekable MP4 Blob.
|
|
11
|
+
*/
|
|
12
|
+
async function patchMp4Duration(blob, durationMs) {
|
|
13
|
+
if (!blob || !(blob instanceof Blob) || !durationMs || durationMs <= 0) {
|
|
14
|
+
return blob;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
try {
|
|
18
|
+
const buffer = await blob.arrayBuffer();
|
|
19
|
+
const view = new DataView(buffer);
|
|
20
|
+
const durationSec = durationMs / 1000;
|
|
21
|
+
let movieTimescale = 1000;
|
|
22
|
+
|
|
23
|
+
function traverseBoxes(startOffset, endOffset) {
|
|
24
|
+
let pos = startOffset;
|
|
25
|
+
while (pos + 8 <= endOffset) {
|
|
26
|
+
let boxSize = view.getUint32(pos);
|
|
27
|
+
const boxType = String.fromCharCode(
|
|
28
|
+
view.getUint8(pos + 4),
|
|
29
|
+
view.getUint8(pos + 5),
|
|
30
|
+
view.getUint8(pos + 6),
|
|
31
|
+
view.getUint8(pos + 7)
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
let headerSize = 8;
|
|
35
|
+
if (boxSize === 1) {
|
|
36
|
+
if (pos + 16 > endOffset) break;
|
|
37
|
+
const high = view.getUint32(pos + 8);
|
|
38
|
+
const low = view.getUint32(pos + 12);
|
|
39
|
+
boxSize = high * 4294967296 + low;
|
|
40
|
+
headerSize = 16;
|
|
41
|
+
} else if (boxSize === 0) {
|
|
42
|
+
boxSize = endOffset - pos;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (boxSize < headerSize || pos + boxSize > endOffset) {
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const contentPos = pos + headerSize;
|
|
50
|
+
const contentSize = boxSize - headerSize;
|
|
51
|
+
|
|
52
|
+
if (boxType === 'moov' || boxType === 'trak' || boxType === 'mdia' || boxType === 'minf') {
|
|
53
|
+
traverseBoxes(contentPos, contentPos + contentSize);
|
|
54
|
+
} else if (boxType === 'mvhd') {
|
|
55
|
+
const version = view.getUint8(contentPos);
|
|
56
|
+
if (version === 1) {
|
|
57
|
+
movieTimescale = view.getUint32(contentPos + 20);
|
|
58
|
+
const durUnits = Math.round(durationSec * movieTimescale);
|
|
59
|
+
const high = Math.floor(durUnits / 4294967296);
|
|
60
|
+
const low = durUnits >>> 0;
|
|
61
|
+
view.setUint32(contentPos + 24, high);
|
|
62
|
+
view.setUint32(contentPos + 28, low);
|
|
63
|
+
} else {
|
|
64
|
+
movieTimescale = view.getUint32(contentPos + 12);
|
|
65
|
+
const durUnits = Math.round(durationSec * movieTimescale) >>> 0;
|
|
66
|
+
view.setUint32(contentPos + 16, durUnits);
|
|
67
|
+
}
|
|
68
|
+
} else if (boxType === 'tkhd') {
|
|
69
|
+
const version = view.getUint8(contentPos);
|
|
70
|
+
const durUnits = Math.round(durationSec * movieTimescale);
|
|
71
|
+
if (version === 1) {
|
|
72
|
+
const high = Math.floor(durUnits / 4294967296);
|
|
73
|
+
const low = durUnits >>> 0;
|
|
74
|
+
view.setUint32(contentPos + 28, high);
|
|
75
|
+
view.setUint32(contentPos + 32, low);
|
|
76
|
+
} else {
|
|
77
|
+
view.setUint32(contentPos + 20, durUnits >>> 0);
|
|
78
|
+
}
|
|
79
|
+
} else if (boxType === 'mdhd') {
|
|
80
|
+
const version = view.getUint8(contentPos);
|
|
81
|
+
if (version === 1) {
|
|
82
|
+
const trackTimescale = view.getUint32(contentPos + 20);
|
|
83
|
+
const durUnits = Math.round(durationSec * trackTimescale);
|
|
84
|
+
const high = Math.floor(durUnits / 4294967296);
|
|
85
|
+
const low = durUnits >>> 0;
|
|
86
|
+
view.setUint32(contentPos + 24, high);
|
|
87
|
+
view.setUint32(contentPos + 28, low);
|
|
88
|
+
} else {
|
|
89
|
+
const trackTimescale = view.getUint32(contentPos + 12);
|
|
90
|
+
const durUnits = Math.round(durationSec * trackTimescale) >>> 0;
|
|
91
|
+
view.setUint32(contentPos + 16, durUnits);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
pos += boxSize;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
traverseBoxes(0, buffer.byteLength);
|
|
100
|
+
return new Blob([buffer], { type: blob.type || 'video/mp4' });
|
|
101
|
+
} catch (err) {
|
|
102
|
+
console.warn('MP4 duration header patch failed, returning original blob:', err);
|
|
103
|
+
return blob;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const ysFixMp4Duration = patchMp4Duration;
|
|
108
|
+
|
|
109
|
+
global.patchMp4Duration = patchMp4Duration;
|
|
110
|
+
global.ysFixMp4Duration = ysFixMp4Duration;
|
|
111
|
+
|
|
112
|
+
if (typeof module !== 'undefined' && module.exports) {
|
|
113
|
+
module.exports = {
|
|
114
|
+
patchMp4Duration,
|
|
115
|
+
ysFixMp4Duration
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
})(typeof window !== 'undefined' ? window : (typeof globalThis !== 'undefined' ? globalThis : this));
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// Generated by mediarecorder-utils — Minimal EBML duration patcher for Chrome WebM output
|
|
2
|
+
(function (global) {
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Minimal EBML duration patcher for Chromium MediaRecorder WebM output.
|
|
7
|
+
* @param {Blob} blob - Raw WebM Blob from MediaRecorder.
|
|
8
|
+
* @param {number} durationMs - Actual recorded duration in milliseconds.
|
|
9
|
+
* @returns {Promise<Blob>} - Patched, seekable WebM Blob.
|
|
10
|
+
*/
|
|
11
|
+
async function patchWebmDuration(blob, durationMs) {
|
|
12
|
+
if (!blob || !(blob instanceof Blob) || !durationMs || durationMs <= 0) {
|
|
13
|
+
return blob;
|
|
14
|
+
}
|
|
15
|
+
try {
|
|
16
|
+
const buffer = await blob.arrayBuffer();
|
|
17
|
+
const bytes = new Uint8Array(buffer);
|
|
18
|
+
const view = new DataView(buffer);
|
|
19
|
+
|
|
20
|
+
// Verify EBML Header magic [0x1A, 0x45, 0xDF, 0xA3]
|
|
21
|
+
if (bytes.length < 16 || bytes[0] !== 0x1A || bytes[1] !== 0x45 || bytes[2] !== 0xDF || bytes[3] !== 0xA3) {
|
|
22
|
+
return blob;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Locate Segment Info Element ID [0x15, 0x49, 0xA9, 0x66]
|
|
26
|
+
let pos = -1;
|
|
27
|
+
for (let i = 4; i < Math.min(bytes.length - 8, 4096); i++) {
|
|
28
|
+
if (bytes[i] === 0x15 && bytes[i + 1] === 0x49 && bytes[i + 2] === 0xA9 && bytes[i + 3] === 0x66) {
|
|
29
|
+
pos = i;
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (pos === -1) return blob;
|
|
35
|
+
|
|
36
|
+
// Read Info block size VINT
|
|
37
|
+
let infoVintLen = 0;
|
|
38
|
+
let infoVintVal = 0;
|
|
39
|
+
const b0 = bytes[pos + 4];
|
|
40
|
+
for (let i = 0; i < 8; i++) {
|
|
41
|
+
if ((b0 & (0x80 >> i)) !== 0) {
|
|
42
|
+
infoVintLen = i + 1;
|
|
43
|
+
const mask = (0x80 >> i) - 1;
|
|
44
|
+
infoVintVal = b0 & mask;
|
|
45
|
+
for (let j = 1; j < infoVintLen; j++) {
|
|
46
|
+
infoVintVal = (infoVintVal * 256) + bytes[pos + 4 + j];
|
|
47
|
+
}
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const infoContentStart = pos + 4 + (infoVintLen > 0 ? infoVintLen : 0);
|
|
53
|
+
const infoContentEnd = infoVintLen > 0 && infoVintVal < 0x0FFFFFFF
|
|
54
|
+
? Math.min(bytes.length, infoContentStart + infoVintVal)
|
|
55
|
+
: Math.min(bytes.length, pos + 1024);
|
|
56
|
+
|
|
57
|
+
// Look for existing Duration element [0x44, 0x89] inside Info block
|
|
58
|
+
for (let i = infoContentStart; i < infoContentEnd - 4; i++) {
|
|
59
|
+
if (bytes[i] === 0x44 && bytes[i + 1] === 0x89) {
|
|
60
|
+
const size = bytes[i + 2];
|
|
61
|
+
if (size === 0x84 || size === 4) {
|
|
62
|
+
view.setFloat32(i + 3, durationMs);
|
|
63
|
+
return new Blob([buffer], { type: blob.type || 'video/webm' });
|
|
64
|
+
} else if (size === 0x88 || size === 8) {
|
|
65
|
+
view.setFloat64(i + 3, durationMs);
|
|
66
|
+
return new Blob([buffer], { type: blob.type || 'video/webm' });
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// If Duration element [0x44, 0x89] is absent in Info, insert it [0x44, 0x89, 0x88, 8-byte float64] (11 bytes)
|
|
72
|
+
const durationElemBytes = new Uint8Array(11);
|
|
73
|
+
durationElemBytes[0] = 0x44;
|
|
74
|
+
durationElemBytes[1] = 0x89;
|
|
75
|
+
durationElemBytes[2] = 0x88;
|
|
76
|
+
new DataView(durationElemBytes.buffer).setFloat64(3, durationMs);
|
|
77
|
+
|
|
78
|
+
const newBytes = new Uint8Array(bytes.length + 11);
|
|
79
|
+
newBytes.set(bytes.subarray(0, infoContentStart), 0);
|
|
80
|
+
newBytes.set(durationElemBytes, infoContentStart);
|
|
81
|
+
newBytes.set(bytes.subarray(infoContentStart), infoContentStart + 11);
|
|
82
|
+
|
|
83
|
+
// Update Info size VINT in newBytes if it was a known size VINT
|
|
84
|
+
if (infoVintLen > 0 && infoVintLen <= 4 && infoVintVal < 0x0FFFFFFF) {
|
|
85
|
+
const newInfoSize = infoVintVal + 11;
|
|
86
|
+
const mask = (0x80 >> (infoVintLen - 1)) - 1;
|
|
87
|
+
let temp = newInfoSize;
|
|
88
|
+
for (let j = infoVintLen - 1; j >= 1; j--) {
|
|
89
|
+
newBytes[pos + 4 + j] = temp & 0xFF;
|
|
90
|
+
temp = Math.floor(temp / 256);
|
|
91
|
+
}
|
|
92
|
+
newBytes[pos + 4] = (0x80 >> (infoVintLen - 1)) | (temp & mask);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return new Blob([newBytes], { type: blob.type || 'video/webm' });
|
|
96
|
+
} catch (err) {
|
|
97
|
+
console.warn('patchWebmDuration notice:', err);
|
|
98
|
+
return blob;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const ysFixWebmDuration = patchWebmDuration;
|
|
103
|
+
|
|
104
|
+
global.patchWebmDuration = patchWebmDuration;
|
|
105
|
+
global.ysFixWebmDuration = ysFixWebmDuration;
|
|
106
|
+
|
|
107
|
+
if (typeof module !== 'undefined' && module.exports) {
|
|
108
|
+
module.exports = {
|
|
109
|
+
patchWebmDuration,
|
|
110
|
+
ysFixWebmDuration
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
})(typeof window !== 'undefined' ? window : (typeof globalThis !== 'undefined' ? globalThis : this));
|