Skip to content

🎥 Make videos programmatically with React

keep it simple for now

Jonny Burgeraa6a341c2af5
diff --git a/packages/video-parser/src/parse-video.tsx b/packages/video-parser/src/parse-video.tsx
index 4d316cd148..7c23cf6940 100644
--- a/packages/video-parser/src/parse-video.tsx
+++ b/packages/video-parser/src/parse-video.tsx
@@ -9,28 +9,48 @@ const fourByteToNumber = (data: Buffer, from: number) => {
);
};
+type ExtraData =
+ | {
+ type: "ftyp-box";
+ majorBrand: string;
+ minorVersion: number;
+ compatibleBrands: string[];
+ }
+ | {
+ type: "boxes";
+ boxes: Box[];
+ };
+
type Box = {
- next: Buffer;
boxType: string;
boxSize: number;
- extraData: unknown | null;
+ extraData: ExtraData | undefined;
+ offset: number;
+};
+
+type BoxAndNext = Box & {
+ next: Buffer;
};
const isoBaseMediaMp4Pattern = Buffer.from("ftyp");
-const parseBoxes = (data: Buffer) => {
- let boxes = [];
+const parseBoxes = (data: Buffer): ExtraData => {
+ let boxes: Box[] = [];
let remaining = data;
+ let bytesConsumed = 0;
while (remaining.length > 0) {
- const { next, boxType, boxSize, extraData } =
- processBoxAndSubtract(remaining);
+ const { next, boxType, boxSize, extraData, offset } = processBoxAndSubtract(
+ remaining,
+ bytesConsumed
+ );
remaining = next;
- boxes.push({ boxType, boxSize, extraData });
+ boxes.push({ boxType, boxSize, extraData, offset: bytesConsumed });
+ bytesConsumed = offset;
}
- return boxes;
+ return { boxes, type: "boxes" };
};
-const parseFtyp = (data: Buffer) => {
+const parseFtyp = (data: Buffer): ExtraData => {
const majorBrand = data.subarray(8, 12).toString("utf-8").trim();
const minorVersion = fourByteToNumber(data, 12);
@@ -51,21 +71,28 @@ const parseFtyp = (data: Buffer) => {
};
};
-const parseMoov = (data: Buffer) => {
- return parseBoxes(data);
-};
-
-const getExtraDataFromBox = (box: Buffer, type: string) => {
+const getExtraDataFromBox = (
+ box: Buffer,
+ type: string
+): ExtraData | undefined => {
if (type === "ftyp") {
return parseFtyp(box);
}
- if (type === "moov") {
- return parseMoov(box.subarray(8));
+
+ if (
+ type === "moov" ||
+ type === "trak" ||
+ type === "mdia" ||
+ type === "minf" ||
+ type === "stbl" ||
+ type === "stsb"
+ ) {
+ return parseBoxes(box.subarray(8));
}
return;
};
-const processBoxAndSubtract = (data: Buffer): Box => {
+const processBoxAndSubtract = (data: Buffer, offset: number): BoxAndNext => {
const boxSize = fourByteToNumber(data, 0);
const boxType = data.subarray(4, 8).toString("utf-8");
@@ -76,6 +103,7 @@ const processBoxAndSubtract = (data: Buffer): Box => {
boxType,
boxSize,
extraData: getExtraDataFromBox(data.subarray(0, boxSize), boxType),
+ offset: offset + boxSize,
};
};
@@ -101,6 +129,9 @@ export const parseVideo = async (src: string, bytes: number) => {
});
if (matchesPattern(isoBaseMediaMp4Pattern)(data.subarray(4, 8))) {
- return parseBoxes(data);
+ const boxes = parseBoxes(data);
+ if (boxes.type === "boxes") {
+ return boxes.boxes;
+ }
}
};
diff --git a/packages/video-parser/src/test/parse-video.test.ts b/packages/video-parser/src/test/parse-video.test.ts
index 15c5aef4cf..bd8cdf120f 100644
--- a/packages/video-parser/src/test/parse-video.test.ts
+++ b/packages/video-parser/src/test/parse-video.test.ts
@@ -6,6 +6,7 @@ test("Parse Big Buck bunny", async () => {
const data = await parseVideo(exampleVideos.bigBuckBunny, 4 * 1024);
expect(data).toEqual([
{
+ offset: 0,
boxSize: 32,
boxType: "ftyp",
extraData: {
@@ -16,6 +17,7 @@ test("Parse Big Buck bunny", async () => {
},
},
{
+ offset: 32,
boxSize: 8,
boxType: "free",
extraData: undefined,
@@ -24,6 +26,7 @@ test("Parse Big Buck bunny", async () => {
boxSize: 14282275,
boxType: "mdat",
extraData: undefined,
+ offset: 40,
},
]);
});
@@ -40,13 +43,19 @@ test("Parse an iPhone video", async () => {
minorVersion: 0,
compatibleBrands: ["qt"],
},
+ offset: 0,
+ },
+ {
+ boxType: "wide",
+ boxSize: 8,
+ extraData: undefined,
+ offset: 20,
},
- { boxType: "wide", boxSize: 8, extraData: undefined },
-
{
boxSize: 39048800,
boxType: "mdat",
extraData: undefined,
+ offset: 28,
},
]);
});
@@ -58,6 +67,7 @@ test("Parse framer", async () => {
);
expect(parsed).toEqual([
{
+ offset: 0,
boxSize: 32,
boxType: "ftyp",
extraData: {
@@ -68,11 +78,13 @@ test("Parse framer", async () => {
},
},
{
+ offset: 32,
boxSize: 8,
boxType: "free",
extraData: undefined,
},
{
+ offset: 40,
boxSize: 73010,
boxType: "mdat",
extraData: undefined,
@@ -82,43 +94,35 @@ test("Parse framer", async () => {
test("Parse a full video", async () => {
const data = await parseVideo(exampleVideos.framer24fps, Infinity);
- expect(data).toEqual([
- {
- boxSize: 32,
- boxType: "ftyp",
- extraData: {
- type: "ftyp-box",
- majorBrand: "isom",
- minorVersion: 512,
- compatibleBrands: ["isom", "iso2", "avc1", "mp41"],
- },
- },
- { boxType: "free", boxSize: 8, extraData: undefined },
- {
- boxSize: 57014,
- boxType: "mdat",
- extraData: undefined,
- },
- {
- boxSize: 1929,
- boxType: "moov",
- extraData: [
- {
- boxType: "mvhd",
- boxSize: 108,
- extraData: undefined,
- },
- {
- boxType: "trak",
- boxSize: 1715,
- extraData: undefined,
- },
- {
- boxType: "udta",
- boxSize: 98,
- extraData: undefined,
- },
- ],
+ if (!data) throw new Error("No data");
+
+ expect(data[0]).toEqual({
+ offset: 0,
+ boxSize: 32,
+ boxType: "ftyp",
+ extraData: {
+ type: "ftyp-box",
+ majorBrand: "isom",
+ minorVersion: 512,
+ compatibleBrands: ["isom", "iso2", "avc1", "mp41"],
},
- ]);
+ });
+ expect(data[1]).toEqual({
+ offset: 32,
+ boxType: "free",
+ boxSize: 8,
+ extraData: undefined,
+ });
+ expect(data[2]).toEqual({
+ offset: 40,
+ boxSize: 57014,
+ boxType: "mdat",
+ extraData: undefined,
+ });
+
+ const moov = data[3].extraData;
+ if (!moov) {
+ throw new Error("No extra data");
+ }
+ console.log(JSON.stringify(moov));
});