Skip to content

🎥 Make videos programmatically with React

parse multiple videos

Jonny Burgere3ae37371980
diff --git a/packages/video-parser/src/parse-video.tsx b/packages/video-parser/src/parse-video.tsx
index 1bab570287..acc80268e2 100644
--- a/packages/video-parser/src/parse-video.tsx
+++ b/packages/video-parser/src/parse-video.tsx
@@ -12,7 +12,7 @@ const fourByteToNumber = (data: Buffer, from: number) => {
const isoBaseMediaMp4Pattern = Buffer.from("ftyp");
const parseFtyp = (data: Buffer) => {
- const majorBrand = data.subarray(8, 12).toString("utf-8");
+ const majorBrand = data.subarray(8, 12).toString("utf-8").trim();
const minorVersion = fourByteToNumber(data, 12);
@@ -21,7 +21,7 @@ const parseFtyp = (data: Buffer) => {
let compatibleBrands: string[] = [];
for (let i = 0; i < types; i++) {
const fourBytes = rest.subarray(i * 4, i * 4 + 4);
- compatibleBrands.push(fourBytes.toString("utf-8"));
+ compatibleBrands.push(fourBytes.toString("utf-8").trim());
}
return {
@@ -58,8 +58,11 @@ const matchesPattern = (pattern: Buffer) => {
};
};
-export const parseVideo = async (src: string) => {
- const stream = createReadStream(src, { end: 4 * 1024 - 1 });
+export const parseVideo = async (src: string, bytes: number) => {
+ const stream = createReadStream(
+ src,
+ Number.isFinite(bytes) ? { end: bytes - 1 } : {}
+ );
const data = await new Promise<Buffer>((resolve, reject) => {
stream.on("data", (chunk) => {
resolve(chunk as Buffer);
diff --git a/packages/video-parser/src/test/example-videos.ts b/packages/video-parser/src/test/example-videos.ts
index d28e61d296..92b867b8dc 100644
--- a/packages/video-parser/src/test/example-videos.ts
+++ b/packages/video-parser/src/test/example-videos.ts
@@ -20,7 +20,12 @@ export const exampleVideos = {
webcam: path.join(examplePackage, "public", "webcam.webm"),
iphonevideo: path.join(examplePackage, "public", "iphonevideo.mov"),
av1: path.join(examplePackage, "public", "av1.webm"),
- framer24fps: path.join(examplePackage, "public", "framer-24fps.mp4"),
+ framer24fps: path.join(
+ examplePackage,
+ "src",
+ "resources",
+ "framer-24fps.mp4"
+ ),
music: path.join(examplePackage, "public", "music.mp3"),
notavideo: path.join(examplePackage, "public", "giphy.gif"),
notafile: path.join(examplePackage, "public", "doesnotexist"),
diff --git a/packages/video-parser/src/test/parse-video.test.ts b/packages/video-parser/src/test/parse-video.test.ts
index 4cdb02cf38..e71f899779 100644
--- a/packages/video-parser/src/test/parse-video.test.ts
+++ b/packages/video-parser/src/test/parse-video.test.ts
@@ -2,8 +2,8 @@ import { test, expect } from "bun:test";
import { parseVideo } from "../parse-video";
import { exampleVideos } from "./example-videos";
-test("Parse a video", async () => {
- const data = await parseVideo(exampleVideos.bigBuckBunny);
+test("Parse Big Buck bunny", async () => {
+ const data = await parseVideo(exampleVideos.bigBuckBunny, 4 * 1024);
expect(data).toEqual([
{
boxSize: 32,
@@ -27,3 +27,82 @@ test("Parse a video", async () => {
},
]);
});
+
+test("Parse an iPhone video", async () => {
+ const data = await parseVideo(exampleVideos.iphonevideo, 4 * 1024);
+ expect(data).toEqual([
+ {
+ boxSize: 20,
+ boxType: "ftyp",
+ extraData: {
+ type: "ftyp-box",
+ majorBrand: "qt",
+ minorVersion: 0,
+ compatibleBrands: ["qt"],
+ },
+ },
+ { boxType: "wide", boxSize: 8, extraData: undefined },
+
+ {
+ boxSize: 39048800,
+ boxType: "mdat",
+ extraData: undefined,
+ },
+ ]);
+});
+
+test("Parse framer", async () => {
+ const parsed = await parseVideo(
+ exampleVideos.framerWithoutFileExtension,
+ 4 * 1024
+ );
+ expect(parsed).toEqual([
+ {
+ boxSize: 32,
+ boxType: "ftyp",
+ extraData: {
+ compatibleBrands: ["isom", "iso2", "avc1", "mp41"],
+ majorBrand: "isom",
+ minorVersion: 512,
+ type: "ftyp-box",
+ },
+ },
+ {
+ boxSize: 8,
+ boxType: "free",
+ extraData: undefined,
+ },
+ {
+ boxSize: 73010,
+ boxType: "mdat",
+ extraData: undefined,
+ },
+ ]);
+});
+
+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: undefined,
+ },
+ ]);
+});