Skip to content

🎥 Make videos programmatically with React

Internal testbed: Remove encoder demo

Jonny Burger2bd9225094dd
diff --git a/packages/example/src/DecoderDemo.tsx b/packages/example/src/DecoderDemo.tsx
deleted file mode 100644
index 9543f15c49..0000000000
--- a/packages/example/src/DecoderDemo.tsx
+++ /dev/null
@@ -1,75 +0,0 @@
-import React, {useEffect, useState} from 'react';
-import {staticFile} from 'remotion';
-import {SrcEncoder} from './Encoder/SrcEncoder';
-
-export const DecoderDemo: React.FC = () => {
- const [blob, setBlob] = useState<string | null>(null);
-
- useEffect(() => {
- fetch('https://remotion.media/iphone-hevc.mov')
- .then((res) => res.blob())
- .then((buffer) => URL.createObjectURL(buffer))
- .then((url) => setBlob(url));
- }, []);
-
- return (
- <>
- <div style={{}}>
- <SrcEncoder label="av1-in-webm.webm" src={staticFile('av1.webm')} />
- <SrcEncoder label="av1-in-mp4.mp4" src={staticFile('av1.mp4')} />
- <SrcEncoder label="mp4-with-mp3.mp4" src={staticFile('mp4-mp3.mp4')} />
- <SrcEncoder
- label="bigbuckbunny.mp4"
- src="https://remotion.media/bigbuckbunny.mp4"
- />
- <SrcEncoder
- label="corrupted-timestamps.mp4"
- src="https://remotion.media/corrupted.mp4"
- />
- <SrcEncoder
- label="lossless-audio.mkv"
- src={staticFile('matroska-pcm16.mkv')}
- />
- <SrcEncoder
- label="https://remotion.media/BigBuckBunny.mp4"
- src="https://remotion.media/BigBuckBunny.mp4"
- />
- <SrcEncoder
- label="h265-with-aac.mkv"
- src={staticFile('matroska-h265-aac.mkv')}
- />
- <SrcEncoder
- label="mp3-in-mkv.mkv"
- src={staticFile('matroska-mp3.mkv')}
- />
- <SrcEncoder label="vid1.mp4" src="https://remotion.media/vid1.mp4" />
- <SrcEncoder
- label="hevc-with-rotation.mov"
- src="https://remotion.media/iphone-hevc.mov"
- />
- <SrcEncoder
- label="https://upload.wikimedia.org/wikipedia/commons/transcoded/c/c0/Big_Buck_Bunny_4K.webm/Big_Buck_Bunny_4K.webm.720p.vp9.webm"
- src="https://upload.wikimedia.org/wikipedia/commons/transcoded/c/c0/Big_Buck_Bunny_4K.webm/Big_Buck_Bunny_4K.webm.720p.vp9.webm"
- />
- <SrcEncoder
- label="vp8-with-stretch.webm"
- src="https://remotion.media/stretched-vp8.webm"
- />
- <SrcEncoder
- label="vp8-vorbis.webm"
- src={staticFile('vp8-vorbis.webm')}
- />
- <SrcEncoder label="opus-audio.webm" src={staticFile('opus.webm')} />
- <SrcEncoder
- label="transparent-with-dar.webm"
- src={staticFile('transparent-with-dar.webm')}
- />
- <SrcEncoder
- label="vp8-opus-5-1-channels.webm"
- src={staticFile('vp8-opus-5-1-channels.webm')}
- />
- {blob ? <SrcEncoder label="blob" src={blob} /> : null}
- </div>
- </>
- );
-};
diff --git a/packages/example/src/Encoder/SrcEncoder.tsx b/packages/example/src/Encoder/SrcEncoder.tsx
deleted file mode 100644
index 5755ce0952..0000000000
--- a/packages/example/src/Encoder/SrcEncoder.tsx
+++ /dev/null
@@ -1,258 +0,0 @@
-import {
- ALL_FORMATS,
- BufferTarget,
- Conversion,
- Input,
- Output,
- UrlSource,
- WebMOutputFormat,
-} from 'mediabunny';
-import React, {useCallback, useEffect, useRef, useState} from 'react';
-import {AbsoluteFill} from 'remotion';
-
-const CANVAS_WIDTH = 1024 / 4;
-const CANVAS_HEIGHT = (CANVAS_WIDTH / 16) * 9;
-
-const SampleLabel: React.FC<{
- readonly children: React.ReactNode;
-}> = ({children}) => {
- return (
- <div
- style={{
- height: 18,
- fontSize: 11,
- border: '1px solid white',
- display: 'inline-flex',
- justifyContent: 'center',
- alignItems: 'center',
- borderRadius: 5,
- marginRight: 4,
- padding: 3,
- fontFamily: 'Arial',
- color: 'white',
- }}
- >
- {children}
- </div>
- );
-};
-
-const SampleCount: React.FC<{
- readonly count: number;
- readonly label: string;
-}> = ({count, label}) => {
- return (
- <div style={{display: 'inline-block', color: 'white'}}>
- <SampleLabel>{label}</SampleLabel>
- {count}
- </div>
- );
-};
-
-export const SrcEncoder: React.FC<{
- readonly src: string;
- readonly label: string;
-}> = ({src, label}) => {
- const [state, setState] = useState({
- decodedAudioFrames: 0,
- decodedVideoFrames: 0,
- overallProgress: 0,
- });
-
- const [downloadFn, setDownloadFn] = useState<null | (() => void)>(null);
- const [abortfn, setAbortFn] = useState<null | (() => void)>(null);
- const [error, setError] = useState<Error | null>(null);
- const abortRef = useRef<null | (() => void)>(null);
- const ref = useRef<HTMLCanvasElement>(null);
-
- useEffect(() => {
- return () => abortRef.current?.();
- }, [src]);
-
- const onClick = useCallback(async () => {
- if (abortRef.current) {
- return;
- }
-
- const input = new Input({
- formats: ALL_FORMATS,
- source: new UrlSource(src),
- });
- const output = new Output({
- format: new WebMOutputFormat(),
- target: new BufferTarget(),
- });
- let conversion: Conversion | null = null;
- let cancelled = false;
- const abort = () => {
- cancelled = true;
- if (conversion) {
- void conversion.cancel();
- } else {
- input.dispose();
- }
- };
- abortRef.current = abort;
- setAbortFn(() => abort);
- setError(null);
- setDownloadFn(null);
- const progress = {
- decodedAudioFrames: 0,
- decodedVideoFrames: 0,
- overallProgress: 0,
- };
- setState({...progress});
-
- try {
- conversion = await Conversion.init({
- input,
- output,
- video: {
- codec: 'vp9',
- forceTranscode: true,
- process: (sample) => {
- progress.decodedVideoFrames++;
- const context = ref.current?.getContext('2d');
- if (context && progress.decodedVideoFrames % 10 === 1) {
- context.clearRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
- sample.drawWithFit(context, {fit: 'contain'});
- }
- return sample;
- },
- },
- audio: {
- codec: 'opus',
- forceTranscode: true,
- process: (sample) => {
- progress.decodedAudioFrames++;
- return sample;
- },
- },
- });
- if (cancelled) {
- return;
- }
- if (!conversion.isValid) {
- throw new Error('Cannot convert this file to WebM in this browser');
- }
- conversion.onProgress = (overallProgress) => {
- progress.overallProgress = overallProgress;
- setState({...progress});
- };
- await conversion.execute();
- const buffer = output.target.buffer;
- if (!buffer) {
- throw new Error('Conversion produced no output');
- }
- setState({...progress, overallProgress: 1});
- const file = new Blob([buffer], {type: 'video/webm'});
- setDownloadFn(() => () => {
- const a = document.createElement('a');
- const url = URL.createObjectURL(file);
- a.href = url;
- a.download = 'converted.webm';
- a.click();
- setTimeout(() => URL.revokeObjectURL(url), 1000);
- });
- } catch (err) {
- if (!cancelled) {
- setError(err as Error);
- }
- } finally {
- try {
- if (conversion && conversion.state !== 'canceled') {
- await conversion.cancel();
- } else {
- await output.cancel();
- }
- } finally {
- input.dispose();
- abortRef.current = null;
- setAbortFn(null);
- }
- }
- }, [src]);
-
- return (
- <div
- style={{
- height: 200,
- width: 1024 / 4,
- padding: 10,
- display: 'inline-block',
- position: 'relative',
- marginBottom: -4,
- }}
- >
- <AbsoluteFill
- style={{
- background: 'black',
- textAlign: 'center',
- fontFamily: 'Arial',
- }}
- >
- <canvas
- ref={ref}
- width={CANVAS_WIDTH}
- height={CANVAS_HEIGHT}
- style={{
- background: 'black',
- }}
- />
- <div
- style={{
- color: 'white',
- height: 20,
- position: 'absolute',
- textAlign: 'left',
- width: 1024 / 4,
- wordBreak: 'break-word',
- fontSize: 14,
- padding: 5,
- }}
- >
- {label}{' '}
- </div>
-
- {error ? (
- <div style={{color: 'red'}}>{error.message}</div>
- ) : downloadFn ? (
- <button type="button" onClick={downloadFn}>
- Download
- </button>
- ) : abortfn ? (
- <button
- type="button"
- onClick={() => {
- abortfn();
- }}
- >
- Abort
- </button>
- ) : (
- <button type="button" onClick={onClick}>
- Decode
- </button>
- )}
- <div
- style={{
- display: 'flex',
- flexDirection: 'row',
- gap: 10,
- justifyContent: 'center',
- alignItems: 'center',
- height: 38,
- }}
- >
- <SampleCount count={state.decodedVideoFrames} label="VD" />
- <SampleCount count={state.decodedAudioFrames} label="AD" />
- <SampleCount
- count={Math.round(state.overallProgress * 100)}
- label="%"
- />
- <br />
- </div>
- </AbsoluteFill>
- </div>
- );
-};
diff --git a/packages/example/src/Encoder/ahead-of-track.ts b/packages/example/src/Encoder/ahead-of-track.ts
deleted file mode 100644
index 13a34fdd3f..0000000000
--- a/packages/example/src/Encoder/ahead-of-track.ts
+++ /dev/null
@@ -1,14 +0,0 @@
-export const getMicroSecondsAheadOfTrack = (
- tracks: Record<number, number>,
- trackNumber: number,
-) => {
- const currentProgress = tracks[trackNumber] ?? 0;
- const otherProgresses = Object.entries(tracks)
- .filter(([key]) => key !== String(trackNumber))
- .map(([, value]) => value);
- const minProgress = Math.min(...otherProgresses);
-
- const ahead = Math.max(0, currentProgress - minProgress);
-
- return ahead;
-};
diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsx
index 279b1a8573..fe9e8a5868 100644
--- a/packages/example/src/Root.tsx
+++ b/packages/example/src/Root.tsx
@@ -41,7 +41,6 @@ import {ControlsShowcase} from './ControlsShowcase';
import CorruptVideo from './CorruptVideo';
import {CssLoaderTest} from './CssLoaderTest';
import {DarkModeTest} from './DarkModeTest';
-import {DecoderDemo} from './DecoderDemo';
import {
DIFFERENT_SEGMENTS_AT_DIFFERENT_SPEEDS_DURATION,
DifferentSegmentsAtDifferentSpeeds,
@@ -2725,12 +2724,6 @@ export const Index: React.FC = () => {
durationInFrames={10_000}
/>
</Folder>
- <Still
- id="DecoderDemo"
- component={DecoderDemo}
- height={1000}
- width={1024}
- />
<Still id="Emojis" component={EmojiTestbed} height={800} width={1024} />
<Still id="HugeImage" component={HugeImage} height={9000} width={9000} />
<Folder name="3DEngine">