🎥 Make videos programmatically with React
progress testbed
Jonny Burgere3f2c8c66eb3
diff --git a/packages/example/src/Encoder/SrcEncoder.tsx b/packages/example/src/Encoder/SrcEncoder.tsxindex 45bfcb5ac4..c9a0e5753e 100644--- a/packages/example/src/Encoder/SrcEncoder.tsx+++ b/packages/example/src/Encoder/SrcEncoder.tsx@@ -1,10 +1,51 @@import {OnAudioTrack, OnVideoTrack, parseMedia} from '@remotion/media-parser';import React, {useCallback, useRef} from 'react';import {flushSync} from 'react-dom';+import {AbsoluteFill} from 'remotion';+import {fitElementSizeInContainer} from './fit-element-size-in-container';++const CANVAS_WIDTH = 1024 / 4;+const CANVAS_HEIGHT = (CANVAS_WIDTH / 16) * 9;++const SampleLabel: React.FC<{+ children: React.ReactNode;+}> = ({children}) => {+ return (+ <div+ style={{+ height: 18,+ width: 18,+ fontSize: 11,+ border: '1px solid black',+ display: 'inline-flex',+ justifyContent: 'center',+ alignItems: 'center',+ borderRadius: 5,+ marginRight: 4,+ fontFamily: 'Arial',+ }}+ >+ {children}+ </div>+ );+};++const SampleCount: React.FC<{+ count: number;+ label: string;+}> = ({count, label}) => {+ return (+ <div style={{display: 'inline-block'}}>+ <SampleLabel>{label}</SampleLabel>+ {count}+ </div>+ );+};export const SrcEncoder: React.FC<{src: string;-}> = ({src}) => {+ label: string;+}> = ({src, label}) => {const [samples, setSamples] = React.useState<number>(0);const [videoFrames, setVideoFrames] = React.useState<number>(0);const [audioFrames, setAudioFrames] = React.useState<number>(0);@@ -28,15 +69,23 @@ export const SrcEncoder: React.FC<{async output(inputFrame) {i.current++;if (i.current % 10 === 1) {- const aspectRatio =- track.displayAspectWidth / track.displayAspectHeight;+ const fitted = fitElementSizeInContainer({+ containerSize: {+ width: CANVAS_WIDTH,+ height: CANVAS_HEIGHT,+ },+ elementSize: {+ width: inputFrame.displayWidth,+ height: inputFrame.displayHeight,+ },+ });const image = await createImageBitmap(inputFrame, {- resizeHeight: 100 / aspectRatio,- resizeWidth: 100,+ resizeHeight: fitted.height,+ resizeWidth: fitted.width,});- ref.current?.getContext('2d')?.drawImage(image, 0, 0);+ ref.current?.getContext('2d')?.drawImage(image, fitted.left, 0);}flushSync(() => {setVideoFrames((prev) => prev + 1);@@ -120,32 +169,47 @@ export const SrcEncoder: React.FC<{}, []);const onClick = useCallback(() => {- console.time('done');parseMedia({src,onVideoTrack,onAudioTrack,- }).then(() => {- console.timeEnd('done');- });+ }).then(() => {});}, [onAudioTrack, onVideoTrack, src]);return (<divstyle={{- height: 200,- width: 200,- background: 'red',+ height: 1024 / 4,+ width: 1024 / 4,+ padding: 10,+ display: 'inline-block',+ position: 'relative',+ marginBottom: 0,}}>- {src}{' '}- <button type="button" onClick={onClick}>- Decode- </button>- {samples > 0 && <div>{samples} samples</div>}- {videoFrames > 0 && <div>{videoFrames} video frames</div>}- {audioFrames > 0 && <div>{audioFrames} audio frames</div>}- <canvas ref={ref} width={100} height={50} />+ <AbsoluteFill+ style={{+ background: 'white',+ }}+ >+ <canvas+ ref={ref}+ width={CANVAS_WIDTH}+ height={CANVAS_HEIGHT}+ style={{+ background: 'black',+ }}+ />+ {label}{' '}+ <button type="button" onClick={onClick}>+ Decode+ </button>+ <div style={{display: 'flex', flexDirection: 'row', gap: 10}}>+ <SampleCount count={samples} label="S" />+ <SampleCount count={videoFrames} label="V" />+ <SampleCount count={audioFrames} label="A" />+ </div>+ </AbsoluteFill></div>);};diff --git a/packages/example/src/Encoder/fit-element-size-in-container.ts b/packages/example/src/Encoder/fit-element-size-in-container.tsnew file mode 100644index 0000000000..b90da2002d--- /dev/null+++ b/packages/example/src/Encoder/fit-element-size-in-container.ts@@ -0,0 +1,42 @@+import type {Dimensions} from '@remotion/layout-utils';++type Rect = {+ width: number;+ height: number;+ top: number;+ left: number;+};++// Resize an aspect ratio to fit into a container+// and center it+export const fitElementSizeInContainer = ({+ containerSize,+ elementSize,+}: {+ containerSize: Dimensions;+ elementSize: Dimensions;+}): Rect => {+ const heightRatio = containerSize.height / elementSize.height;+ const widthRatio = containerSize.width / elementSize.width;++ const ratio = Math.min(heightRatio, widthRatio);++ const newWidth = elementSize.width * ratio;+ const newHeight = elementSize.height * ratio;++ if (+ newWidth > containerSize.width + 0.000001 ||+ newHeight > containerSize.height + 0.000001+ ) {+ throw new Error(+ `Element is too big to fit into the container. Max size: ${containerSize.width}x${containerSize.height}, element size: ${newWidth}x${newHeight}`,+ );+ }++ return {+ width: newWidth,+ height: newHeight,+ top: (containerSize.height - newHeight) / 2,+ left: (containerSize.width - newWidth) / 2,+ };+};diff --git a/packages/example/src/EncoderDemo.tsx b/packages/example/src/EncoderDemo.tsxindex a3e328a37a..adfaa6d806 100644--- a/packages/example/src/EncoderDemo.tsx+++ b/packages/example/src/EncoderDemo.tsx@@ -1,4 +1,3 @@-import {parseVideo, reencodeVideo} from '@remotion/browser-renderer';import React from 'react';import {staticFile} from 'remotion';import {SrcEncoder} from './Encoder/SrcEncoder';@@ -6,36 +5,44 @@ import {SrcEncoder} from './Encoder/SrcEncoder';export const EncoderDemo: React.FC = () => {return (<>- <div- style={{- overflowY: 'scroll',- }}- >- <div>- <input- type="file"- onChange={(e) => parseVideo(e.target.files?.[0] as File)}- />- <input- type="file"- onChange={(e) => reencodeVideo(e.target.files?.[0] as File)}- />- </div>- <SrcEncoder src={staticFile('av1.webm')} />- <SrcEncoder src={staticFile('av1.mp4')} />- <SrcEncoder src={staticFile('mp4-mp3.mp4')} />- <SrcEncoder src={staticFile('bigbuckbunny.mp4')} />- <SrcEncoder src={staticFile('corrupted.mp4')} />- <SrcEncoder src={staticFile('matroska-pcm16.mkv')} />- <SrcEncoder src={staticFile('matroska-h265-aac.mkv')} />- <SrcEncoder src={staticFile('matroska-mp3.mkv')} />- <SrcEncoder src={staticFile('vid1.mp4')} />- <SrcEncoder src={staticFile('whip.mp3')} />- <SrcEncoder src={staticFile('iphone-hevc.mov')} />- <SrcEncoder src={staticFile('sample.aac')} />- <SrcEncoder src={staticFile('stretched-vp8.webm')} />- <SrcEncoder src={staticFile('opus.webm')} />- <SrcEncoder src="https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" />+ <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={staticFile('bigbuckbunny.mp4')}+ />+ <SrcEncoder+ label="corrupted-timestamps.mp4"+ src={staticFile('corrupted.mp4')}+ />+ <SrcEncoder+ label="lossless-audio.mkv"+ src={staticFile('matroska-pcm16.mkv')}+ />+ <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={staticFile('vid1.mp4')} />+ <SrcEncoder+ label="hevc-with-rotation.mov"+ src={staticFile('iphone-hevc.mov')}+ />+ <SrcEncoder+ label="vp8-with-stretch.webm"+ src={staticFile('stretched-vp8.webm')}+ />+ <SrcEncoder label="opus-audio.webm" src={staticFile('opus.webm')} />+ <SrcEncoder+ label="https://remotion.dev/bbb.mp4"+ src="https://remotion.dev/bbb.mp4"+ /></div></>);diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsxindex 9958661b9b..1a2b685112 100644--- a/packages/example/src/Root.tsx+++ b/packages/example/src/Root.tsx@@ -1443,8 +1443,8 @@ export const Index: React.FC = () => {<Stillid="EncoderDemo"component={EncoderDemo}- height={500}- width={800}+ height={1024}+ width={1024}/></>);diff --git a/vercel.json b/vercel.jsonindex 9a20d2cc70..7e758dab0c 100644--- a/vercel.json+++ b/vercel.json@@ -330,12 +330,36 @@{"source": "/bbb.webm","destination": "https://upload.wikimedia.org/wikipedia/commons/transcoded/c/c0/Big_Buck_Bunny_4K.webm/Big_Buck_Bunny_4K.webm.720p.vp9.webm",- "permanent": false+ "permanent": false,+ "headers": [+ {"key": "Access-Control-Allow-Credentials", "value": "true"},+ {"key": "Access-Control-Allow-Origin", "value": "*"},+ {+ "key": "Access-Control-Allow-Methods",+ "value": "GET,OPTIONS,PATCH,DELETE,POST,PUT"+ },+ {+ "key": "Access-Control-Allow-Headers",+ "value": "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version"+ }+ ]},{"source": "/bbb.mp4","destination": "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4",- "permanent": false+ "permanent": false,+ "headers": [+ {"key": "Access-Control-Allow-Credentials", "value": "true"},+ {"key": "Access-Control-Allow-Origin", "value": "*"},+ {+ "key": "Access-Control-Allow-Methods",+ "value": "GET,OPTIONS,PATCH,DELETE,POST,PUT"+ },+ {+ "key": "Access-Control-Allow-Headers",+ "value": "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version"+ }+ ]}]}