🎥 Make videos programmatically with React
read duration
Jonny Burger4e5a96e2563c
diff --git a/packages/video-parser/package.json b/packages/video-parser/package.jsonindex eaa51a1fc5..af96400330 100644--- a/packages/video-parser/package.json+++ b/packages/video-parser/package.json@@ -54,7 +54,5 @@"react","player"],- "publishConfig": {- "access": "public"- }+ "private": true}diff --git a/packages/video-parser/src/boxes/iso-base-media/mvhd.ts b/packages/video-parser/src/boxes/iso-base-media/mvhd.tsnew file mode 100644index 0000000000..8e59092706--- /dev/null+++ b/packages/video-parser/src/boxes/iso-base-media/mvhd.ts@@ -0,0 +1,104 @@+type ThreeDMatrix = [+ number,+ number,+ number,+ number,+ number,+ number,+ number,+ number,+ number,+];++export type MvhdBox = {+ durationInUnits: number;+ durationInSeconds: number;+ creationTime: number | null;+ modificationTime: number | null;+ timeScale: number;+ rate: number;+ volume: number;+ matrix: ThreeDMatrix;+ nextTrackId: number;+};++export const parseMvhd = (data: Buffer): MvhdBox => {+ let offset = 0;++ const size = data.readUInt32BE(offset);+ offset += 4;+ const atom = data.subarray(offset, offset + 4);+ if (atom.toString() !== "mvhd") {+ throw new Error(`Expected mvhd atom, got ${atom.toString("utf-8")}`);+ }++ offset += 4;+ const version = data.readUInt8(offset);+ if (version !== 0) {+ throw new Error(`Unsupported MVHD version ${version}`);+ }++ offset += 1;++ if (size !== 108) {+ throw new Error(`Expected mvhd size of version 0 to be 108, got ${size}`);+ }++ // Flags, we discard them+ data.subarray(offset, offset + 3);+ offset += 3;++ const creationTime = data.readUInt32BE(offset);+ offset += 4;++ const modificationTime = data.readUInt32BE(offset);+ offset += 4;++ const timeScale = data.readUInt32BE(offset);+ offset += 4;++ const durationInUnits = data.readUInt32BE(offset);+ const durationInSeconds = durationInUnits / timeScale;+ offset += 4;++ const rateArray = data.subarray(offset, offset + 4);+ const rate =+ rateArray[0] * 10 + rateArray[1] + rateArray[2] * 0.1 + rateArray[3] * 0.01;+ offset += 4;++ const volumeArray = data.subarray(offset, offset + 2);+ offset += 2;+ const volume = volumeArray[0] + volumeArray[1] * 0.1;++ // reserved 16bit+ offset += 2;++ // reserved 32bit x2+ offset += 4 * 2;++ // matrix+ const matrix: number[] = [];+ for (let i = 0; i < 9; i++) {+ matrix.push(data.readUInt32BE(offset));+ offset += 4;+ }++ // pre-defined+ offset += 4 * 6;++ // next track id+ const nextTrackId = data.readUInt32BE(offset);+ offset += 4;++ return {+ creationTime: creationTime === 0 ? null : 0,+ modificationTime: modificationTime === 0 ? null : 0,+ timeScale,+ durationInUnits,+ durationInSeconds,+ rate,+ volume,+ matrix: matrix as ThreeDMatrix,+ nextTrackId,+ };+};diff --git a/packages/video-parser/src/get-duration.ts b/packages/video-parser/src/get-duration.tsindex 7daeb499ee..e187c38aab 100644--- a/packages/video-parser/src/get-duration.ts+++ b/packages/video-parser/src/get-duration.ts@@ -1,3 +1,27 @@import { Box } from "./parse-video";-export const getDuration = (box: Box) => {};+export const getDuration = (boxes: Box[]): number | null => {+ const moovBox = boxes.find((b) => b.boxType === "moov");+ if (!moovBox) {+ return null;+ }+ const extraData = moovBox.extraData;+ if (!extraData) {+ return null;+ }++ if (extraData.type !== "boxes") {+ return null;+ }++ const mvhdBox = extraData.boxes.find((b) => b.boxType === "mvhd");+ if (!mvhdBox || !mvhdBox.extraData) {+ return null;+ }++ if (mvhdBox.extraData.type !== "mvhd-box") {+ throw new Error("Expected mvhd-box");+ }++ return mvhdBox.extraData.box.durationInSeconds;+};diff --git a/packages/video-parser/src/index.ts b/packages/video-parser/src/index.tsindex 5dd29b0b6b..79163c6963 100644--- a/packages/video-parser/src/index.ts+++ b/packages/video-parser/src/index.ts@@ -1 +1,2 @@export { parseVideo } from "./parse-video";+export { getDuration } from "./get-duration";diff --git a/packages/video-parser/src/parse-video.ts b/packages/video-parser/src/parse-video.tsindex 3366c710e1..958c098c9d 100644--- a/packages/video-parser/src/parse-video.ts+++ b/packages/video-parser/src/parse-video.ts@@ -1,4 +1,6 @@import { createReadStream } from "fs";+import type { MvhdBox } from "./boxes/iso-base-media/mvhd";+import { parseMvhd } from "./boxes/iso-base-media/mvhd";const fourByteToNumber = (data: Buffer, from: number) => {return (@@ -19,6 +21,10 @@ type ExtraData =| {type: "boxes";boxes: Box[];+ }+ | {+ type: "mvhd-box";+ box: MvhdBox;};export type Box = {@@ -36,6 +42,10 @@ const getExtraDataFromBox = (return parseFtyp(box);}+ if (type === "mvhd") {+ return { type: "mvhd-box", box: parseMvhd(box) };+ }+if (type === "moov" ||type === "trak" ||diff --git a/packages/video-parser/src/test/mvhd.test.ts b/packages/video-parser/src/test/mvhd.test.tsnew file mode 100644index 0000000000..5e2c38f101--- /dev/null+++ b/packages/video-parser/src/test/mvhd.test.ts@@ -0,0 +1,78 @@+import { test, expect } from "bun:test";+import { parseMvhd } from "../boxes/iso-base-media/mvhd";++test("Should be able to parse a MVHD box correctly", () => {+ const buffer = Buffer.from([+ // size, 32 bit+ 0, 0, 0, 108,+ // mvhd atom, 32 bit+ 109, 118, 104, 100,+ // version, 8 unsigned bit+ 0,+ // flags+ 0, 0, 0,+ // creation time+ 0, 0, 0, 0,+ // modification time+ 0, 0, 0, 0,+ // time scale+ 0, 0, 3, 232,+ // duration+ 0, 0, 16, 71,+ // rate+ 0, 1, 0, 0,+ // volume+ 1, 0,+ // reserved 16 bit+ 0, 0,+ // reserved 32 bit [0]+ 0, 0, 0, 0,+ // reserved 32 bit [1]+ 0, 0, 0, 0,+ // matrix [0]+ 0, 1, 0, 0,+ // matrix [1]+ 0, 0, 0, 0,+ // matrix [2]+ 0, 0, 0, 0,+ // matrix [3]+ 0, 0, 0, 0,+ // matrix [4]+ 0, 1, 0, 0,+ // matrix [5]+ 0, 0, 0, 0,+ // matrix [6]+ 0, 0, 0, 0,+ // matrix [7]+ 0, 0, 0, 0,+ // matrix [8]+ 64, 0, 0, 0,+ // predefined [0]+ 0, 0, 0, 0,+ // predefined [1]+ 0, 0, 0, 0,+ // predefined [2]+ 0, 0, 0, 0,+ // predefined [3]+ 0, 0, 0, 0,+ // predefined [4]+ 0, 0, 0, 0,+ // predefined [5]+ 0, 0, 0, 0,+ // next track id+ 0, 0, 0, 2,+ ]);++ const mvhd = parseMvhd(buffer);+ expect(mvhd).toEqual({+ creationTime: null,+ modificationTime: null,+ timeScale: 1000,+ durationInUnits: 4167,+ durationInSeconds: 4.167,+ rate: 1,+ volume: 1,+ matrix: [65536, 0, 0, 0, 65536, 0, 0, 0, 1073741824],+ nextTrackId: 2,+ });+});diff --git a/packages/video-parser/src/test/parse-video.test.ts b/packages/video-parser/src/test/parse-video.test.tsindex e874dbdc9b..6523966a94 100644--- a/packages/video-parser/src/test/parse-video.test.ts+++ b/packages/video-parser/src/test/parse-video.test.ts@@ -1,6 +1,7 @@import { test, expect } from "bun:test";import { parseVideo } from "../parse-video";import { exampleVideos } from "./example-videos";+import { getDuration } from "../get-duration";test("Parse Big Buck bunny", async () => {const data = await parseVideo(exampleVideos.bigBuckBunny, 4 * 1024);@@ -125,3 +126,13 @@ test("Parse a full video", async () => {throw new Error("No extra data");}});++test("Should get duration of video", async () => {+ const parsed = await parseVideo(exampleVideos.framer24fps, 128 * 1024);+ if (!parsed) {+ throw new Error("No parsed data");+ }++ const duration = getDuration(parsed);+ expect(duration).toBe(4.167);+});diff --git a/packages/video-parser/tsconfig.json b/packages/video-parser/tsconfig.jsonindex 66a04633f9..cab2e0faf8 100644--- a/packages/video-parser/tsconfig.json+++ b/packages/video-parser/tsconfig.json@@ -5,5 +5,6 @@"outDir": "./dist","skipLibCheck": true},- "references": []+ "references": [],+ "include": ["src"]}