🎥 Make videos programmatically with React
thumbnails
Jonny Burgerfa8ac0d94934
diff --git a/packages/cli/src/editor/components/Preview.tsx b/packages/cli/src/editor/components/Preview.tsxindex 8b59f556f0..b60fc638f8 100644--- a/packages/cli/src/editor/components/Preview.tsx+++ b/packages/cli/src/editor/components/Preview.tsx@@ -1,6 +1,12 @@import React, {Suspense, useContext, useMemo} from 'react';import {Internals, useVideoConfig} from 'remotion';import styled from 'styled-components';+import {+ checkerboardBackgroundColor,+ checkerboardBackgroundImage,+ CHECKERBOARD_BACKGROUND_POS,+ CHECKERBOARD_BACKGROUND_SIZE,+} from '../helpers/checkerboard-background';import {Size} from '../hooks/get-el-size';import {CheckerboardContext} from '../state/checkerboard';import {PreviewSizeContext} from '../state/preview-size';@@ -22,24 +28,16 @@ export const Container = styled.div<{height: ${(props): number => props.height}px;display: flex;position: absolute;- background: ${(props) =>- props.checkerboard- ? `- linear-gradient(- 45deg,- rgba(0, 0, 0, 0.1) 25%,- transparent 25%- ),- linear-gradient(135deg, rgba(0, 0, 0, 0.1) 25%, transparent 25%),- linear-gradient(45deg, transparent 75%, rgba(0, 0, 0, 0.1) 75%),- linear-gradient(135deg, transparent 75%, rgba(0, 0, 0, 0.1) 75%), white;- `- : 'black'};-- background-size: ${checkerboardSize}px ${checkerboardSize}px; /* Must be a square */- background-position: 0 0, ${checkerboardSize / 2}px 0,- ${checkerboardSize / 2}px -${checkerboardSize / 2}px,- 0px ${checkerboardSize / 2}px; /* Must be half of one side of the square */+ background-color: ${(props) =>+ checkerboardBackgroundColor(props.checkerboard)};+ background-image: ${(props) =>+ checkerboardBackgroundImage(props.checkerboard)};+ background-size: ${CHECKERBOARD_BACKGROUND_SIZE(+ checkerboardSize+ )}; /* Must be a square */+ background-position: ${CHECKERBOARD_BACKGROUND_POS(+ checkerboardSize+ )}; /* Must be half of one side of the square */`;const Inner: React.FC<{diff --git a/packages/cli/src/editor/components/Thumbnail.tsx b/packages/cli/src/editor/components/Thumbnail.tsxnew file mode 100644index 0000000000..37b9438f44--- /dev/null+++ b/packages/cli/src/editor/components/Thumbnail.tsx@@ -0,0 +1,89 @@+import React, {Suspense, useContext, useMemo} from 'react';+import {Internals, TComposition, TimelineContextValue} from 'remotion';+import {+ checkerboardBackgroundColor,+ checkerboardBackgroundImage,+ CHECKERBOARD_BACKGROUND_POS,+ CHECKERBOARD_BACKGROUND_SIZE,+} from '../helpers/checkerboard-background';+import {CheckerboardContext} from '../state/checkerboard';++const targetHeight = 60;+const targetWidth = (targetHeight * 16) / 9;++const container: React.CSSProperties = {+ width: targetWidth,+ height: targetHeight,+ backgroundColor: 'rgba(0, 0, 0, 0.4)',+ display: 'flex',+ justifyContent: 'center',+ alignItems: 'center',+};++export const Thumbnail: React.FC<{+ composition: TComposition<unknown>;+}> = ({composition}) => {+ const {height, width, durationInFrames} = composition;+ const heightRatio = targetHeight / height;+ const widthRatio = targetWidth / width;+ const ratio = Math.min(heightRatio, widthRatio);+ const scale = ratio;+ const actualWidth = width * scale;+ const actualHeight = height * scale;+ const correction = 0 - (1 - scale) / 2;+ const xCorrection = correction * width;+ const yCorrection = correction * height;++ const {checkerboard} = useContext(CheckerboardContext);++ const outer: React.CSSProperties = useMemo(() => {+ return {+ width: actualWidth,+ height: actualHeight,+ backgroundColor: checkerboardBackgroundColor(checkerboard),+ backgroundImage: checkerboardBackgroundImage(checkerboard),+ backgroundPosition: CHECKERBOARD_BACKGROUND_POS(25),+ backgroundSize: CHECKERBOARD_BACKGROUND_SIZE(25),+ };+ }, [actualHeight, actualWidth, checkerboard]);++ const inner: React.CSSProperties = useMemo(() => {+ return {+ width,+ height,+ transform: `scale(${scale}) `,+ overflow: 'hidden',+ marginLeft: xCorrection,+ marginTop: yCorrection,+ display: 'flex',+ color: 'black',+ };+ }, [height, scale, width, xCorrection, yCorrection]);++ const timelineState: TimelineContextValue = useMemo(() => {+ return {+ playing: false,+ frame: Math.floor(durationInFrames / 2),+ };+ }, [durationInFrames]);++ const props = useMemo(() => {+ return ((composition.props as unknown) as {}) ?? {};+ }, [composition.props]);++ const ThumbnailComponent = composition.component;++ return (+ <div style={container}>+ <Suspense fallback={null}>+ <div style={outer}>+ <div style={inner}>+ <Internals.Timeline.TimelineContext.Provider value={timelineState}>+ <ThumbnailComponent {...props} />+ </Internals.Timeline.TimelineContext.Provider>+ </div>+ </div>+ </Suspense>+ </div>+ );+};diff --git a/packages/cli/src/editor/helpers/checkerboard-background.ts b/packages/cli/src/editor/helpers/checkerboard-background.tsnew file mode 100644index 0000000000..9e14a9bd84--- /dev/null+++ b/packages/cli/src/editor/helpers/checkerboard-background.ts@@ -0,0 +1,27 @@+export const CHECKERBOARD_BACKGROUND_SIZE = (size: number) =>+ `${size}px ${size}px`;+export const CHECKERBOARD_BACKGROUND_POS = (size: number) =>+ `0 0, ${size / 2}px 0, ${size / 2}px -${size / 2}px, 0px ${size / 2}px`;++export const checkerboardBackgroundColor = (checkerboard: boolean) => {+ if (checkerboard) {+ return 'white';+ }+ return 'black';+};++export const checkerboardBackgroundImage = (checkerboard: boolean) => {+ if (checkerboard) {+ return `+ linear-gradient(+ 45deg,+ rgba(0, 0, 0, 0.1) 25%,+ transparent 25%+ ),+ linear-gradient(135deg, rgba(0, 0, 0, 0.1) 25%, transparent 25%),+ linear-gradient(45deg, transparent 75%, rgba(0, 0, 0, 0.1) 75%),+ linear-gradient(135deg, transparent 75%, rgba(0, 0, 0, 0.1) 75%)+ `;+ }+ return undefined;+};diff --git a/packages/core/src/internals.ts b/packages/core/src/internals.tsindex 6ae839796f..e832297af9 100644--- a/packages/core/src/internals.ts+++ b/packages/core/src/internals.ts@@ -50,6 +50,7 @@ import * as perf from './perf';import {getCompositionName, getIsEvaluation, getRoot} from './register-root';import {RemotionRoot} from './RemotionRoot';import * as Timeline from './timeline-position-state';+import {TimelineContextValue} from './timeline-position-state';import {useUnsafeVideoConfig} from './use-unsafe-video-config';import {useVideo} from './use-video';@@ -99,4 +100,5 @@ export type {TCompMetadata,TSequence,WebpackOverrideFn,+ TimelineContextValue,};