Skip to content

🎥 Make videos programmatically with React

stsz atom

Jonny Burger5f3b2b406eed
diff --git a/packages/media-parser/src/create/iso-base-media/create-stsz.ts b/packages/media-parser/src/create/iso-base-media/create-stsz.ts
new file mode 100644
index 0000000000..839cf26e38
--- /dev/null
+++ b/packages/media-parser/src/create/iso-base-media/create-stsz.ts
@@ -0,0 +1,32 @@
+import {combineUint8Arrays} from '../../boxes/webm/make-header';
+import type {SamplePosition} from '../../get-sample-positions';
+import {
+ addSize,
+ numberTo32BitUIntOrInt,
+ stringsToUint8Array,
+} from './primitives';
+
+export const createStsz = (samplePositions: SamplePosition[]) => {
+ const sampleSizes = samplePositions.map(
+ (samplePosition) => samplePosition.size,
+ );
+
+ return addSize(
+ combineUint8Arrays([
+ // type
+ stringsToUint8Array('stsz'),
+ // version
+ new Uint8Array([0]),
+ // flags
+ new Uint8Array([0, 0, 0]),
+ // sample size
+ // Possible optimization for the future: If all sizes are the same, we don't have to list them all out
+ // https://developer.apple.com/documentation/quicktime-file-format/sample_size_atom
+ numberTo32BitUIntOrInt(0),
+ // number of entries
+ numberTo32BitUIntOrInt(sampleSizes.length),
+ // sample sizes
+ ...sampleSizes.map((size) => numberTo32BitUIntOrInt(size)),
+ ]),
+ );
+};
diff --git a/packages/media-parser/src/test/stsz.test.ts b/packages/media-parser/src/test/stsz.test.ts
new file mode 100644
index 0000000000..e89c4a9e2b
--- /dev/null
+++ b/packages/media-parser/src/test/stsz.test.ts
@@ -0,0 +1,43 @@
+import {expect, test} from 'bun:test';
+import {createStsz} from '../create/iso-base-media/create-stsz';
+import {exampleSamplePositions} from '../create/iso-base-media/example-stts';
+
+const refernce = new Uint8Array([
+ 0, 0, 3, 12, 115, 116, 115, 122, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 190, 0, 0,
+ 3, 78, 0, 0, 3, 78, 0, 0, 3, 79, 0, 0, 3, 78, 0, 0, 3, 78, 0, 0, 3, 79, 0, 0,
+ 3, 78, 0, 0, 3, 78, 0, 0, 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 3, 78, 0, 0, 3, 78, 0, 0,
+ 3, 79, 0, 0, 3, 78, 0, 0, 3, 78, 0, 0, 3, 79, 0, 0, 3, 78, 0, 0, 3, 78, 0, 0,
+ 3, 79, 0, 0, 3, 78, 0, 0, 3, 78, 0, 0, 3, 79, 0, 0, 3, 78, 0, 0, 3, 78, 0, 0,
+ 3, 79, 0, 0, 3, 78, 0, 0, 3, 78, 0, 0, 3, 79,
+]);
+
+test('stsz', () => {
+ expect(createStsz(exampleSamplePositions)).toEqual(refernce);
+});