Skip to content

🎥 Make videos programmatically with React

stts box

Jonny Burgerf3bc665f8ebf
diff --git a/packages/video-parser/src/boxes/iso-base-media/process-box.ts b/packages/video-parser/src/boxes/iso-base-media/process-box.ts
index 0a8f5a06fb..50bbb4deb1 100644
--- a/packages/video-parser/src/boxes/iso-base-media/process-box.ts
+++ b/packages/video-parser/src/boxes/iso-base-media/process-box.ts
@@ -6,6 +6,7 @@ import {parseMoov} from './moov/moov';
import {parseMvhd} from './mvhd';
import {parseMebx} from './stsd/mebx';
import {parseStsd} from './stsd/stsd';
+import {parseStts} from './stts/stts';
import {parseTkhd} from './tkhd';
import {parseTrak} from './trak/trak';
@@ -114,7 +115,7 @@ const processBox = ({
}
if (boxType === 'mebx') {
- const box = parseMebx({iterator, offset: 0, size: boxSize});
+ const box = parseMebx({iterator, offset: fileOffset, size: boxSize});
return {
type: 'complete',
@@ -147,6 +148,20 @@ const processBox = ({
};
}
+ if (boxType === 'stts') {
+ const box = parseStts({
+ data: iterator,
+ size: boxSize,
+ fileOffset,
+ });
+
+ return {
+ type: 'complete',
+ box,
+ size: boxSize,
+ };
+ }
+
const bytesRemainingInBox =
boxSize - (iterator.counter.getOffset() - fileOffset);
diff --git a/packages/video-parser/src/boxes/iso-base-media/stts/stts.ts b/packages/video-parser/src/boxes/iso-base-media/stts/stts.ts
new file mode 100644
index 0000000000..a44021c6d1
--- /dev/null
+++ b/packages/video-parser/src/boxes/iso-base-media/stts/stts.ts
@@ -0,0 +1,62 @@
+import type {BufferIterator} from '../../../buffer-iterator';
+
+export interface SttsBox {
+ type: 'stts-box';
+ sampleDistribution: SampleDistribution[];
+}
+
+type SampleDistribution = {
+ sampleCount: number;
+ sampleDelta: number;
+};
+
+export const parseStts = ({
+ data,
+ size,
+ fileOffset,
+}: {
+ data: BufferIterator;
+ size: number;
+ fileOffset: number;
+}): SttsBox => {
+ const initialOffset = data.counter.getOffset();
+ const initialCounter = initialOffset - fileOffset;
+
+ const version = data.getUint8();
+ if (version !== 0) {
+ throw new Error(`Unsupported STTS version ${version}`);
+ }
+
+ // flags, we discard them
+ data.discard(3);
+
+ // entry count
+ const entryCount = data.getUint32();
+
+ const sampleDistributions: SampleDistribution[] = [];
+
+ // entries
+ for (let i = 0; i < entryCount; i++) {
+ const sampleCount = data.getUint32();
+ const sampleDelta = data.getUint32();
+
+ const sampleDistribution: SampleDistribution = {
+ sampleCount,
+ sampleDelta,
+ };
+ sampleDistributions.push(sampleDistribution);
+ }
+
+ const bytesUsed = data.counter.getOffset() - initialOffset + initialCounter;
+
+ if (bytesUsed !== size) {
+ throw new Error(
+ `Expected stts box to be ${size} bytes, but was ${bytesUsed} bytes`,
+ );
+ }
+
+ return {
+ type: 'stts-box',
+ sampleDistribution: sampleDistributions,
+ };
+};
diff --git a/packages/video-parser/src/parse-result.ts b/packages/video-parser/src/parse-result.ts
index fce6867613..fd73e12171 100644
--- a/packages/video-parser/src/parse-result.ts
+++ b/packages/video-parser/src/parse-result.ts
@@ -5,6 +5,7 @@ import type {MvhdBox} from './boxes/iso-base-media/mvhd';
import type {KeysBox} from './boxes/iso-base-media/stsd/keys';
import type {MebxBox} from './boxes/iso-base-media/stsd/mebx';
import type {StsdBox} from './boxes/iso-base-media/stsd/stsd';
+import type {SttsBox} from './boxes/iso-base-media/stts/stts';
import type {TkhdBox} from './boxes/iso-base-media/tkhd';
import type {TrakBox} from './boxes/iso-base-media/trak/trak';
import type {MatroskaSegment} from './boxes/webm/segments';
@@ -26,7 +27,8 @@ export type IsoBaseMediaBox =
| MebxBox
| KeysBox
| MoovBox
- | TrakBox;
+ | TrakBox
+ | SttsBox;
export type AnySegment = MatroskaSegment | IsoBaseMediaBox;
diff --git a/packages/video-parser/src/test/parse-stts.test.ts b/packages/video-parser/src/test/parse-stts.test.ts
new file mode 100644
index 0000000000..7ef6472763
--- /dev/null
+++ b/packages/video-parser/src/test/parse-stts.test.ts
@@ -0,0 +1,38 @@
+import {expect, test} from 'bun:test';
+import {parseStts} from '../boxes/iso-base-media/stts/stts';
+import {getArrayBufferIterator} from '../buffer-iterator';
+
+const buffer = new Uint8Array([
+ // mock header
+ 0, 0, 0, 0, 0, 0, 0, 0,
+ // actual box
+ 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 1, 13, 0, 0, 0, 20, 0, 0, 0, 1, 0, 0, 0, 21, 0,
+ 0, 0, 107, 0, 0, 0, 20, 0, 0, 11, 216, 99, 116, 116, 115,
+]);
+
+test('Should parse stts box', () => {
+ const iterator = getArrayBufferIterator(buffer);
+ iterator.counter.increment(8);
+ const result = parseStts({
+ data: iterator,
+ size: buffer.length - 8,
+ fileOffset: 0,
+ });
+ expect(result).toEqual({
+ type: 'stts-box',
+ sampleDistribution: [
+ {
+ sampleCount: 269,
+ sampleDelta: 20,
+ },
+ {
+ sampleCount: 1,
+ sampleDelta: 21,
+ },
+ {
+ sampleCount: 107,
+ sampleDelta: 20,
+ },
+ ],
+ });
+});