Skip to content

🎥 Make videos programmatically with React

uint 16

Jonny Burger12da058ca2f8
diff --git a/packages/video-parser/src/boxes/iso-base-media/tkhd.ts b/packages/video-parser/src/boxes/iso-base-media/tkhd.ts
index d9e08a2b54..ea31e9827d 100644
--- a/packages/video-parser/src/boxes/iso-base-media/tkhd.ts
+++ b/packages/video-parser/src/boxes/iso-base-media/tkhd.ts
@@ -1,3 +1,4 @@
+import {getArrayBufferIterator} from '../../read-and-increment-offset';
import type {BaseBox} from './base-type';
import type {ThreeDMatrix} from './mvhd';
import {toUnixTimestamp} from './to-date';
@@ -18,86 +19,68 @@ export interface TkhdBox extends BaseBox {
}
export const parseTkhd = (data: ArrayBuffer, fileOffset: number): TkhdBox => {
- let chunkOffset = 0;
+ const iterator = getArrayBufferIterator(data, 0);
- const view = new DataView(data);
+ const size = iterator.getUint32();
- const size = view.getUint32(chunkOffset);
- chunkOffset += 4;
- const atom = data.slice(chunkOffset, chunkOffset + 4);
- const atomAsString = new TextDecoder().decode(atom);
- if (atomAsString !== 'tkhd') {
- throw new Error(`Expected tkhd atom, got ${atomAsString}`);
+ const atom = iterator.getAtom();
+ if (atom !== 'tkhd') {
+ throw new Error(`Expected tkhd atom, got ${atom}`);
}
- chunkOffset += 4;
- const version = view.getUint8(chunkOffset);
- if (version !== 0) {
- throw new Error(`Unsupported TKHD version ${version}`);
- }
-
- chunkOffset += 1;
-
if (size !== 92) {
throw new Error(`Expected tkhd size of version 0 to be 92, got ${size}`);
}
+ const version = iterator.getUint8();
+ if (version !== 0) {
+ throw new Error(`Unsupported TKHD version ${version}`);
+ }
+
// Flags, we discard them
- data.slice(chunkOffset, chunkOffset + 3);
- chunkOffset += 3;
+ iterator.discard(3);
- const creationTime = view.getUint32(chunkOffset);
- chunkOffset += 4;
+ const creationTime = iterator.getUint32();
- const modificationTime = view.getUint32(chunkOffset);
- chunkOffset += 4;
+ const modificationTime = iterator.getUint32();
- const trackId = view.getUint32(chunkOffset);
- chunkOffset += 4;
+ const trackId = iterator.getUint32();
// reserved
- chunkOffset += 4;
+ iterator.discard(4);
- const duration = view.getUint32(chunkOffset);
- chunkOffset += 4;
+ const duration = iterator.getUint32();
// reserved 2
- chunkOffset += 4;
+ iterator.discard(4);
// reserved 3
- chunkOffset += 4;
+ iterator.discard(4);
- const layer = view.getUint16(chunkOffset);
- chunkOffset += 2;
+ const layer = iterator.getUint16();
- const alternateGroup = view.getUint16(chunkOffset);
- chunkOffset += 2;
+ const alternateGroup = iterator.getUint16();
- const volume = view.getUint16(chunkOffset);
- chunkOffset += 2;
+ const volume = iterator.getUint16();
// reserved 4
- chunkOffset += 2;
+ iterator.discard(2);
const matrix = [
- view.getUint32(chunkOffset),
- view.getUint32(chunkOffset + 4),
- view.getUint32(chunkOffset + 8),
- view.getUint32(chunkOffset + 12),
- view.getUint32(chunkOffset + 16),
- view.getUint32(chunkOffset + 20),
- view.getUint32(chunkOffset + 24),
- view.getUint32(chunkOffset + 28),
- view.getUint32(chunkOffset + 32),
+ iterator.getUint32(),
+ iterator.getUint32(),
+ iterator.getUint32(),
+ iterator.getUint32(),
+ iterator.getUint32(),
+ iterator.getUint32(),
+ iterator.getUint32(),
+ iterator.getUint32(),
+ iterator.getUint32(),
];
- chunkOffset += 36;
-
- const width = view.getUint32(chunkOffset);
- chunkOffset += 4;
+ const width = iterator.getUint32();
- const height = view.getUint32(chunkOffset);
- chunkOffset += 4;
+ const height = iterator.getUint32();
return {
offset: fileOffset,
diff --git a/packages/video-parser/src/read-and-increment-offset.ts b/packages/video-parser/src/read-and-increment-offset.ts
index aa675a205c..e0460d4df8 100644
--- a/packages/video-parser/src/read-and-increment-offset.ts
+++ b/packages/video-parser/src/read-and-increment-offset.ts
@@ -63,6 +63,11 @@ export const getArrayBufferIterator = (
counter.increment(1);
return val;
},
+ getUint16: () => {
+ const val = view.getUint16(counter.getOffset());
+ counter.increment(2);
+ return val;
+ },
getUint32: () => {
const val = view.getUint32(counter.getOffset());
counter.increment(4);