Skip to content

🎥 Make videos programmatically with React

pasp and avcc boxes

Jonny Burgereedbba315c03
diff --git a/packages/media-parser/src/create/iso-base-media/create-avcc.ts b/packages/media-parser/src/create/iso-base-media/create-avcc.ts
new file mode 100644
index 0000000000..31a1bf5b71
--- /dev/null
+++ b/packages/media-parser/src/create/iso-base-media/create-avcc.ts
@@ -0,0 +1,12 @@
+import {combineUint8Arrays} from '../../boxes/webm/make-header';
+import {addSize, stringsToUint8Array} from './primitives';
+
+export const createAvccBox = (privateData: Uint8Array) => {
+ return addSize(
+ combineUint8Arrays([
+ // type
+ stringsToUint8Array('avcC'),
+ privateData,
+ ]),
+ );
+};
diff --git a/packages/media-parser/src/create/iso-base-media/create-colr.ts b/packages/media-parser/src/create/iso-base-media/create-colr.ts
new file mode 100644
index 0000000000..b4efe8449d
--- /dev/null
+++ b/packages/media-parser/src/create/iso-base-media/create-colr.ts
@@ -0,0 +1,34 @@
+import {combineUint8Arrays} from '../../boxes/webm/make-header';
+import {addSize, stringsToUint8Array} from './primitives';
+
+export const createColr = ({
+ fullRange,
+ matrixIndex,
+ primaries,
+ transferFunction,
+}: {
+ fullRange: boolean;
+ matrixIndex: number;
+ primaries: number;
+ transferFunction: number;
+}) => {
+ return addSize(
+ combineUint8Arrays([
+ // type
+ stringsToUint8Array('colr'),
+ // colour_type
+ stringsToUint8Array('nclx'),
+ // primaries
+ // 1 = 'ITU-R BT.7090
+ new Uint8Array([0, primaries]),
+ // transfer_function
+ // 1 = 'ITU-R BT.7090
+ new Uint8Array([0, transferFunction]),
+ // matrix_index
+ // 1 = 'ITU-R BT.7090
+ new Uint8Array([0, matrixIndex]),
+ // full_range_flag
+ new Uint8Array([fullRange ? 1 : 0]),
+ ]),
+ );
+};
diff --git a/packages/media-parser/src/create/iso-base-media/create-pasp.ts b/packages/media-parser/src/create/iso-base-media/create-pasp.ts
new file mode 100644
index 0000000000..270a5c3595
--- /dev/null
+++ b/packages/media-parser/src/create/iso-base-media/create-pasp.ts
@@ -0,0 +1,16 @@
+import {combineUint8Arrays} from '../../boxes/webm/make-header';
+import {
+ addSize,
+ numberTo32BitUIntOrInt,
+ stringsToUint8Array,
+} from './primitives';
+
+export const createPasp = (x: number, y: number) => {
+ return addSize(
+ combineUint8Arrays([
+ stringsToUint8Array('pasp'),
+ numberTo32BitUIntOrInt(x),
+ numberTo32BitUIntOrInt(y),
+ ]),
+ );
+};
diff --git a/packages/media-parser/src/test/avcc.test.ts b/packages/media-parser/src/test/avcc.test.ts
new file mode 100644
index 0000000000..cbdaf8a51b
--- /dev/null
+++ b/packages/media-parser/src/test/avcc.test.ts
@@ -0,0 +1,20 @@
+import {expect, test} from 'bun:test';
+import {createAvccBox} from '../create/iso-base-media/create-avcc';
+
+test('avcc box', () => {
+ expect(
+ createAvccBox(
+ new Uint8Array([
+ 1, 100, 0, 32, 255, 225, 0, 27, 103, 100, 0, 32, 172, 217, 64, 68, 2,
+ 39, 150, 92, 4, 64, 0, 0, 3, 0, 64, 0, 0, 12, 3, 198, 12, 101, 128, 1,
+ 0, 6, 104, 235, 224, 140, 178, 44, 253, 248, 248, 0,
+ ]),
+ ),
+ ).toEqual(
+ new Uint8Array([
+ 0, 0, 0, 56, 97, 118, 99, 67, 1, 100, 0, 32, 255, 225, 0, 27, 103, 100, 0,
+ 32, 172, 217, 64, 68, 2, 39, 150, 92, 4, 64, 0, 0, 3, 0, 64, 0, 0, 12, 3,
+ 198, 12, 101, 128, 1, 0, 6, 104, 235, 224, 140, 178, 44, 253, 248, 248, 0,
+ ]),
+ );
+});
diff --git a/packages/media-parser/src/test/colr.test.ts b/packages/media-parser/src/test/colr.test.ts
new file mode 100644
index 0000000000..bb80a016b2
--- /dev/null
+++ b/packages/media-parser/src/test/colr.test.ts
@@ -0,0 +1,18 @@
+import {expect, test} from 'bun:test';
+import {createColr} from '../create/iso-base-media/create-colr';
+
+const reference = new Uint8Array([
+ 0x00, 0x00, 0x00, 0x13, 0x63, 0x6f, 0x6c, 0x72, 0x6e, 0x63, 0x6c, 0x78, 0x00,
+ 0x01, 0x00, 0x01, 0x00, 0x01, 0x0,
+]);
+
+test('colr', () => {
+ expect(
+ createColr({
+ fullRange: false,
+ matrixIndex: 1,
+ primaries: 1,
+ transferFunction: 1,
+ }),
+ ).toEqual(reference);
+});
diff --git a/packages/media-parser/src/test/pasp.test.ts b/packages/media-parser/src/test/pasp.test.ts
new file mode 100644
index 0000000000..2f6d8e2e7d
--- /dev/null
+++ b/packages/media-parser/src/test/pasp.test.ts
@@ -0,0 +1,12 @@
+import {expect, test} from 'bun:test';
+import {createPasp} from '../create/iso-base-media/create-pasp';
+
+const reference = new Uint8Array([
+ 0x00, 0x00, 0x00, 0x10, 0x70, 0x61, 0x73, 0x70, 0x00, 0x00, 0x00, 0x01, 0x00,
+ 0x00, 0x00, 0x01,
+]);
+
+test('pasp box', () => {
+ const pasp = createPasp(1, 1);
+ expect(pasp).toEqual(reference);
+});
diff --git a/packages/media-parser/src/test/stream-samples.test.ts b/packages/media-parser/src/test/stream-samples.test.ts
index 5172a071b0..e022095442 100644
--- a/packages/media-parser/src/test/stream-samples.test.ts
+++ b/packages/media-parser/src/test/stream-samples.test.ts
@@ -87,7 +87,7 @@ test('Stream samples', async () => {
dts: 1152,
duration: 1152,
cts: 1152,
- chunk: 0,
+ chunk: 1,
},
{
offset: 9526,
@@ -96,7 +96,7 @@ test('Stream samples', async () => {
dts: 2304,
duration: 1152,
cts: 2304,
- chunk: 0,
+ chunk: 1,
},
{
offset: 11081,
@@ -105,7 +105,7 @@ test('Stream samples', async () => {
dts: 3456,
duration: 1152,
cts: 3456,
- chunk: 0,
+ chunk: 2,
},
{
offset: 12589,
@@ -114,7 +114,7 @@ test('Stream samples', async () => {
dts: 4608,
duration: 1152,
cts: 4608,
- chunk: 0,
+ chunk: 3,
},
{
offset: 13549,
@@ -123,7 +123,7 @@ test('Stream samples', async () => {
dts: 5760,
duration: 1152,
cts: 5760,
- chunk: 0,
+ chunk: 3,
},
{
offset: 15120,
@@ -132,7 +132,7 @@ test('Stream samples', async () => {
dts: 6912,
duration: 1152,
cts: 6912,
- chunk: 0,
+ chunk: 4,
},
{
offset: 16781,
@@ -141,7 +141,7 @@ test('Stream samples', async () => {
dts: 8064,
duration: 1152,
cts: 8064,
- chunk: 0,
+ chunk: 5,
},
{
offset: 18276,
@@ -150,7 +150,7 @@ test('Stream samples', async () => {
dts: 9216,
duration: 1152,
cts: 9216,
- chunk: 0,
+ chunk: 6,
},
{
offset: 19236,
@@ -159,7 +159,7 @@ test('Stream samples', async () => {
dts: 10368,
duration: 1152,
cts: 10368,
- chunk: 0,
+ chunk: 6,
},
{
offset: 20972,
@@ -168,7 +168,7 @@ test('Stream samples', async () => {
dts: 11520,
duration: 1152,
cts: 11520,
- chunk: 0,
+ chunk: 7,
},
{
offset: 22518,
@@ -177,7 +177,7 @@ test('Stream samples', async () => {
dts: 12672,
duration: 1152,
cts: 12672,
- chunk: 0,
+ chunk: 8,
},
{
offset: 23478,
@@ -186,7 +186,7 @@ test('Stream samples', async () => {
dts: 13824,
duration: 1152,
cts: 13824,
- chunk: 0,
+ chunk: 8,
},
{
offset: 24438,
@@ -195,7 +195,7 @@ test('Stream samples', async () => {
dts: 14976,
duration: 1152,
cts: 14976,
- chunk: 0,
+ chunk: 8,
},
{
offset: 25398,
@@ -204,7 +204,7 @@ test('Stream samples', async () => {
dts: 16128,
duration: 1152,
cts: 16128,
- chunk: 0,
+ chunk: 8,
},
]);
});
diff --git a/packages/media-parser/src/test/stsd.test.ts b/packages/media-parser/src/test/stsd.test.ts
index 6016046f85..dee03abcb1 100644
--- a/packages/media-parser/src/test/stsd.test.ts
+++ b/packages/media-parser/src/test/stsd.test.ts
@@ -205,6 +205,7 @@ test('Should be able to parse a STSD video box correctly', async () => {
0, 0, 0, 56, 97, 118, 99, 67, 1, 100, 0, 32, 255, 225, 0, 27, 103, 100, 0,
32, 172, 217, 64, 68, 2, 39, 150, 92, 4, 64, 0, 0, 3, 0, 64, 0, 0, 12, 3,
198, 12, 101, 128, 1, 0, 6, 104, 235, 224, 140, 178, 44, 253, 248, 248, 0,
+ // pasp
0, 0, 0, 16, 112, 97, 115, 112, 0, 0, 0, 1, 0, 0, 0, 1,
]);