Skip to content

🎥 Make videos programmatically with React

parse video test

Jonny Burger1d92298b392b
diff --git a/packages/renderer/src/index.ts b/packages/renderer/src/index.ts
index 10cfd3ec1a..0a02c9960c 100644
--- a/packages/renderer/src/index.ts
+++ b/packages/renderer/src/index.ts
@@ -144,6 +144,7 @@ import {
supportedAudioCodecs,
} from './options/audio-codec';
import {getShouldRenderAudio} from './render-has-audio';
+import {exampleVideos} from './test/example-videos';
import {toMegabytes} from './to-megabytes';
import {validatePuppeteerTimeout} from './validate-puppeteer-timeout';
import {validateBitrate} from './validate-videobitrate';
@@ -238,6 +239,7 @@ export const RenderInternals = {
codecSupportsMedia,
toMegabytes,
internalEnsureBrowser,
+ exampleVideos,
};
// Warn of potential performance issues with Apple Silicon (M1 chip under Rosetta)
diff --git a/packages/renderer/src/test/example-videos.ts b/packages/renderer/src/test/example-videos.ts
index 99df51b2ff..9d229b6557 100644
--- a/packages/renderer/src/test/example-videos.ts
+++ b/packages/renderer/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/.prettierrc.js b/packages/video-parser/.prettierrc.js
new file mode 100644
index 0000000000..32eb24f3bf
--- /dev/null
+++ b/packages/video-parser/.prettierrc.js
@@ -0,0 +1,14 @@
+module.exports = {
+ singleQuote: true,
+ bracketSpacing: false,
+ useTabs: true,
+ overrides: [
+ {
+ files: ['*.yml'],
+ options: {
+ singleQuote: false,
+ },
+ },
+ ],
+ plugins: [require.resolve('prettier-plugin-organize-imports')],
+};
diff --git a/packages/video-parser/package.json b/packages/video-parser/package.json
index af96400330..1f3a86e3d3 100644
--- a/packages/video-parser/package.json
+++ b/packages/video-parser/package.json
@@ -1,58 +1,56 @@
{
- "name": "@remotion/video-parser",
- "version": "4.0.150",
- "description": "CLI for Remotion",
- "main": "dist/index.js",
- "sideEffects": false,
- "bin": {
- "remotion": "remotion-cli.js"
- },
- "scripts": {
- "formatting": "prettier src --check",
- "lint": "eslint src --ext ts,tsx",
- "test": "vitest --run",
- "build": "tsc -d",
- "watch": "tsc -w"
- },
- "repository": {
- "url": "https://github.com/remotion-dev/remotion"
- },
- "bugs": {
- "url": "https://github.com/remotion-dev/remotion/issues"
- },
- "exports": {
- ".": "./dist/index.js",
- "./config": "./dist/config/index.js",
- "./package.json": "./package.json"
- },
- "typesVersions": {
- ">=1.0": {
- "config": [
- "dist/config/index.d.ts"
- ]
- }
- },
- "author": "Jonny Burger <[email protected]>",
- "license": "SEE LICENSE IN LICENSE.md",
- "dependencies": {},
- "devDependencies": {
- "@jonny/eslint-config": "3.0.276",
- "@types/bun": "^1.0.4",
- "@types/node": "18.14.6",
- "eslint": "8.42.0",
- "eslint-plugin-10x": "1.5.2",
- "eslint-plugin-react": "7.32.2",
- "eslint-plugin-react-hooks": "4.4.0",
- "prettier": "3.1.1",
- "prettier-plugin-organize-imports": "3.2.4",
- "vitest": "0.31.1"
- },
- "keywords": [
- "remotion",
- "ffmpeg",
- "video",
- "react",
- "player"
- ],
- "private": true
+ "name": "@remotion/video-parser",
+ "version": "4.0.150",
+ "description": "CLI for Remotion",
+ "main": "dist/index.js",
+ "sideEffects": false,
+ "scripts": {
+ "formatting": "prettier src --check",
+ "lint": "eslint src --ext ts,tsx",
+ "test": "vitest --run",
+ "build": "tsc -d",
+ "watch": "tsc -w"
+ },
+ "repository": {
+ "url": "https://github.com/remotion-dev/remotion"
+ },
+ "bugs": {
+ "url": "https://github.com/remotion-dev/remotion/issues"
+ },
+ "exports": {
+ ".": "./dist/index.js",
+ "./config": "./dist/config/index.js",
+ "./package.json": "./package.json"
+ },
+ "typesVersions": {
+ ">=1.0": {
+ "config": [
+ "dist/config/index.d.ts"
+ ]
+ }
+ },
+ "author": "Jonny Burger <[email protected]>",
+ "license": "SEE LICENSE IN LICENSE.md",
+ "dependencies": {},
+ "devDependencies": {
+ "@jonny/eslint-config": "3.0.276",
+ "@types/bun": "^1.0.4",
+ "@types/node": "18.14.6",
+ "@remotion/renderer": "workspace:",
+ "eslint": "8.42.0",
+ "eslint-plugin-10x": "1.5.2",
+ "eslint-plugin-react": "7.32.2",
+ "eslint-plugin-react-hooks": "4.4.0",
+ "prettier": "3.1.1",
+ "prettier-plugin-organize-imports": "3.2.4",
+ "vitest": "0.31.1"
+ },
+ "keywords": [
+ "remotion",
+ "ffmpeg",
+ "video",
+ "react",
+ "player"
+ ],
+ "private": true
}
diff --git a/packages/video-parser/src/parse-video.ts b/packages/video-parser/src/parse-video.ts
index 958c098c9d..d8a564bbc0 100644
--- a/packages/video-parser/src/parse-video.ts
+++ b/packages/video-parser/src/parse-video.ts
@@ -1,147 +1,147 @@
-import { createReadStream } from "fs";
-import type { MvhdBox } from "./boxes/iso-base-media/mvhd";
-import { parseMvhd } from "./boxes/iso-base-media/mvhd";
+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 (
- (data[from + 0] << 24) |
- (data[from + 1] << 16) |
- (data[from + 2] << 8) |
- data[from + 3]
- );
+ return (
+ (data[from + 0] << 24) |
+ (data[from + 1] << 16) |
+ (data[from + 2] << 8) |
+ data[from + 3]
+ );
};
type ExtraData =
- | {
- type: "ftyp-box";
- majorBrand: string;
- minorVersion: number;
- compatibleBrands: string[];
- }
- | {
- type: "boxes";
- boxes: Box[];
- }
- | {
- type: "mvhd-box";
- box: MvhdBox;
- };
+ | {
+ type: 'ftyp-box';
+ majorBrand: string;
+ minorVersion: number;
+ compatibleBrands: string[];
+ }
+ | {
+ type: 'boxes';
+ boxes: Box[];
+ }
+ | {
+ type: 'mvhd-box';
+ box: MvhdBox;
+ };
export type Box = {
- boxType: string;
- boxSize: number;
- extraData: ExtraData | undefined;
- offset: number;
+ boxType: string;
+ boxSize: number;
+ extraData: ExtraData | undefined;
+ offset: number;
};
const getExtraDataFromBox = (
- box: Buffer,
- type: string
+ box: Buffer,
+ type: string,
): ExtraData | undefined => {
- if (type === "ftyp") {
- return parseFtyp(box);
- }
-
- if (type === "mvhd") {
- return { type: "mvhd-box", box: parseMvhd(box) };
- }
-
- if (
- type === "moov" ||
- type === "trak" ||
- type === "mdia" ||
- type === "minf" ||
- type === "stbl" ||
- type === "stsb"
- ) {
- return parseBoxes(box.subarray(8));
- }
+ if (type === 'ftyp') {
+ return parseFtyp(box);
+ }
+
+ if (type === 'mvhd') {
+ return {type: 'mvhd-box', box: parseMvhd(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,
- };
+ 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;
+ next: Buffer;
};
-const isoBaseMediaMp4Pattern = Buffer.from("ftyp");
+const isoBaseMediaMp4Pattern = Buffer.from('ftyp');
const parseBoxes = (data: Buffer): ExtraData => {
- const boxes: Box[] = [];
- let remaining = data;
- let bytesConsumed = 0;
- while (remaining.length > 0) {
- const { next, boxType, boxSize, extraData, offset } = processBoxAndSubtract(
- remaining,
- bytesConsumed
- );
- remaining = next;
- boxes.push({ boxType, boxSize, extraData, offset: bytesConsumed });
- bytesConsumed = offset;
- }
-
- return { boxes, type: "boxes" };
+ const boxes: Box[] = [];
+ let remaining = data;
+ let bytesConsumed = 0;
+ while (remaining.length > 0) {
+ const {next, boxType, boxSize, extraData, offset} = processBoxAndSubtract(
+ remaining,
+ bytesConsumed,
+ );
+ remaining = next;
+ boxes.push({boxType, boxSize, extraData, offset: bytesConsumed});
+ bytesConsumed = offset;
+ }
+
+ return {boxes, type: 'boxes'};
};
const parseFtyp = (data: Buffer): ExtraData => {
- const majorBrand = data.subarray(8, 12).toString("utf-8").trim();
-
- const minorVersion = fourByteToNumber(data, 12);
-
- const rest = data.subarray(16);
- 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());
- }
-
- return {
- type: "ftyp-box",
- majorBrand,
- minorVersion,
- compatibleBrands,
- };
+ const majorBrand = data.subarray(8, 12).toString('utf-8').trim();
+
+ const minorVersion = fourByteToNumber(data, 12);
+
+ const rest = data.subarray(16);
+ 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());
+ }
+
+ return {
+ type: 'ftyp-box',
+ majorBrand,
+ minorVersion,
+ compatibleBrands,
+ };
};
const matchesPattern = (pattern: Buffer) => {
- return (data: Buffer) => {
- return pattern.every((value, index) => data[index] === value);
- };
+ return (data: Buffer) => {
+ return pattern.every((value, index) => data[index] === value);
+ };
};
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);
- });
-
- stream.on("error", (err) => {
- reject(err);
- });
- });
-
- if (matchesPattern(isoBaseMediaMp4Pattern)(data.subarray(4, 8))) {
- const boxes = parseBoxes(data);
- if (boxes.type === "boxes") {
- return boxes.boxes;
- }
- }
+ 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);
+ });
+
+ stream.on('error', (err) => {
+ reject(err);
+ });
+ });
+
+ if (matchesPattern(isoBaseMediaMp4Pattern)(data.subarray(4, 8))) {
+ const boxes = parseBoxes(data);
+ if (boxes.type === 'boxes') {
+ return boxes.boxes;
+ }
+ }
};
diff --git a/packages/video-parser/src/test/example-videos.ts b/packages/video-parser/src/test/example-videos.ts
deleted file mode 100644
index 92b867b8dc..0000000000
--- a/packages/video-parser/src/test/example-videos.ts
+++ /dev/null
@@ -1,32 +0,0 @@
-import path from "node:path";
-
-const examplePackage = path.join(__dirname, "..", "..", "..", "example");
-const docsPackage = path.join(__dirname, "..", "..", "..", "docs");
-
-export const exampleVideos = {
- bigBuckBunny: path.join(examplePackage, "public/bigbuckbunny.mp4"),
- transparentWebm: path.join(docsPackage, "/static/img/transparent-video.webm"),
- framerWithoutFileExtension: path.join(
- examplePackage,
- "public",
- "framermp4withoutfileextension"
- ),
- corrupted: path.join(examplePackage, "public", "corrupted.mp4"),
- customDar: path.join(examplePackage, "public", "custom-dar.mp4"),
- screenrecording: path.join(examplePackage, "public", "quick.mov"),
- nofps: path.join(examplePackage, "public", "nofps.webm"),
- variablefps: path.join(examplePackage, "public", "variablefps.webm"),
- zerotimestamp: path.join(examplePackage, "public", "zero-timestamp.mp4"),
- 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,
- "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 6523966a94..7d2c5bab51 100644
--- a/packages/video-parser/src/test/parse-video.test.ts
+++ b/packages/video-parser/src/test/parse-video.test.ts
@@ -1,138 +1,140 @@
-import { test, expect } from "bun:test";
-import { parseVideo } from "../parse-video";
-import { exampleVideos } from "./example-videos";
-import { getDuration } from "../get-duration";
+import {RenderInternals} from '@remotion/renderer';
+import {expect, test} from 'bun:test';
+import {getDuration} from '../get-duration';
+import {parseVideo} from '../parse-video';
-test("Parse Big Buck bunny", async () => {
- const data = await parseVideo(exampleVideos.bigBuckBunny, 4 * 1024);
- expect(data).toEqual([
- {
- offset: 0,
- boxSize: 32,
- boxType: "ftyp",
- extraData: {
- type: "ftyp-box",
- majorBrand: "isom",
- minorVersion: 512,
- compatibleBrands: ["isom", "iso2", "avc1", "mp41"],
- },
- },
- {
- offset: 32,
- boxSize: 8,
- boxType: "free",
- extraData: undefined,
- },
- {
- boxSize: 14282275,
- boxType: "mdat",
- extraData: undefined,
- offset: 40,
- },
- ]);
+const {exampleVideos} = RenderInternals;
+
+test('Parse Big Buck bunny', async () => {
+ const data = await parseVideo(exampleVideos.bigBuckBunny, 4 * 1024);
+ expect(data).toEqual([
+ {
+ offset: 0,
+ boxSize: 32,
+ boxType: 'ftyp',
+ extraData: {
+ type: 'ftyp-box',
+ majorBrand: 'isom',
+ minorVersion: 512,
+ compatibleBrands: ['isom', 'iso2', 'avc1', 'mp41'],
+ },
+ },
+ {
+ offset: 32,
+ boxSize: 8,
+ boxType: 'free',
+ extraData: undefined,
+ },
+ {
+ boxSize: 14282275,
+ boxType: 'mdat',
+ extraData: undefined,
+ offset: 40,
+ },
+ ]);
});
-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"],
- },
- offset: 0,
- },
- {
- boxType: "wide",
- boxSize: 8,
- extraData: undefined,
- offset: 20,
- },
- {
- boxSize: 39048800,
- boxType: "mdat",
- extraData: undefined,
- offset: 28,
- },
- ]);
+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'],
+ },
+ offset: 0,
+ },
+ {
+ boxType: 'wide',
+ boxSize: 8,
+ extraData: undefined,
+ offset: 20,
+ },
+ {
+ boxSize: 39048800,
+ boxType: 'mdat',
+ extraData: undefined,
+ offset: 28,
+ },
+ ]);
});
-test("Parse framer", async () => {
- const parsed = await parseVideo(
- exampleVideos.framerWithoutFileExtension,
- 4 * 1024
- );
- expect(parsed).toEqual([
- {
- offset: 0,
- boxSize: 32,
- boxType: "ftyp",
- extraData: {
- compatibleBrands: ["isom", "iso2", "avc1", "mp41"],
- majorBrand: "isom",
- minorVersion: 512,
- type: "ftyp-box",
- },
- },
- {
- offset: 32,
- boxSize: 8,
- boxType: "free",
- extraData: undefined,
- },
- {
- offset: 40,
- boxSize: 73010,
- boxType: "mdat",
- extraData: undefined,
- },
- ]);
+test('Parse framer', async () => {
+ const parsed = await parseVideo(
+ exampleVideos.framerWithoutFileExtension,
+ 4 * 1024,
+ );
+ expect(parsed).toEqual([
+ {
+ offset: 0,
+ boxSize: 32,
+ boxType: 'ftyp',
+ extraData: {
+ compatibleBrands: ['isom', 'iso2', 'avc1', 'mp41'],
+ majorBrand: 'isom',
+ minorVersion: 512,
+ type: 'ftyp-box',
+ },
+ },
+ {
+ offset: 32,
+ boxSize: 8,
+ boxType: 'free',
+ extraData: undefined,
+ },
+ {
+ offset: 40,
+ boxSize: 73010,
+ boxType: 'mdat',
+ extraData: undefined,
+ },
+ ]);
});
-test("Parse a full video", async () => {
- const data = await parseVideo(exampleVideos.framer24fps, Infinity);
- if (!data) throw new Error("No data");
+test('Parse a full video', async () => {
+ const data = await parseVideo(exampleVideos.framer24fps, Infinity);
+ 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,
- });
+ 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");
- }
+ const moov = data[3].extraData;
+ if (!moov) {
+ 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");
- }
+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);
+ const duration = getDuration(parsed);
+ expect(duration).toBe(4.167);
});
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 1653e23200..18373f9725 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -2203,6 +2203,9 @@ importers:
'@jonny/eslint-config':
specifier: 3.0.276
+ '@remotion/renderer':
+ specifier: 'workspace:'
+ version: link:../renderer
'@types/bun':
specifier: ^1.0.4
version: 1.0.4