Skip to content

🎥 Make videos programmatically with React

some progress

Jonny Burger6eb3ce2ce803
diff --git a/packages/video-parser/.eslintrc b/packages/video-parser/.eslintrc
new file mode 100644
index 0000000000..e11b5ec8b1
--- /dev/null
+++ b/packages/video-parser/.eslintrc
@@ -0,0 +1,7 @@
+{
+ "extends": "@jonny/eslint-config",
+ "rules": {
+ "no-bitwise": "off",
+ "@typescript-eslint/no-use-before-define": "off"
+ }
+}
diff --git a/packages/video-parser/src/get-duration.ts b/packages/video-parser/src/get-duration.ts
new file mode 100644
index 0000000000..7daeb499ee
--- /dev/null
+++ b/packages/video-parser/src/get-duration.ts
@@ -0,0 +1,3 @@
+import { Box } from "./parse-video";
+
+export const getDuration = (box: Box) => {};
diff --git a/packages/video-parser/src/parse-video.tsx b/packages/video-parser/src/parse-video.ts
similarity index 95%
rename from packages/video-parser/src/parse-video.tsx
rename to packages/video-parser/src/parse-video.ts
index 7c23cf6940..3366c710e1 100644
--- a/packages/video-parser/src/parse-video.tsx
+++ b/packages/video-parser/src/parse-video.ts
@@ -21,13 +21,48 @@ type ExtraData =
boxes: Box[];
};
-type Box = {
+export type Box = {
boxType: string;
boxSize: number;
extraData: ExtraData | undefined;
offset: number;
};
+const getExtraDataFromBox = (
+ box: Buffer,
+ type: string
+): ExtraData | undefined => {
+ if (type === "ftyp") {
+ return parseFtyp(box);
+ }
+
+ if (
+ type === "moov" ||
+ type === "trak" ||
+ type === "mdia" ||
+ type === "minf" ||
+ type === "stbl" ||
+ type === "stsb"
+ ) {
+ return parseBoxes(box.subarray(8));
+ }
+};
+
+const processBoxAndSubtract = (data: Buffer, offset: number): BoxAndNext => {
+ const boxSize = fourByteToNumber(data, 0);
+ const boxType = data.subarray(4, 8).toString("utf-8");
+
+ const next = data.subarray(boxSize);
+
+ return {
+ next,
+ boxType,
+ boxSize,
+ extraData: getExtraDataFromBox(data.subarray(0, boxSize), boxType),
+ offset: offset + boxSize,
+ };
+};
+
type BoxAndNext = Box & {
next: Buffer;
};
@@ -35,7 +70,7 @@ type BoxAndNext = Box & {
const isoBaseMediaMp4Pattern = Buffer.from("ftyp");
const parseBoxes = (data: Buffer): ExtraData => {
- let boxes: Box[] = [];
+ const boxes: Box[] = [];
let remaining = data;
let bytesConsumed = 0;
while (remaining.length > 0) {
@@ -47,6 +82,7 @@ const parseBoxes = (data: Buffer): ExtraData => {
boxes.push({ boxType, boxSize, extraData, offset: bytesConsumed });
bytesConsumed = offset;
}
+
return { boxes, type: "boxes" };
};
@@ -56,8 +92,8 @@ const parseFtyp = (data: Buffer): ExtraData => {
const minorVersion = fourByteToNumber(data, 12);
const rest = data.subarray(16);
- let types = rest.length / 4;
- let compatibleBrands: string[] = [];
+ const types = rest.length / 4;
+ const compatibleBrands: string[] = [];
for (let i = 0; i < types; i++) {
const fourBytes = rest.subarray(i * 4, i * 4 + 4);
compatibleBrands.push(fourBytes.toString("utf-8").trim());
@@ -71,42 +107,6 @@ const parseFtyp = (data: Buffer): ExtraData => {
};
};
-const getExtraDataFromBox = (
- box: Buffer,
- type: string
-): ExtraData | undefined => {
- if (type === "ftyp") {
- return parseFtyp(box);
- }
-
- if (
- type === "moov" ||
- type === "trak" ||
- type === "mdia" ||
- type === "minf" ||
- type === "stbl" ||
- type === "stsb"
- ) {
- return parseBoxes(box.subarray(8));
- }
- return;
-};
-
-const processBoxAndSubtract = (data: Buffer, offset: number): BoxAndNext => {
- const boxSize = fourByteToNumber(data, 0);
- const boxType = data.subarray(4, 8).toString("utf-8");
-
- const next = data.subarray(boxSize);
-
- return {
- next,
- boxType,
- boxSize,
- extraData: getExtraDataFromBox(data.subarray(0, boxSize), boxType),
- offset: offset + boxSize,
- };
-};
-
const matchesPattern = (pattern: Buffer) => {
return (data: Buffer) => {
return pattern.every((value, index) => data[index] === value);
diff --git a/packages/video-parser/src/test/parse-video.test.ts b/packages/video-parser/src/test/parse-video.test.ts
index bd8cdf120f..e874dbdc9b 100644
--- a/packages/video-parser/src/test/parse-video.test.ts
+++ b/packages/video-parser/src/test/parse-video.test.ts
@@ -124,5 +124,4 @@ test("Parse a full video", async () => {
if (!moov) {
throw new Error("No extra data");
}
- console.log(JSON.stringify(moov));
});