Skip to content

🎥 Make videos programmatically with React

get duration

Jonny Burger1370badf495c
diff --git a/packages/video-parser/src/get-dimensions.ts b/packages/video-parser/src/get-dimensions.ts
new file mode 100644
index 0000000000..f3d9c90d21
--- /dev/null
+++ b/packages/video-parser/src/get-dimensions.ts
@@ -0,0 +1,80 @@
+import type {Box} from './parse-video';
+
+export const getDimensions = (boxes: Box[]): number[] => {
+ const moovBox = boxes.find((b) => b.type === 'moov-box');
+ if (!moovBox || moovBox.type !== 'moov-box') {
+ throw new Error('Expected moov box');
+ }
+
+ const {children} = moovBox;
+ if (!children) {
+ throw new Error('Expected moov box children');
+ }
+
+ const trakBoxes = children.filter((b) => b.type === 'trak-box');
+ const tracks = trakBoxes
+ .map((t) => {
+ if (t.type !== 'trak-box') {
+ throw new Error('Expected trak box');
+ }
+
+ const mdiaBox = t.children.find(
+ (c) => c.type === 'regular-box' && c.boxType === 'mdia',
+ );
+ if (!mdiaBox) {
+ throw new Error('Expected mdia box');
+ }
+
+ if (mdiaBox.type !== 'regular-box') {
+ throw new Error('Expected mdia box');
+ }
+
+ const minfBox = mdiaBox.children.find(
+ (c) => c.type === 'regular-box' && c.boxType === 'minf',
+ );
+ if (!minfBox) {
+ throw new Error('Expected minf box');
+ }
+
+ if (minfBox.type !== 'regular-box') {
+ throw new Error('Expected minf box');
+ }
+
+ const stblBox = minfBox.children.find(
+ (c) => c.type === 'regular-box' && c.boxType === 'stbl',
+ );
+
+ if (!stblBox) {
+ throw new Error('Expected stbl box');
+ }
+
+ if (stblBox.type !== 'regular-box') {
+ throw new Error('Expected stbl box');
+ }
+
+ const stsdBox = stblBox.children.find((c) => c.type === 'stsd-box');
+
+ if (!stsdBox) {
+ throw new Error('Expected stsd box');
+ }
+
+ if (stsdBox.type !== 'stsd-box') {
+ throw new Error('Expected stsd box');
+ }
+
+ const videoSamples = stsdBox.samples.filter((s) => s.type === 'video');
+ return videoSamples[0];
+ })
+ .filter(Boolean);
+
+ if (tracks.length === 0) {
+ throw new Error('Has no video stream');
+ }
+
+ const [firstTrack] = tracks;
+ if (firstTrack.type !== 'video') {
+ throw new Error('Expected video track');
+ }
+
+ return [firstTrack.width, firstTrack.height];
+};
diff --git a/packages/video-parser/src/test/duration.test.ts b/packages/video-parser/src/test/duration.test.ts
index 2908db8333..a6ceaa7eb2 100644
--- a/packages/video-parser/src/test/duration.test.ts
+++ b/packages/video-parser/src/test/duration.test.ts
@@ -1,4 +1,5 @@
import {expect, test} from 'bun:test';
+import {getDimensions} from '../get-dimensions';
import {getDuration} from '../get-duration';
import {parseVideo} from '../parse-video';
import {exampleVideos} from './example-videos';
@@ -11,6 +12,9 @@ test('Should get duration of video', async () => {
const duration = getDuration(parsed);
expect(duration).toBe(4.167);
+
+ const dimensions = getDimensions(parsed);
+ expect(dimensions).toEqual([1080, 1080]);
});
test('Should get duration of HEVC video', async () => {
@@ -24,31 +28,9 @@ test('Should get duration of HEVC video', async () => {
throw new Error('Expected regular box');
}
- const trak = moovBox.children[1];
- if (trak.type !== 'trak-box') {
- throw new Error('Expected trak box');
- }
-
- const mdiaBox = trak.children[3];
- if (mdiaBox.type !== 'regular-box') {
- throw new Error('Expected regular box');
- }
-
- const minfBox = mdiaBox.children[2];
- if (minfBox.type !== 'regular-box') {
- throw new Error('Expected minf box');
- }
-
- const stblBox = minfBox.children[3];
- if (stblBox.type !== 'regular-box') {
- throw new Error('Expected stbl box');
- }
-
- const stsdBox = stblBox.children[0];
- if (stsdBox.type !== 'stsd-box') {
- throw new Error('Expected stsd box');
- }
-
const duration = getDuration(parsed);
expect(duration).toBe(3.4);
+
+ const dimensions = getDimensions(parsed);
+ expect(dimensions).toEqual([1920, 1080]);
});