Skip to content

🎥 Make videos programmatically with React

webcodec tests

Jonny Burger453f3e833179
diff --git a/packages/convert/app/components/tests/RunAll.tsx b/packages/convert/app/components/tests/RunAll.tsx
new file mode 100644
index 0000000000..db7fc77633
--- /dev/null
+++ b/packages/convert/app/components/tests/RunAll.tsx
@@ -0,0 +1,20 @@
+import {useCallback} from 'react';
+import {TestStructure} from '~/tests/test-structure';
+import {Button} from '../ui/button';
+
+export const RunAll: React.FC<{
+ tests: TestStructure[];
+}> = ({tests}) => {
+ const runTests = useCallback(async () => {
+ for (const test of tests) {
+ await test.run();
+ }
+ }, [tests]);
+
+ return (
+ <div className="flex flex-row">
+ <div className="flex-1" />
+ <Button onClick={runTests}>Run all</Button>
+ </div>
+ );
+};
diff --git a/packages/convert/app/components/tests/TestStateDisplay.tsx b/packages/convert/app/components/tests/TestStateDisplay.tsx
new file mode 100644
index 0000000000..b516108de7
--- /dev/null
+++ b/packages/convert/app/components/tests/TestStateDisplay.tsx
@@ -0,0 +1,33 @@
+import {formatSeconds} from '~/lib/format-seconds';
+import {TestState} from '~/tests/test-structure';
+
+export const TestStateDisplay: React.FC<{
+ testState: TestState;
+}> = ({testState}) => {
+ if (testState.type === 'succeeded') {
+ return (
+ <div className="text-green-700 text-sm font-bold">
+ Succeeded in {testState.timeInMilliseconds}ms
+ </div>
+ );
+ }
+ if (testState.type === 'failed') {
+ return (
+ <div className="text-red-500 text-sm font-bold">
+ Failed: {testState.error.message}
+ </div>
+ );
+ }
+
+ if (testState.type === 'running') {
+ if (testState.state) {
+ return (
+ <div className="text-sm">
+ {formatSeconds(testState.state.millisecondsWritten / 1000)}
+ </div>
+ );
+ }
+ return <div className="text-sm">Running...</div>;
+ }
+ return null;
+};
diff --git a/packages/convert/app/routes/test._index.tsx b/packages/convert/app/routes/test._index.tsx
new file mode 100644
index 0000000000..d60c32991c
--- /dev/null
+++ b/packages/convert/app/routes/test._index.tsx
@@ -0,0 +1,22 @@
+import React from 'react';
+import {RunAll} from '~/components/tests/RunAll';
+import {runBigBuckBunny, runBigBuckBunny2} from '~/tests/big-buck-bunny';
+import {TestStructure} from '~/tests/test-structure';
+import {TestComponent} from '~/tests/TestComponent';
+
+const tests: TestStructure[] = [runBigBuckBunny(), runBigBuckBunny2()];
+
+const TestSuite: React.FC = () => {
+ return (
+ <div className="bg-slate-50 min-h-screen w-screen">
+ <div className="w-full max-w-[900px] pt-16 m-auto gap-4 flex flex-col px-5">
+ <RunAll tests={tests} />
+ {tests.map((t) => {
+ return <TestComponent key={t.name} test={t} />;
+ })}
+ </div>
+ </div>
+ );
+};
+
+export default TestSuite;
diff --git a/packages/convert/app/tests/TestComponent.tsx b/packages/convert/app/tests/TestComponent.tsx
new file mode 100644
index 0000000000..6d1ec60b0d
--- /dev/null
+++ b/packages/convert/app/tests/TestComponent.tsx
@@ -0,0 +1,25 @@
+import React from 'react';
+import {TestStateDisplay} from '~/components/tests/TestStateDisplay';
+import {Button} from '~/components/ui/button';
+import {TestStructure, useTest} from './test-structure';
+
+export const TestComponent: React.FC<{
+ readonly test: TestStructure;
+}> = ({test}) => {
+ const {state, run} = useTest(test);
+
+ return (
+ <div className="border-black rounded border-2 p-4 bg-white">
+ <div className="flex flex-row">
+ <div>
+ <div className="font-brand font-bold text-xl">{test.name}</div>
+ <TestStateDisplay testState={state} />
+ </div>
+ <div className="flex-1" />
+ {state.type === 'not-run' ? (
+ <Button onClick={run}>Run test</Button>
+ ) : null}
+ </div>
+ </div>
+ );
+};
diff --git a/packages/convert/app/tests/big-buck-bunny.tsx b/packages/convert/app/tests/big-buck-bunny.tsx
new file mode 100644
index 0000000000..3447227bf6
--- /dev/null
+++ b/packages/convert/app/tests/big-buck-bunny.tsx
@@ -0,0 +1,32 @@
+import {convertMedia} from '@remotion/webcodecs';
+import {
+ addTestWatcher,
+ makeProgressReporter,
+ TestStructure,
+} from './test-structure';
+
+export const runBigBuckBunny = (): TestStructure => {
+ return addTestWatcher({
+ name: 'convert mp4 to webm',
+ async execute(onUpdate) {
+ await convertMedia({
+ src: 'https://remotion-assets.s3.eu-central-1.amazonaws.com/example-videos/riverside.mp4',
+ container: 'webm',
+ onProgress: makeProgressReporter(onUpdate),
+ });
+ },
+ });
+};
+
+export const runBigBuckBunny2 = (): TestStructure => {
+ return addTestWatcher({
+ name: 'Live Photo + LPCM audio codec',
+ async execute(onUpdate) {
+ await convertMedia({
+ src: 'https://remotion-assets.s3.eu-central-1.amazonaws.com/example-videos/livephoto-lpcm-audio.mov',
+ container: 'webm',
+ onProgress: makeProgressReporter(onUpdate),
+ });
+ },
+ });
+};
diff --git a/packages/convert/app/tests/test-structure.ts b/packages/convert/app/tests/test-structure.ts
new file mode 100644
index 0000000000..c9c499772f
--- /dev/null
+++ b/packages/convert/app/tests/test-structure.ts
@@ -0,0 +1,95 @@
+import {
+ ConvertMediaOnProgress,
+ ConvertMediaProgress,
+} from '@remotion/webcodecs';
+import React from 'react';
+
+export type TestState =
+ | {
+ type: 'not-run';
+ }
+ | {
+ type: 'running';
+ state: ConvertMediaProgress | null;
+ }
+ | {
+ type: 'failed';
+ error: Error;
+ }
+ | {
+ type: 'succeeded';
+ timeInMilliseconds: number;
+ };
+
+type TestUpdater = (up: TestState) => void;
+
+export const makeProgressReporter = (
+ testUpdater: TestUpdater,
+): ConvertMediaOnProgress => {
+ return (progress) => {
+ testUpdater({
+ type: 'running',
+ state: progress,
+ });
+ };
+};
+
+export type TestStructure = {
+ name: string;
+ execute: (onUpdate: (up: TestState) => void) => Promise<void>;
+ run: () => Promise<void>;
+ watchState: (cb: (state: TestState) => void) => void;
+};
+
+export const addTestWatcher = (
+ test: Omit<TestStructure, 'watchState' | 'run'>,
+): TestStructure => {
+ const watchers = new Set<(state: TestState) => void>();
+
+ return {
+ ...test,
+ watchState: (cb) => {
+ watchers.add(cb);
+ },
+ async run() {
+ try {
+ const startTime = Date.now();
+ for (const watcher of watchers) {
+ watcher({type: 'running', state: null});
+ }
+
+ await test.execute((stateUpdate) => {
+ for (const watcher of watchers) {
+ watcher(stateUpdate);
+ }
+ });
+ for (const watcher of watchers) {
+ watcher({
+ type: 'succeeded',
+ timeInMilliseconds: Date.now() - startTime,
+ });
+ }
+ } catch (e) {
+ for (const watcher of watchers) {
+ watcher({type: 'failed', error: e as Error});
+ }
+ }
+ },
+ };
+};
+
+export const useTest = (test: TestStructure) => {
+ const [state, setState] = React.useState<TestState>({type: 'not-run'});
+
+ React.useEffect(() => {
+ test.watchState((up) => {
+ setState(up);
+ });
+ }, [test]);
+
+ const run = React.useCallback(async () => {
+ await test.run();
+ }, [test]);
+
+ return {state, run};
+};
diff --git a/packages/media-parser/src/parse-media.ts b/packages/media-parser/src/parse-media.ts
index cc2197b4b2..f533b46e75 100644
--- a/packages/media-parser/src/parse-media.ts
+++ b/packages/media-parser/src/parse-media.ts
@@ -156,7 +156,6 @@ export const parseMedia: ParseMedia = async ({
logLevel,
'Continuing parsing of file, currently at position',
iterator.counter.getOffset(),
- getAvailableInfo(fields ?? {}, parseResult, state),
);
parseResult = await parseResult.continueParsing();
} else {