nanoodle 0.2.0 → 0.4.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/README.md +72 -9
- package/bin/nanoodle.mjs +33 -5
- package/package.json +5 -1
- package/src/browser.mjs +38 -0
- package/src/catalog.mjs +22 -0
- package/src/client.mjs +116 -17
- package/src/graph.mjs +25 -9
- package/src/index.mjs +7 -2
- package/src/io.mjs +42 -2
- package/src/local-media.mjs +998 -0
- package/src/media.mjs +68 -16
- package/src/mp4cat.mjs +284 -0
- package/src/nodes.mjs +253 -37
- package/src/qr.mjs +385 -0
- package/src/share.mjs +20 -14
- package/src/workflow.mjs +102 -18
- package/src/x402.mjs +77 -0
- package/src/zlib.mjs +44 -0
package/src/media.mjs
CHANGED
|
@@ -1,10 +1,32 @@
|
|
|
1
|
-
import { readFile, writeFile } from "node:fs/promises";
|
|
2
|
-
import { extname } from "node:path";
|
|
3
1
|
import { NanoodleError } from "./errors.mjs";
|
|
4
2
|
|
|
5
3
|
/** NanoGPT's edge rejects request bodies over ~4.5 MB; media rides inline as base64 (no upload endpoint). */
|
|
6
4
|
export const MEDIA_INLINE_MAX = 4.4 * 1024 * 1024;
|
|
7
5
|
|
|
6
|
+
/* ---------- base64 (browser + Node; no hard Buffer dependency) ------------ */
|
|
7
|
+
|
|
8
|
+
/** @param {Uint8Array|ArrayBuffer|number[]} bytes */
|
|
9
|
+
export function bytesToBase64(bytes) {
|
|
10
|
+
const u8 = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes);
|
|
11
|
+
if (typeof Buffer !== "undefined") return Buffer.from(u8).toString("base64");
|
|
12
|
+
// chunked to avoid call-stack / arg limits on large media
|
|
13
|
+
const CH = 0x8000;
|
|
14
|
+
let bin = "";
|
|
15
|
+
for (let i = 0; i < u8.length; i += CH) {
|
|
16
|
+
bin += String.fromCharCode.apply(null, u8.subarray(i, i + CH));
|
|
17
|
+
}
|
|
18
|
+
return btoa(bin);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** @param {string} b64 @returns {Uint8Array} */
|
|
22
|
+
export function base64ToBytes(b64) {
|
|
23
|
+
if (typeof Buffer !== "undefined") return new Uint8Array(Buffer.from(b64, "base64"));
|
|
24
|
+
const bin = atob(b64);
|
|
25
|
+
const out = new Uint8Array(bin.length);
|
|
26
|
+
for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i);
|
|
27
|
+
return out;
|
|
28
|
+
}
|
|
29
|
+
|
|
8
30
|
/** Sniff an image's format from its base64 magic bytes (mirrors the nanoodle app runtime). */
|
|
9
31
|
export function b64ImageMime(b64) {
|
|
10
32
|
if (b64.startsWith("/9j/")) return "image/jpeg";
|
|
@@ -36,6 +58,11 @@ export function extForMime(mime) {
|
|
|
36
58
|
return MIME_EXT[String(mime || "").split(";")[0].trim().toLowerCase()] || "bin";
|
|
37
59
|
}
|
|
38
60
|
|
|
61
|
+
/** Extension → MIME (for mediaFromFile and friends). */
|
|
62
|
+
export function mimeFromExt(ext) {
|
|
63
|
+
return EXT_MIME[String(ext || "").toLowerCase()] || null;
|
|
64
|
+
}
|
|
65
|
+
|
|
39
66
|
/** Sniff a MIME type from magic bytes of common media containers. */
|
|
40
67
|
export function sniffMime(bytes) {
|
|
41
68
|
const b = bytes;
|
|
@@ -59,7 +86,7 @@ export function sniffMime(bytes) {
|
|
|
59
86
|
/** Encode bytes as a data: URL, sniffing the MIME when not given. */
|
|
60
87
|
export function bytesToDataUrl(bytes, mime) {
|
|
61
88
|
const u8 = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes);
|
|
62
|
-
return "data:" + (mime || sniffMime(u8)) + ";base64," +
|
|
89
|
+
return "data:" + (mime || sniffMime(u8)) + ";base64," + bytesToBase64(u8);
|
|
63
90
|
}
|
|
64
91
|
|
|
65
92
|
/** Decode a data: URL into { bytes, mime }. */
|
|
@@ -70,14 +97,16 @@ export function dataUrlBytes(url) {
|
|
|
70
97
|
const mime = (head.split(";")[0] || "application/octet-stream") || "application/octet-stream";
|
|
71
98
|
const body = url.slice(comma + 1);
|
|
72
99
|
const bytes = /;base64$/i.test(head) || /;base64;/i.test(head + ";")
|
|
73
|
-
?
|
|
74
|
-
: new
|
|
100
|
+
? base64ToBytes(body)
|
|
101
|
+
: new TextEncoder().encode(decodeURIComponent(body));
|
|
75
102
|
return { bytes, mime };
|
|
76
103
|
}
|
|
77
104
|
|
|
78
105
|
/**
|
|
79
106
|
* A media output value: a data: or https URL plus lazy byte access.
|
|
80
107
|
* String-coerces to the URL so it drops into templates / JSON naturally.
|
|
108
|
+
*
|
|
109
|
+
* `.save(path)` is Node-only (dynamic `node:fs`); browsers should use `.bytes()` + download.
|
|
81
110
|
*/
|
|
82
111
|
export class MediaRef {
|
|
83
112
|
/**
|
|
@@ -111,27 +140,50 @@ export class MediaRef {
|
|
|
111
140
|
return new Uint8Array(await r.arrayBuffer());
|
|
112
141
|
}
|
|
113
142
|
|
|
114
|
-
/** Write the media bytes to `path
|
|
143
|
+
/** Write the media bytes to `path` (Node only); resolves to the path. */
|
|
115
144
|
async save(path) {
|
|
145
|
+
const { writeFile } = await import("node:fs/promises");
|
|
116
146
|
await writeFile(path, await this.bytes());
|
|
117
147
|
return path;
|
|
118
148
|
}
|
|
119
149
|
}
|
|
120
150
|
|
|
121
151
|
/**
|
|
122
|
-
* Read a local file as a media input value: `{ data, mime }
|
|
152
|
+
* Read a local file as a media input value: `{ data, mime }` (Node only).
|
|
123
153
|
* MIME comes from the extension, else magic-byte sniffing.
|
|
124
154
|
*/
|
|
125
155
|
export async function mediaFromFile(path, mime) {
|
|
156
|
+
const { readFile } = await import("node:fs/promises");
|
|
157
|
+
const { extname } = await import("node:path");
|
|
126
158
|
const data = await readFile(path);
|
|
127
|
-
|
|
159
|
+
const u8 = new Uint8Array(data);
|
|
160
|
+
return { data: u8, mime: mime || mimeFromExt(extname(path)) || sniffMime(u8) };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Refuse a data: URL that exceeds NanoGPT's inline body cap.
|
|
165
|
+
* Used at network send sites and when coercing inputs for graphs that call NanoGPT.
|
|
166
|
+
* Local-only graphs (resize/combine/vframes/…) skip this — they never POST media.
|
|
167
|
+
*/
|
|
168
|
+
export function assertInlineMediaSize(url, what = "media") {
|
|
169
|
+
if (typeof url === "string" && url.startsWith("data:") && url.length > MEDIA_INLINE_MAX) {
|
|
170
|
+
throw new NanoodleError(
|
|
171
|
+
what + ": media is too large to send inline (~4 MB max). nanoodle sends media as base64 in the request body " +
|
|
172
|
+
"(NanoGPT has no upload endpoint) — use a smaller file.");
|
|
173
|
+
}
|
|
128
174
|
}
|
|
129
175
|
|
|
130
176
|
/**
|
|
131
177
|
* Coerce a user-supplied media input into a URL string (data: or http(s)).
|
|
132
178
|
* Accepts: data:/https URL strings, MediaRef, Uint8Array/Buffer, { data, mime }.
|
|
179
|
+
*
|
|
180
|
+
* @param {*} value
|
|
181
|
+
* @param {string} what label for errors
|
|
182
|
+
* @param {{ enforceInlineMax?: boolean }} [opts] when true (default), refuse data: URLs
|
|
183
|
+
* over MEDIA_INLINE_MAX. Pass false for local-only workflows that never hit NanoGPT.
|
|
133
184
|
*/
|
|
134
|
-
export function coerceMediaInput(value, what) {
|
|
185
|
+
export function coerceMediaInput(value, what, opts = {}) {
|
|
186
|
+
const enforceInlineMax = opts.enforceInlineMax !== false;
|
|
135
187
|
let url;
|
|
136
188
|
if (value instanceof MediaRef) url = value.url;
|
|
137
189
|
else if (typeof value === "string") {
|
|
@@ -142,16 +194,16 @@ export function coerceMediaInput(value, what) {
|
|
|
142
194
|
"For a local file use mediaFromFile(\"" + value.slice(0, 60) + "\").");
|
|
143
195
|
}
|
|
144
196
|
} else if (value instanceof Uint8Array) url = bytesToDataUrl(value);
|
|
145
|
-
else if (
|
|
146
|
-
|
|
197
|
+
else if (typeof Buffer !== "undefined" && Buffer.isBuffer && Buffer.isBuffer(value)) {
|
|
198
|
+
url = bytesToDataUrl(new Uint8Array(value));
|
|
199
|
+
} else if (value && typeof value === "object" && value.data != null) {
|
|
200
|
+
const data = typeof value.data === "string"
|
|
201
|
+
? base64ToBytes(value.data)
|
|
202
|
+
: new Uint8Array(value.data);
|
|
147
203
|
url = bytesToDataUrl(data, value.mime);
|
|
148
204
|
} else {
|
|
149
205
|
throw new NanoodleError(what + ": unsupported media value (" + typeof value + ")");
|
|
150
206
|
}
|
|
151
|
-
if (
|
|
152
|
-
throw new NanoodleError(
|
|
153
|
-
what + ": media is too large to send inline (~4 MB max). nanoodle sends media as base64 in the request body " +
|
|
154
|
-
"(NanoGPT has no upload endpoint) — use a smaller file.");
|
|
155
|
-
}
|
|
207
|
+
if (enforceInlineMax) assertInlineMediaSize(url, what);
|
|
156
208
|
return url;
|
|
157
209
|
}
|
package/src/mp4cat.mjs
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lossless in-browser mp4 concatenation (Combine node) — ported from nanoodle
|
|
3
|
+
* play.html / index.html MP4CAT IIFE (keep in sync; see nanoodle/scripts/check-combine.mjs).
|
|
4
|
+
*
|
|
5
|
+
* Copies compressed H.264+AAC samples onto one timeline with no decode/re-encode.
|
|
6
|
+
* Used when every clip is mp4 with matching codec params; concatVideos falls back
|
|
7
|
+
* to ffmpeg otherwise (browser falls back to MediaRecorder).
|
|
8
|
+
*/
|
|
9
|
+
const MP4CAT = (()=>{
|
|
10
|
+
const fourcc = (dv, p) => String.fromCharCode(dv.getUint8(p), dv.getUint8(p+1), dv.getUint8(p+2), dv.getUint8(p+3));
|
|
11
|
+
|
|
12
|
+
function walk(dv, start, end){
|
|
13
|
+
const out = [];
|
|
14
|
+
let p = start;
|
|
15
|
+
while(p + 8 <= end){
|
|
16
|
+
let size = dv.getUint32(p);
|
|
17
|
+
const type = fourcc(dv, p+4);
|
|
18
|
+
let hs = 8;
|
|
19
|
+
if(size === 1){ size = Number(dv.getBigUint64(p+8)); hs = 16; }
|
|
20
|
+
else if(size === 0){ size = end - p; }
|
|
21
|
+
if(size < 8 || p + size > end) break;
|
|
22
|
+
out.push({ type, start: p, end: p+size, body: p+hs });
|
|
23
|
+
p += size;
|
|
24
|
+
}
|
|
25
|
+
return out;
|
|
26
|
+
}
|
|
27
|
+
const find = (boxes, type) => boxes.find(b => b.type === type);
|
|
28
|
+
|
|
29
|
+
// Scan a byte range for a box of the given 4cc and return its bytes (used to pull avcC/esds out of
|
|
30
|
+
// an stsd for the match gate — the surrounding sample entry can carry clip-specific boxes like btrt).
|
|
31
|
+
function scanForBox(u8, type){
|
|
32
|
+
const dv = new DataView(u8.buffer, u8.byteOffset, u8.byteLength);
|
|
33
|
+
for(let p=0; p+8<=u8.length; p++){
|
|
34
|
+
if(fourcc(dv, p+4) === type){
|
|
35
|
+
const size = dv.getUint32(p);
|
|
36
|
+
if(size >= 8 && p + size <= u8.length) return u8.slice(p, p+size);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Parse one mp4 into { moovTimescale, tracks:[{kind, timescale, stsdRaw, samples:[{offset,size,dur,cts,sync}], width,height}] }
|
|
43
|
+
function parseMp4(u8){
|
|
44
|
+
const dv = new DataView(u8.buffer, u8.byteOffset, u8.byteLength);
|
|
45
|
+
const top = walk(dv, 0, u8.byteLength);
|
|
46
|
+
const moov = find(top, "moov");
|
|
47
|
+
if(!moov) throw new Error("no moov");
|
|
48
|
+
const moovBoxes = walk(dv, moov.body, moov.end);
|
|
49
|
+
const traks = moovBoxes.filter(b => b.type === "trak");
|
|
50
|
+
const tracks = [];
|
|
51
|
+
for(const trak of traks){
|
|
52
|
+
const tb = walk(dv, trak.body, trak.end);
|
|
53
|
+
const mdia = find(tb, "mdia"); if(!mdia) continue;
|
|
54
|
+
const mb = walk(dv, mdia.body, mdia.end);
|
|
55
|
+
const mdhd = find(mb, "mdhd");
|
|
56
|
+
const hdlr = find(mb, "hdlr");
|
|
57
|
+
const mdhdV1 = dv.getUint8(mdhd.body) === 1;
|
|
58
|
+
// v0: [ver/flags 4][ctime 4][mtime 4][timescale 4]; v1: [ver/flags 4][ctime 8][mtime 8][timescale 4]
|
|
59
|
+
const timescale = dv.getUint32(mdhd.body + (mdhdV1 ? 20 : 12));
|
|
60
|
+
const handler = fourcc(dv, hdlr.body + 8); // after ver/flags(4)+pre_defined(4)
|
|
61
|
+
const kind = handler === "vide" ? "video" : handler === "soun" ? "audio" : handler;
|
|
62
|
+
const minf = find(mb, "minf"); const minfB = walk(dv, minf.body, minf.end);
|
|
63
|
+
const stbl = find(minfB, "stbl"); const sb = walk(dv, stbl.body, stbl.end);
|
|
64
|
+
const stsd = find(sb, "stsd");
|
|
65
|
+
const stsdRaw = u8.slice(stsd.start, stsd.end); // whole stsd box, copied verbatim into output
|
|
66
|
+
// codec config only (SPS/PPS or audio decoder config) — the equality signal for the match gate,
|
|
67
|
+
// excluding clip-specific sample-entry boxes (btrt etc) that differ by content.
|
|
68
|
+
const codecCfg = scanForBox(stsdRaw, kind==="video" ? "avcC" : "esds")
|
|
69
|
+
|| scanForBox(stsdRaw, "hvcC") || scanForBox(stsdRaw, "vpcC") || scanForBox(stsdRaw, "av1C");
|
|
70
|
+
// video dims from tkhd
|
|
71
|
+
let width = 0, height = 0;
|
|
72
|
+
const tkhd = find(tb, "tkhd");
|
|
73
|
+
if(tkhd && kind === "video"){ width = dv.getUint16(tkhd.end - 8) ; height = dv.getUint16(tkhd.end - 4); }
|
|
74
|
+
// audio rate/channels from the mp4a sample entry (esds carries clip-specific bitrate, so it's not
|
|
75
|
+
// a stable equality signal — rate+channels is what decides concat compatibility).
|
|
76
|
+
let channels = 0, sampleRate = 0;
|
|
77
|
+
if(kind === "audio"){ const mp4a = scanForBox(stsdRaw, "mp4a"); if(mp4a){ const adv = new DataView(mp4a.buffer, mp4a.byteOffset, mp4a.byteLength); channels = adv.getUint16(24); sampleRate = adv.getUint16(32); } }
|
|
78
|
+
|
|
79
|
+
// --- sample tables ---
|
|
80
|
+
const stts = find(sb, "stts"), stsc = find(sb, "stsc"), stsz = find(sb, "stsz");
|
|
81
|
+
const stco = find(sb, "stco"), co64 = find(sb, "co64"), ctts = find(sb, "ctts"), stss = find(sb, "stss");
|
|
82
|
+
|
|
83
|
+
// stsz
|
|
84
|
+
const stszSampleSize = dv.getUint32(stsz.body + 4);
|
|
85
|
+
const sampleCount = dv.getUint32(stsz.body + 8);
|
|
86
|
+
const sizes = new Array(sampleCount);
|
|
87
|
+
if(stszSampleSize === 0){ for(let i=0;i<sampleCount;i++) sizes[i] = dv.getUint32(stsz.body + 12 + i*4); }
|
|
88
|
+
else sizes.fill(stszSampleSize);
|
|
89
|
+
|
|
90
|
+
// stts -> per-sample duration
|
|
91
|
+
const sttsN = dv.getUint32(stts.body + 4);
|
|
92
|
+
const durs = new Array(sampleCount); let si = 0;
|
|
93
|
+
for(let e=0;e<sttsN;e++){ const cnt = dv.getUint32(stts.body + 8 + e*8); const delta = dv.getUint32(stts.body + 12 + e*8); for(let k=0;k<cnt && si<sampleCount;k++) durs[si++] = delta; }
|
|
94
|
+
while(si < sampleCount) durs[si++] = durs[si-2] || 0;
|
|
95
|
+
|
|
96
|
+
// ctts -> per-sample composition offset (may be signed in v1; treat as int32)
|
|
97
|
+
const cts = new Array(sampleCount).fill(0);
|
|
98
|
+
if(ctts){ const n = dv.getUint32(ctts.body + 4); let ci = 0; for(let e=0;e<n;e++){ const cnt = dv.getUint32(ctts.body + 8 + e*8); const off = dv.getInt32(ctts.body + 12 + e*8); for(let k=0;k<cnt && ci<sampleCount;k++) cts[ci++] = off; } }
|
|
99
|
+
|
|
100
|
+
// stss -> sync set (1-based). absent => all sync
|
|
101
|
+
let syncSet = null;
|
|
102
|
+
if(stss){ syncSet = new Set(); const n = dv.getUint32(stss.body + 4); for(let e=0;e<n;e++) syncSet.add(dv.getUint32(stss.body + 8 + e*4)); }
|
|
103
|
+
|
|
104
|
+
// chunk offsets
|
|
105
|
+
const co = stco || co64; const is64 = !!co64;
|
|
106
|
+
const coN = dv.getUint32(co.body + 4);
|
|
107
|
+
const chunkOffsets = new Array(coN);
|
|
108
|
+
for(let e=0;e<coN;e++) chunkOffsets[e] = is64 ? Number(dv.getBigUint64(co.body + 8 + e*8)) : dv.getUint32(co.body + 8 + e*4);
|
|
109
|
+
|
|
110
|
+
// stsc -> samples per chunk
|
|
111
|
+
const stscN = dv.getUint32(stsc.body + 4);
|
|
112
|
+
const stscEntries = [];
|
|
113
|
+
for(let e=0;e<stscN;e++) stscEntries.push({ first: dv.getUint32(stsc.body + 8 + e*12), spc: dv.getUint32(stsc.body + 12 + e*12) });
|
|
114
|
+
|
|
115
|
+
// compute per-sample file offset
|
|
116
|
+
const samples = [];
|
|
117
|
+
let sIdx = 0;
|
|
118
|
+
for(let c=0;c<coN;c++){
|
|
119
|
+
// samples in this chunk = spc from the applicable stsc entry
|
|
120
|
+
let spc = 1;
|
|
121
|
+
for(let e=stscEntries.length-1;e>=0;e--){ if((c+1) >= stscEntries[e].first){ spc = stscEntries[e].spc; break; } }
|
|
122
|
+
let off = chunkOffsets[c];
|
|
123
|
+
for(let k=0;k<spc && sIdx<sampleCount;k++){
|
|
124
|
+
samples.push({ offset: off, size: sizes[sIdx], dur: durs[sIdx], cts: cts[sIdx], sync: syncSet ? syncSet.has(sIdx+1) : true });
|
|
125
|
+
off += sizes[sIdx];
|
|
126
|
+
sIdx++;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if(samples.length !== sampleCount) throw new Error("sample count mismatch " + samples.length + "/" + sampleCount);
|
|
130
|
+
tracks.push({ kind, timescale, stsdRaw, codecCfg, samples, width, height, channels, sampleRate });
|
|
131
|
+
}
|
|
132
|
+
return { tracks };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// ---- box writers ----
|
|
136
|
+
const enc = (s) => Uint8Array.from(s, c => c.charCodeAt(0));
|
|
137
|
+
function u32(n){ const a = new Uint8Array(4); new DataView(a.buffer).setUint32(0, n>>>0); return a; }
|
|
138
|
+
function u16(n){ const a = new Uint8Array(2); new DataView(a.buffer).setUint16(0, n & 0xffff); return a; }
|
|
139
|
+
function concat(arrs){ let len=0; for(const a of arrs) len += a.length; const out = new Uint8Array(len); let p=0; for(const a of arrs){ out.set(a, p); p += a.length; } return out; }
|
|
140
|
+
function box(type, ...payload){ const body = concat(payload); return concat([u32(body.length + 8), enc(type), body]); }
|
|
141
|
+
function fullbox(type, version, flags, ...payload){ return box(type, Uint8Array.from([version, (flags>>16)&255, (flags>>8)&255, flags&255]), ...payload); }
|
|
142
|
+
|
|
143
|
+
function rle(values){ // -> [count,val] runs, returns entries array of Uint8Array pairs
|
|
144
|
+
const runs = []; let i=0;
|
|
145
|
+
while(i<values.length){ let j=i+1; while(j<values.length && values[j]===values[i]) j++; runs.push([j-i, values[i]]); i=j; }
|
|
146
|
+
return runs;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Concatenate. buffers: array of Uint8Array (whole mp4 files). opts.dedup drops each later clip's first video sample.
|
|
150
|
+
function concatMp4(buffers, opts){
|
|
151
|
+
const dedup = !!(opts && opts.dedup);
|
|
152
|
+
const parsed = buffers.map(parseMp4);
|
|
153
|
+
// gather track kinds present in clip0
|
|
154
|
+
const base = parsed[0];
|
|
155
|
+
const outTracks = [];
|
|
156
|
+
for(let ti=0; ti<base.tracks.length; ti++){
|
|
157
|
+
const kind = base.tracks[ti].kind;
|
|
158
|
+
if(kind !== "video" && kind !== "audio") continue;
|
|
159
|
+
const outTs = base.tracks[ti].timescale;
|
|
160
|
+
const merged = { kind, timescale: outTs, stsdRaw: base.tracks[ti].stsdRaw, width: base.tracks[ti].width, height: base.tracks[ti].height, samples: [] };
|
|
161
|
+
for(let ci=0; ci<parsed.length; ci++){
|
|
162
|
+
const t = parsed[ci].tracks.find((x,i)=> x.kind===kind && (kind!=="video" || true) );
|
|
163
|
+
if(!t){ throw new Error("clip "+ci+" missing "+kind+" track"); }
|
|
164
|
+
const scale = outTs / t.timescale;
|
|
165
|
+
let list = t.samples;
|
|
166
|
+
if(dedup && kind==="video" && ci>0) list = list.slice(1);
|
|
167
|
+
for(const s of list){
|
|
168
|
+
merged.samples.push({ bufIdx: ci, offset: s.offset, size: s.size, dur: Math.round(s.dur*scale), cts: Math.round(s.cts*scale), sync: s.sync });
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
outTracks.push(merged);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Build mdat first so chunk offsets are known. Layout: ftyp + mdat + moov.
|
|
175
|
+
const ftyp = box("ftyp", enc("isom"), u32(0x200), enc("isomiso2avc1mp41"));
|
|
176
|
+
// assemble mdat data, recording each sample's absolute offset
|
|
177
|
+
const mdatParts = [];
|
|
178
|
+
let cursor = ftyp.length + 8; // 8 = mdat header
|
|
179
|
+
for(const t of outTracks){
|
|
180
|
+
for(const s of t.samples){
|
|
181
|
+
s.newOffset = cursor;
|
|
182
|
+
const src = buffers[s.bufIdx].subarray(s.offset, s.offset + s.size);
|
|
183
|
+
mdatParts.push(src);
|
|
184
|
+
cursor += s.size;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
const mdatData = concat(mdatParts);
|
|
188
|
+
const mdat = concat([u32(mdatData.length + 8), enc("mdat"), mdatData]);
|
|
189
|
+
|
|
190
|
+
// moov
|
|
191
|
+
const mvTimescale = 1000;
|
|
192
|
+
let maxDurMs = 0;
|
|
193
|
+
const trakBoxes = [];
|
|
194
|
+
let trackId = 1;
|
|
195
|
+
for(const t of outTracks){
|
|
196
|
+
const totalTicks = t.samples.reduce((a,s)=>a+s.dur, 0);
|
|
197
|
+
const durMs = Math.round(totalTicks / t.timescale * mvTimescale);
|
|
198
|
+
if(durMs > maxDurMs) maxDurMs = durMs;
|
|
199
|
+
|
|
200
|
+
// stbl children
|
|
201
|
+
const sttsRuns = rle(t.samples.map(s=>s.dur));
|
|
202
|
+
const stts = fullbox("stts", 0, 0, u32(sttsRuns.length), ...sttsRuns.map(r=>concat([u32(r[0]), u32(r[1])])));
|
|
203
|
+
const stsz = fullbox("stsz", 0, 0, u32(0), u32(t.samples.length), ...t.samples.map(s=>u32(s.size)));
|
|
204
|
+
const stsc = fullbox("stsc", 0, 0, u32(1), concat([u32(1), u32(1), u32(1)]));
|
|
205
|
+
const stco = fullbox("stco", 0, 0, u32(t.samples.length), ...t.samples.map(s=>u32(s.newOffset)));
|
|
206
|
+
const children = [t.stsdRaw, stts];
|
|
207
|
+
if(t.kind==="video"){
|
|
208
|
+
const anyCts = t.samples.some(s=>s.cts!==0);
|
|
209
|
+
if(anyCts){ const cttsRuns = rle(t.samples.map(s=>s.cts)); children.push(fullbox("ctts", 0, 0, u32(cttsRuns.length), ...cttsRuns.map(r=>concat([u32(r[0]), u32(r[1]>>>0)])))); }
|
|
210
|
+
const syncIdx = []; t.samples.forEach((s,i)=>{ if(s.sync) syncIdx.push(i+1); });
|
|
211
|
+
if(syncIdx.length && syncIdx.length !== t.samples.length) children.push(fullbox("stss", 0, 0, u32(syncIdx.length), ...syncIdx.map(u32)));
|
|
212
|
+
}
|
|
213
|
+
children.push(stsc, stsz, stco);
|
|
214
|
+
const stbl = box("stbl", ...children);
|
|
215
|
+
|
|
216
|
+
const mediaHeader = t.kind==="video"
|
|
217
|
+
? box("vmhd", Uint8Array.from([0,0,0,1]), new Uint8Array(8))
|
|
218
|
+
: box("smhd", new Uint8Array(8));
|
|
219
|
+
const dref = fullbox("dref", 0, 0, u32(1), fullbox("url ", 0, 1));
|
|
220
|
+
const dinf = box("dinf", dref);
|
|
221
|
+
const minf = box("minf", mediaHeader, dinf, stbl);
|
|
222
|
+
|
|
223
|
+
const hdlrName = enc(t.kind==="video" ? "VideoHandler\0" : "SoundHandler\0");
|
|
224
|
+
const hdlr = fullbox("hdlr", 0, 0, u32(0), enc(t.kind==="video"?"vide":"soun"), new Uint8Array(12), hdlrName);
|
|
225
|
+
const mdhd = fullbox("mdhd", 0, 0, u32(0), u32(0), u32(t.timescale), u32(totalTicks), Uint8Array.from([0x55,0xc4,0,0]));
|
|
226
|
+
const mdia = box("mdia", mdhd, hdlr, minf);
|
|
227
|
+
|
|
228
|
+
// tkhd (enabled+in_movie flags=7)
|
|
229
|
+
const w = (t.width||0), h = (t.height||0);
|
|
230
|
+
const tkhdBody = concat([
|
|
231
|
+
u32(0), u32(0), u32(trackId), u32(0), u32(durMs),
|
|
232
|
+
new Uint8Array(8), u16(0), u16(0), u16(t.kind==="audio"?0x0100:0), u16(0),
|
|
233
|
+
// matrix
|
|
234
|
+
concat([u32(0x00010000),u32(0),u32(0),u32(0),u32(0x00010000),u32(0),u32(0),u32(0),u32(0x40000000)]),
|
|
235
|
+
u32(w<<16), u32(h<<16)
|
|
236
|
+
]);
|
|
237
|
+
const tkhd = fullbox("tkhd", 0, 7, tkhdBody);
|
|
238
|
+
trakBoxes.push(box("trak", tkhd, mdia));
|
|
239
|
+
trackId++;
|
|
240
|
+
}
|
|
241
|
+
const mvhd = fullbox("mvhd", 0, 0, u32(0), u32(0), u32(mvTimescale), u32(maxDurMs),
|
|
242
|
+
u32(0x00010000), u16(0x0100), u16(0), new Uint8Array(8),
|
|
243
|
+
concat([u32(0x00010000),u32(0),u32(0),u32(0),u32(0x00010000),u32(0),u32(0),u32(0),u32(0x40000000)]),
|
|
244
|
+
new Uint8Array(24), u32(trackId));
|
|
245
|
+
const moov = box("moov", mvhd, ...trakBoxes);
|
|
246
|
+
|
|
247
|
+
return concat([ftyp, mdat, moov]);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// quick sniff
|
|
251
|
+
function isMp4(u8){ if(u8.length<12) return false; const dv=new DataView(u8.buffer,u8.byteOffset,u8.byteLength); return fourcc(dv,4)==="ftyp"; }
|
|
252
|
+
|
|
253
|
+
function bytesEqual(a, b){ if(!a || !b || a.length !== b.length) return false; for(let i=0;i<a.length;i++) if(a[i]!==b[i]) return false; return true; }
|
|
254
|
+
|
|
255
|
+
// Strict gate: every clip must have the same track shape and byte-identical codec config
|
|
256
|
+
// (avcC / esds live in stsd). A false positive silently corrupts output, so default to NO on any doubt.
|
|
257
|
+
function mp4ParamsMatch(bufs){
|
|
258
|
+
try{
|
|
259
|
+
if(bufs.length < 2) return false;
|
|
260
|
+
const ps = bufs.map(parseMp4);
|
|
261
|
+
const sig = (p)=>{
|
|
262
|
+
const vids = p.tracks.filter(t=>t.kind==="video");
|
|
263
|
+
const auds = p.tracks.filter(t=>t.kind==="audio");
|
|
264
|
+
if(vids.length !== 1) return null; // need exactly one video track
|
|
265
|
+
if(auds.length > 1) return null;
|
|
266
|
+
return { v: vids[0], a: auds[0] || null, na: auds.length };
|
|
267
|
+
};
|
|
268
|
+
const base = sig(ps[0]); if(!base) return false;
|
|
269
|
+
for(let i=1;i<ps.length;i++){
|
|
270
|
+
const s = sig(ps[i]); if(!s) return false;
|
|
271
|
+
if(s.na !== base.na) return false; // all-or-none audio
|
|
272
|
+
if(s.v.width !== base.v.width || s.v.height !== base.v.height) return false;
|
|
273
|
+
if(!bytesEqual(s.v.codecCfg, base.v.codecCfg)) return false; // same video SPS/PPS (avcC)
|
|
274
|
+
if(base.a && (s.a.sampleRate !== base.a.sampleRate || s.a.channels !== base.a.channels)) return false; // audio concat-compatible
|
|
275
|
+
}
|
|
276
|
+
return true;
|
|
277
|
+
}catch(e){ return false; }
|
|
278
|
+
}
|
|
279
|
+
return { concatMp4, isMp4, mp4ParamsMatch, parseMp4 };
|
|
280
|
+
})();
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
export { MP4CAT };
|
|
284
|
+
export default MP4CAT;
|