@svta/cml-iso-bmff 0.23.2 → 1.0.0-alpha.1

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 CHANGED
@@ -10,6 +10,182 @@ npm i @svta/cml-iso-bmff
10
10
 
11
11
  ## Usage
12
12
 
13
+ ### Reading boxes
14
+
15
+ Boxes can be read from a buffer using the `readIsoBoxes` function which returns an array of parsed boxes. By default the function will only read the box header. To parse the box content, a reader function must be provided for that box type.
16
+
17
+ > [!NOTE]
18
+ > Container boxes are automatically parsed and do not need a reader function. All known container types are listed in the `CONTAINERS` constant.
19
+
20
+ ```typescript
21
+ import { readFtyp, readIsoBoxes } from "@svta/cml-iso-bmff";
22
+
23
+ const buffer = await fetch("https://example.com/bbb.mp4").then((r) =>
24
+ r.arrayBuffer()
25
+ );
26
+
27
+ const boxes = readIsoBoxes(buffer, {
28
+ readers: {
29
+ ftyp: readFtyp,
30
+ },
31
+ });
32
+ ```
33
+
34
+ ### Writing boxes
35
+
36
+ Boxes can be written to using the `writeIsoBoxes` function. The function takes an array of streamable items and returns a readable stream. Streamable items include:
37
+
38
+ - Raw box objects (e.g. `{ type: 'ftyp', majorBrand: 'isom', minorVersion: 1, compatibleBrands: ['isom'] }`)
39
+ - ArrayBufferViews like `Uint8Array` and `DataView` as well as the box strutures returned by the `readIsoBoxes` function (they adhere to the `ArrayBufferView` interface).
40
+
41
+ When providing raw box objects, a configuration object must be provided to specify the writers for those box types.
42
+
43
+ ```typescript
44
+ import { writeFtyp, writeIsoBoxes, writeMdat } from "@svta/cml-iso-bmff";
45
+
46
+ const boxes = [
47
+ {
48
+ type: "ftyp",
49
+ majorBrand: "isom",
50
+ minorVersion: 1,
51
+ compatibleBrands: ["isom"],
52
+ },
53
+ new Uint8Array([
54
+ 0x00, 0x00, 0x00, 0x14, 0x66, 0x74, 0x79, 0x70, 0x69, 0x73, 0x6f, 0x6d,
55
+ 0x00, 0x00, 0x00, 0x01, 0x69, 0x73, 0x6f, 0x6d,
56
+ ]),
57
+ {
58
+ type: "mdat",
59
+ data: new Uint8Array([
60
+ 102, 105, 114, 115, 116, 115, 101, 99, 111, 110, 100, 116, 104, 105, 114,
61
+ 100,
62
+ ]),
63
+ },
64
+ ];
65
+
66
+ const byteArrays = writeIsoBoxes(boxes, {
67
+ writers: {
68
+ ftyp: writeFtyp,
69
+ mdat: writeMdat,
70
+ },
71
+ });
72
+
73
+ for (const byteArray of byteArrays) {
74
+ sourceBuffer.appendBuffer(byteArray.buffer);
75
+ // wait for updateend event
76
+ }
77
+ ```
78
+
79
+ Boxes can also be written to a readable stream using the `createIsoBoxReadableStream` function.
80
+
81
+ ```typescript
82
+ import {
83
+ createIsoBoxReadableStreams,
84
+ writeFtyp,
85
+ writeMdat,
86
+ } from "@svta/cml-iso-bmff";
87
+
88
+ const boxes = [
89
+ {
90
+ type: "ftyp",
91
+ majorBrand: "isom",
92
+ minorVersion: 1,
93
+ compatibleBrands: ["isom"],
94
+ },
95
+ new Uint8Array([
96
+ 0x00, 0x00, 0x00, 0x14, 0x66, 0x74, 0x79, 0x70, 0x69, 0x73, 0x6f, 0x6d,
97
+ 0x00, 0x00, 0x00, 0x01, 0x69, 0x73, 0x6f, 0x6d,
98
+ ]),
99
+ {
100
+ type: "mdat",
101
+ data: new Uint8Array([
102
+ 102, 105, 114, 115, 116, 115, 101, 99, 111, 110, 100, 116, 104, 105, 114,
103
+ 100,
104
+ ]),
105
+ },
106
+ ];
107
+
108
+ const readableStream = createIsoBoxReadableStreams(boxes, {
109
+ writers: {
110
+ ftyp: writeFtyp,
111
+ mdat: writeMdat,
112
+ },
113
+ });
114
+
115
+ const stream = await webTransport.createBidirectionalStream();
116
+ readableStream.pipeTo(transport.writable);
117
+ ```
118
+
119
+ #### Type safety
120
+
121
+ When working with raw box objects in TypeScript, cast the objects to a box type to ensure type safety. This can be done in two ways. Either by using a box type from the libray:
122
+
123
+ ```typescript
124
+ import type { FileTypeBox } from "@svta/cml-iso-bmff";
125
+
126
+ const box: FileTypeBox = {
127
+ type: "ftyp",
128
+ majorBrand: "isom",
129
+ minorVersion: 1,
130
+ compatibleBrands: ["isom"],
131
+ };
132
+ ```
133
+
134
+ Or by using the `as const` operator on the `type` property:
135
+
136
+ ```typescript
137
+ const box = {
138
+ type: "ftyp" as const,
139
+ majorBrand: "isom",
140
+ minorVersion: 1,
141
+ compatibleBrands: ["isom"],
142
+ };
143
+ >
144
+ ```
145
+
146
+ Box types can also be accessed from `IsoBoxMap` using the box's FourCC:
147
+
148
+ ```typescript
149
+ import type { IsoBoxMap } from "@svta/cml-iso-bmff";
150
+
151
+ const box: IsoBoxMap["ftyp"] = {
152
+ type: "ftyp",
153
+ majorBrand: "isom",
154
+ minorVersion: 1,
155
+ compatibleBrands: ["isom"],
156
+ };
157
+ ```
158
+
159
+ ### Utilities
160
+
161
+ #### Traversing boxes
162
+
163
+ The `traverseIsoBoxes` function can be used to traverse box iterators. It returns a generator of boxes. The function takes an iterable of boxes, and optional arguments for depth-first traversal and maximum depth. By default the function will traverse the boxes depth-first with a maximum depth of infinity. A value of 0 for the maximum depth will only traverse the root boxes.
164
+
165
+ ```typescript
166
+ import { traverseIsoBoxes } from "@svta/cml-iso-bmff";
167
+
168
+ const boxes = traverseIsoBoxes(buffer);
169
+ ```
170
+
171
+ #### Type guards
172
+
173
+ The library provides type guards for checking if a box is a container or full box. These can be used to narrow the type of a box in TypeScript.
174
+
175
+ The `isContainer` function can be used to check if a box is a container box.
176
+
177
+ ```typescript
178
+ import { isContainer } from "@svta/cml-iso-bmff";
179
+
180
+ const box = { type: "meta", boxes: [] };
181
+ const isContainer = isContainer(box);
182
+ ```
183
+
184
+ The `isFullBox` function can be used to check if a box is a full box.
185
+
13
186
  ```typescript
187
+ import { isFullBox } from "@svta/cml-iso-bmff";
14
188
 
189
+ const box = { type: "meta", version: 1, flags: 0 };
190
+ const isFullBox = isFullBox(box);
15
191
  ```