Skip to content

🎥 Make videos programmatically with React

video encoding benchmarks

Jonny Burgerdda380c5ca9a
diff --git a/packages/it-tests/package.json b/packages/it-tests/package.json
index 3aaaa3ab68..ef9e3b433d 100644
--- a/packages/it-tests/package.json
+++ b/packages/it-tests/package.json
@@ -25,6 +25,7 @@
"@remotion/noise": "workspace:*",
"@remotion/motion-blur": "workspace:*",
"@remotion/media-utils": "workspace:*",
+ "@remotion/media-parser": "workspace:*",
"@remotion/lottie": "workspace:*",
"@remotion/layout-utils": "workspace:*",
"@remotion/install-whisper-cpp": "workspace:*",
diff --git a/packages/it-tests/src/size-benchmark/benchmark.ts b/packages/it-tests/src/size-benchmark/benchmark.ts
new file mode 100644
index 0000000000..412de0d96a
--- /dev/null
+++ b/packages/it-tests/src/size-benchmark/benchmark.ts
@@ -0,0 +1,24 @@
+import {exampleVideos} from '@remotion/example-videos';
+import {getBenchmarks, saveBenchmark} from './persistance';
+import {runBenchmark} from './run-benchmark';
+
+const encoder = 'libx264';
+
+const exampleVideo = exampleVideos.bigBuckBunny;
+
+const crfsToTest = new Array(52).fill(true).map((_, i) => i);
+
+for (let crf of crfsToTest) {
+ const benchmarks = await getBenchmarks();
+ const item = await runBenchmark({
+ exampleVideo,
+ qualityControl: {
+ crf,
+ type: 'crf',
+ },
+ encoder,
+ benchmarks,
+ });
+ console.log(JSON.stringify(item));
+ await saveBenchmark([...benchmarks, item]);
+}
diff --git a/packages/it-tests/src/size-benchmark/persistance.ts b/packages/it-tests/src/size-benchmark/persistance.ts
new file mode 100644
index 0000000000..ee424bd6de
--- /dev/null
+++ b/packages/it-tests/src/size-benchmark/persistance.ts
@@ -0,0 +1,22 @@
+import path from 'path';
+import {BenchmarkItem, getBenchmarkKeyFromItem} from './types';
+
+export const getBenchmarks = async (): Promise<BenchmarkItem[]> => {
+ return (await Bun.file(
+ path.join(__dirname, 'results.json'),
+ ).json()) as BenchmarkItem[];
+};
+
+export const saveBenchmark = async (items: BenchmarkItem[]) => {
+ await Bun.write(
+ path.join(__dirname, 'results.json'),
+ JSON.stringify(items, null, 2),
+ );
+};
+
+export const hasBenchmark = (
+ items: BenchmarkItem[],
+ key: string,
+): BenchmarkItem | null => {
+ return items.find((i) => getBenchmarkKeyFromItem(i) === key) ?? null;
+};
diff --git a/packages/it-tests/src/size-benchmark/results.json b/packages/it-tests/src/size-benchmark/results.json
new file mode 100644
index 0000000000..93464053eb
--- /dev/null
+++ b/packages/it-tests/src/size-benchmark/results.json
@@ -0,0 +1,184 @@
+[
+ {
+ "filename": "bigbuckbunny.mp4",
+ "encoder": "libx264",
+ "quality": {
+ "crf": 0,
+ "type": "crf"
+ },
+ "width": 1280,
+ "height": 720,
+ "fps": 24,
+ "durationInSeconds": 60,
+ "size": 246604324,
+ "timeToEncodeInMs": 11777
+ },
+ {
+ "filename": "bigbuckbunny.mp4",
+ "encoder": "libx264",
+ "quality": {
+ "crf": 1,
+ "type": "crf"
+ },
+ "width": 1280,
+ "height": 720,
+ "fps": 24,
+ "durationInSeconds": 60,
+ "size": 98887629,
+ "timeToEncodeInMs": 11928
+ },
+ {
+ "filename": "bigbuckbunny.mp4",
+ "encoder": "libx264",
+ "quality": {
+ "crf": 2,
+ "type": "crf"
+ },
+ "width": 1280,
+ "height": 720,
+ "fps": 24,
+ "durationInSeconds": 60,
+ "size": 90903539,
+ "timeToEncodeInMs": 12362
+ },
+ {
+ "filename": "bigbuckbunny.mp4",
+ "encoder": "libx264",
+ "quality": {
+ "crf": 3,
+ "type": "crf"
+ },
+ "width": 1280,
+ "height": 720,
+ "fps": 24,
+ "durationInSeconds": 60,
+ "size": 83081856,
+ "timeToEncodeInMs": 13876
+ },
+ {
+ "filename": "bigbuckbunny.mp4",
+ "encoder": "libx264",
+ "quality": {
+ "crf": 4,
+ "type": "crf"
+ },
+ "width": 1280,
+ "height": 720,
+ "fps": 24,
+ "durationInSeconds": 60,
+ "size": 75594990,
+ "timeToEncodeInMs": 13975
+ },
+ {
+ "filename": "bigbuckbunny.mp4",
+ "encoder": "libx264",
+ "quality": {
+ "crf": 5,
+ "type": "crf"
+ },
+ "width": 1280,
+ "height": 720,
+ "fps": 24,
+ "durationInSeconds": 60,
+ "size": 68409627,
+ "timeToEncodeInMs": 13241
+ },
+ {
+ "filename": "bigbuckbunny.mp4",
+ "encoder": "libx264",
+ "quality": {
+ "crf": 6,
+ "type": "crf"
+ },
+ "width": 1280,
+ "height": 720,
+ "fps": 24,
+ "durationInSeconds": 60,
+ "size": 61812641,
+ "timeToEncodeInMs": 12394
+ },
+ {
+ "filename": "bigbuckbunny.mp4",
+ "encoder": "libx264",
+ "quality": {
+ "crf": 7,
+ "type": "crf"
+ },
+ "width": 1280,
+ "height": 720,
+ "fps": 24,
+ "durationInSeconds": 60,
+ "size": 55540797,
+ "timeToEncodeInMs": 12004
+ },
+ {
+ "filename": "bigbuckbunny.mp4",
+ "encoder": "libx264",
+ "quality": {
+ "crf": 8,
+ "type": "crf"
+ },
+ "width": 1280,
+ "height": 720,
+ "fps": 24,
+ "durationInSeconds": 60,
+ "size": 49413975,
+ "timeToEncodeInMs": 11783
+ },
+ {
+ "filename": "bigbuckbunny.mp4",
+ "encoder": "libx264",
+ "quality": {
+ "crf": 9,
+ "type": "crf"
+ },
+ "width": 1280,
+ "height": 720,
+ "fps": 24,
+ "durationInSeconds": 60,
+ "size": 43612594,
+ "timeToEncodeInMs": 12132
+ },
+ {
+ "filename": "bigbuckbunny.mp4",
+ "encoder": "libx264",
+ "quality": {
+ "crf": 10,
+ "type": "crf"
+ },
+ "width": 1280,
+ "height": 720,
+ "fps": 24,
+ "durationInSeconds": 60,
+ "size": 38383797,
+ "timeToEncodeInMs": 13105
+ },
+ {
+ "filename": "bigbuckbunny.mp4",
+ "encoder": "libx264",
+ "quality": {
+ "crf": 11,
+ "type": "crf"
+ },
+ "width": 1280,
+ "height": 720,
+ "fps": 24,
+ "durationInSeconds": 60,
+ "size": 33651997,
+ "timeToEncodeInMs": 12172
+ },
+ {
+ "filename": "bigbuckbunny.mp4",
+ "encoder": "libx264",
+ "quality": {
+ "crf": 12,
+ "type": "crf"
+ },
+ "width": 1280,
+ "height": 720,
+ "fps": 24,
+ "durationInSeconds": 60,
+ "size": 29730600,
+ "timeToEncodeInMs": 17480
+ }
+]
\ No newline at end of file
diff --git a/packages/it-tests/src/size-benchmark/run-benchmark.ts b/packages/it-tests/src/size-benchmark/run-benchmark.ts
new file mode 100644
index 0000000000..ed29928557
--- /dev/null
+++ b/packages/it-tests/src/size-benchmark/run-benchmark.ts
@@ -0,0 +1,86 @@
+import {parseMedia} from '@remotion/media-parser';
+import {nodeReader} from '@remotion/media-parser/node';
+import {$} from 'bun';
+import fs from 'fs';
+import os from 'os';
+import path from 'path';
+import {hasBenchmark} from './persistance';
+import {BenchmarkItem, getBenchmarkKey, QualityControl} from './types';
+
+const stringifyQualityControl = (qualityControl: QualityControl) => {
+ if (qualityControl.type === 'crf') {
+ return [`-crf`, `${qualityControl.crf}`];
+ }
+
+ throw new Error(
+ 'Unknown quality control ' + (qualityControl.type satisfies never),
+ );
+};
+
+export const runBenchmark = async ({
+ exampleVideo,
+ encoder,
+ qualityControl,
+ benchmarks,
+}: {
+ exampleVideo: string;
+ encoder: string;
+ qualityControl: QualityControl;
+ benchmarks: BenchmarkItem[];
+}): Promise<BenchmarkItem> => {
+ const filename = path.basename(exampleVideo);
+
+ const key = getBenchmarkKey({
+ filename,
+ encoder,
+ qualityControl,
+ });
+
+ const found = hasBenchmark(benchmarks, key);
+ if (found) {
+ return found;
+ }
+
+ const {dimensions, fps, durationInSeconds} = await parseMedia({
+ src: exampleVideo,
+ fields: {
+ dimensions: true,
+ durationInSeconds: true,
+ fps: true,
+ },
+ reader: nodeReader,
+ });
+
+ if (!durationInSeconds) {
+ throw new Error('Could not get duration of video');
+ }
+ if (!fps) {
+ throw new Error('Could not get fps of video');
+ }
+
+ const output = `${os.tmpdir()}/output.mp4`;
+
+ const time = Date.now();
+ await $`bunx remotion ffmpeg -hide_banner -i ${exampleVideo} -c:v ${encoder} ${stringifyQualityControl(qualityControl)} -an -y ${output}`
+ .cwd(path.join(__dirname, '..', '..', '..', 'example'))
+ .quiet();
+ const timeToEncodeInMs = Date.now() - time;
+
+ const size = fs.lstatSync(output).size;
+
+ fs.unlinkSync(output);
+
+ const item: BenchmarkItem = {
+ filename,
+ encoder,
+ quality: qualityControl,
+ width: dimensions.width,
+ height: dimensions.height,
+ fps,
+ durationInSeconds,
+ size,
+ timeToEncodeInMs,
+ };
+
+ return item;
+};
diff --git a/packages/it-tests/src/size-benchmark/types.ts b/packages/it-tests/src/size-benchmark/types.ts
new file mode 100644
index 0000000000..90f8e02757
--- /dev/null
+++ b/packages/it-tests/src/size-benchmark/types.ts
@@ -0,0 +1,46 @@
+export type QualityControl = {
+ type: 'crf';
+ crf: number;
+};
+
+export type BenchmarkItem = {
+ filename: string;
+ encoder: string;
+ quality: QualityControl;
+ width: number;
+ height: number;
+ fps: number;
+ durationInSeconds: number;
+ size: number;
+ timeToEncodeInMs: number;
+};
+
+const stringifyQualityControl = (qualityControl: QualityControl) => {
+ if (qualityControl.type === 'crf') {
+ return `crf-${qualityControl.crf}`;
+ }
+
+ throw new Error(
+ 'Unknown quality control type ' + (qualityControl.type satisfies never),
+ );
+};
+
+export const getBenchmarkKey = ({
+ filename,
+ encoder,
+ qualityControl,
+}: {
+ filename: string;
+ encoder: string;
+ qualityControl: QualityControl;
+}) => {
+ return [filename, encoder, stringifyQualityControl(qualityControl)].join('-');
+};
+
+export const getBenchmarkKeyFromItem = (item: BenchmarkItem) => {
+ return getBenchmarkKey({
+ filename: item.filename,
+ encoder: item.encoder,
+ qualityControl: item.quality,
+ });
+};
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index b98b6f5330..37d740499b 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -1219,6 +1219,9 @@ importers:
'@remotion/lottie':
specifier: workspace:*
version: link:../lottie
+ '@remotion/media-parser':
+ specifier: workspace:*
+ version: link:../media-parser
'@remotion/media-utils':
specifier: workspace:*
version: link:../media-utils