🎥 Make videos programmatically with React
video on canvas
Jonny Burger8b704546a73f
diff --git a/packages/docs/docs/video-manipulation.md b/packages/docs/docs/video-manipulation.mdnew file mode 100644index 0000000000..9d90c2addf--- /dev/null+++ b/packages/docs/docs/video-manipulation.md@@ -0,0 +1,133 @@+---+id: video-manipulation+title: Video manipulation+---++You can manipulate a video buffer by rendering it on a Canvas.++## Rendering a video on a Canvas - basic example++```tsx twoslash+import { useVideoConfig, Video } from "remotion";+import { useCallback, useEffect, useRef } from "react";+import React from "react";+import { AbsoluteFill } from "remotion";++export const VideoOnCanvas: React.FC = () => {+ const video = useRef<HTMLVideoElement>(null);+ const canvas = useRef<HTMLCanvasElement>(null);+ const { width, height } = useVideoConfig();++ const onVideoFrame = useCallback(() => {+ if (+ !canvas.current ||+ !video.current ||+ !video.current.requestVideoFrameCallback+ ) {+ return;+ }+ const context = canvas.current.getContext("2d");++ if (!context) {+ return;+ }++ context.filter = "grayscale(100%)";+ context.drawImage(video.current, 0, 0, width, height);+ video.current.requestVideoFrameCallback(() => onVideoFrame());+ }, [height, width]);++ useEffect(() => {+ if (!video.current || !video.current.requestVideoFrameCallback) {+ return;+ }+ video.current.requestVideoFrameCallback(() => onVideoFrame());+ }, [onVideoFrame]);++ return (+ <AbsoluteFill>+ <AbsoluteFill>+ <Video+ ref={video}+ style={{ opacity: 0 }}+ startFrom={300}+ src="http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"+ />+ </AbsoluteFill>+ <AbsoluteFill>+ <canvas ref={canvas} width={width} height={height} />+ </AbsoluteFill>+ </AbsoluteFill>+ );+};+```++## Video manipulation example - Greenscreen++```tsx twoslash+import { useVideoConfig, Video } from "remotion";+import { useCallback, useEffect, useRef } from "react";+import React from "react";+import { AbsoluteFill } from "remotion";++export const Greenscreen: React.FC = () => {+ const video = useRef<HTMLVideoElement>(null);+ const canvas = useRef<HTMLCanvasElement>(null);+ const { width, height } = useVideoConfig();++ const onVideoFrame = useCallback(() => {+ if (+ !canvas.current ||+ !video.current ||+ !video.current.requestVideoFrameCallback+ ) {+ return;+ }+ const context = canvas.current.getContext("2d");++ if (!context) {+ return;+ }++ context.drawImage(video.current, 0, 0, width, height);+ const imageFrame = context.getImageData(0, 0, width, height);+ const { length } = imageFrame.data;++ for (let i = 0; i < length; i += 4) {+ const red = imageFrame.data[i + 0];+ const green = imageFrame.data[i + 1];+ const blue = imageFrame.data[i + 2];+ if (green > 100 && red < 100 && blue < 100) {+ imageFrame.data[i + 3] = 0;+ }+ }+ context.putImageData(imageFrame, 0, 0);++ video.current.requestVideoFrameCallback(() => onVideoFrame());+ }, [height, width]);++ useEffect(() => {+ if (!video.current || !video.current.requestVideoFrameCallback) {+ return;+ }+ video.current.requestVideoFrameCallback(() => onVideoFrame());+ }, [onVideoFrame]);++ return (+ <AbsoluteFill>+ <AbsoluteFill>+ <Video+ ref={video}+ style={{ opacity: 0 }}+ startFrom={300}+ crossOrigin="anonymous"+ src="https://remotion-assets.s3.eu-central-1.amazonaws.com/just-do-it.mp4"+ />+ </AbsoluteFill>+ <AbsoluteFill>+ <canvas ref={canvas} width={width} height={height} />+ </AbsoluteFill>+ </AbsoluteFill>+ );+};+```diff --git a/packages/docs/docusaurus.config.js b/packages/docs/docusaurus.config.jsindex db644b22bf..385cc3b2e3 100644--- a/packages/docs/docusaurus.config.js+++ b/packages/docs/docusaurus.config.js@@ -162,7 +162,7 @@ module.exports = {vfsRoot: process.cwd(),themes: ["github-light", "github-dark"],defaultCompilerOptions: {- types: ["node"],+ types: ["node", "web"],},},],diff --git a/packages/docs/package.json b/packages/docs/package.jsonindex e791fd9b9d..f7ba38b2a3 100644--- a/packages/docs/package.json+++ b/packages/docs/package.json@@ -48,6 +48,7 @@"@types/jest": "^27.4.0","@types/node": "^17.0.17","@types/react": "17.0.39",+ "@types/web": "^0.0.48","eslint": "^7.25.0","react": "^17.0.1","react-dom": "^17.0.1",diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.jsindex 936867acb9..0ea8d43f9a 100644--- a/packages/docs/sidebars.js+++ b/packages/docs/sidebars.js@@ -52,6 +52,7 @@ module.exports = {"third-party","stills","scaling",+ "video-manipulation",],},"cli",diff --git a/packages/docs/tsconfig.json b/packages/docs/tsconfig.jsonindex 1ab236a5ae..47b4dc8cba 100644--- a/packages/docs/tsconfig.json+++ b/packages/docs/tsconfig.json@@ -2,6 +2,6 @@"extends": "@tsconfig/docusaurus/tsconfig.json","include": ["src/"],"compilerOptions": {- "types": ["node", "jest", "@docusaurus/module-type-aliases"]+ "types": ["node", "jest", "@docusaurus/module-type-aliases", "web"]}}diff --git a/packages/example/src/Video.tsx b/packages/example/src/Video.tsxindex 871d4bae92..d3c956235b 100644--- a/packages/example/src/Video.tsx+++ b/packages/example/src/Video.tsx@@ -14,6 +14,7 @@ import {StaticDemo} from './StaticServer';import {TenFrameTester} from './TenFrameTester';import ThreeBasic from './ThreeBasic';import {VideoOnCanvas} from './VideoOnCanvas';+import {Greenscreen} from './VideoOnCanvas/greenscreen';import {VideoSpeed} from './VideoSpeed';import {VideoTesting} from './VideoTesting';@@ -310,6 +311,14 @@ export const Index: React.FC = () => {fps={30}durationInFrames={100}/>+ <Composition+ id="greenscreen"+ component={Greenscreen}+ width={1920}+ height={1080}+ fps={30}+ durationInFrames={100}+ /><Still id="Orb" component={OrbScene} width={2000} height={2000} /><Stillid="static-demo"diff --git a/packages/example/src/VideoOnCanvas/greenscreen.tsx b/packages/example/src/VideoOnCanvas/greenscreen.tsxnew file mode 100644index 0000000000..963ff8cd36--- /dev/null+++ b/packages/example/src/VideoOnCanvas/greenscreen.tsx@@ -0,0 +1,65 @@+import {useVideoConfig, Video} from 'remotion';+import {useCallback, useEffect, useRef} from 'react';+import React from 'react';+import {AbsoluteFill} from 'remotion';++export const Greenscreen: React.FC = () => {+ const video = useRef<HTMLVideoElement>(null);+ const canvas = useRef<HTMLCanvasElement>(null);+ const {width, height} = useVideoConfig();++ const onVideoFrame = useCallback(() => {+ if (+ !canvas.current ||+ !video.current ||+ !video.current.requestVideoFrameCallback+ ) {+ return;+ }+ const context = canvas.current.getContext('2d');++ if (!context) {+ return;+ }++ context.drawImage(video.current, 0, 0, width, height);+ const imageFrame = context.getImageData(0, 0, width, height);+ const {length} = imageFrame.data;++ for (let i = 0; i < length; i += 4) {+ const red = imageFrame.data[i + 0];+ const green = imageFrame.data[i + 1];+ const blue = imageFrame.data[i + 2];+ if (green > 100 && red < 100 && blue < 100) {+ imageFrame.data[i + 3] = 0;+ }+ }+ context.putImageData(imageFrame, 0, 0);++ video.current.requestVideoFrameCallback(() => onVideoFrame());+ }, [height, width]);++ useEffect(() => {+ if (!video.current || !video.current.requestVideoFrameCallback) {+ return;+ }+ video.current.requestVideoFrameCallback(() => onVideoFrame());+ }, [onVideoFrame]);++ return (+ <AbsoluteFill>+ <AbsoluteFill>+ <Video+ ref={video}+ style={{opacity: 0}}+ startFrom={300}+ crossOrigin="anonymous"+ src="https://remotion-assets.s3.eu-central-1.amazonaws.com/just-do-it.mp4"+ />+ </AbsoluteFill>+ <AbsoluteFill>+ <canvas ref={canvas} width={width} height={height} />+ </AbsoluteFill>+ </AbsoluteFill>+ );+};diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 5158c909a7..043fc8bee6 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -273,6 +273,7 @@ importers:'@types/jest': ^27.4.0'@types/node': ^17.0.17'@types/react': 17.0.39+ '@types/web': ^0.0.48chalk: 4.1.2clipboard: ^2.0.8clsx: ^1.1.1@@ -322,6 +323,7 @@ importers:'@types/jest': 27.4.0'@types/node': 17.0.17'@types/react': 17.0.39+ '@types/web': 0.0.48eslint: 7.32.0react: 17.0.2react-dom: [email protected]@@ -7061,7 +7063,6 @@ packages:/@types/web/0.0.48:resolution: {integrity: sha512-X5KKfR0MNm5ujo7421tVTF6n9MXh58DfWOuvPnuMAlr12vloTtyxJEC0rHVKIvcpqqoOJSrlkW2dMJjl9eK3Dg==}- dev: false/@types/webpack-env/1.16.2:resolution: {integrity: sha512-vKx7WNQNZDyJveYcHAm9ZxhqSGLYwoyLhrHjLBOkw3a7cT76sTdjgtwyijhk1MaHyRIuSztcVwrUOO/NEu68Dw==}