Skip to content

🎥 Make videos programmatically with React

fixup style and implement documentation

Antoine Caron481cd329486a
diff --git a/packages/docs/docs/player/thumbnail.md b/packages/docs/docs/player/thumbnail.md
index d8e714a2e8..6fc6dac802 100644
--- a/packages/docs/docs/player/thumbnail.md
+++ b/packages/docs/docs/player/thumbnail.md
@@ -9,7 +9,7 @@ This component share React props definition of the [`<Player>`](/player/api) com
```tsx twoslash title="MyApp.tsx"
// @allowUmdGlobalAccess
// @filename: ./remotion/MyVideo.tsx
-export const MyVideo = () => <></>;
+export const MyVideo = ({ title }) => <>{ title }</>;
// @filename: index.tsx
// ---cut---
@@ -18,17 +18,67 @@ import { MyVideo } from "./remotion/MyVideo";
export const App: React.FC = () => {
return (
- <Thumbnail
- component={MyVideo}
- durationInFrames={120}
- compositionWidth={1920}
- compositionHeight={1080}
- fps={30}
- />
+ <Thumbnail
+ inputProps={{
+ title: 'Foo'
+ }}
+ component={MyVideo}
+ compositionWidth={600}
+ compositionHeight={600}
+ frameToDisplay={30}
+ durationInFrames={120} f
+ ps={30}
+ />
);
};
```
+## API
+
+### `component` or `lazyComponent`
+
+Pass a React component in directly **or** pass a function that returns a dynamic import. Passing neither or both of the props is an error.
+
+If you use `lazyComponent`, wrap it in a `useCallback()` to avoid constant rendering. [See here for an example.](/docs/player/examples#loading-a-component-lazily)
+
+:::note
+Thumbnail does not use [`<Composition>`](/docs/composition)'s. Pass your component directly and do not wrap it in a `<Composition>` component.
+:::
+
+### `frameToDisplay`
+
+Index of the frame rendered by the composition in the Thumbnail.
+
+
+### `compositionWidth`
+
+The width you would like the video to have when rendered. Use `style={{width: <width>}}` to define a different natural width for the Thumbnail.
+
+### `compositionHeight`
+
+The height of the canvas in pixels.
+The height you would like the video to have when rendered. Use `style={{height: <height>}}` to define a different natural height for the Thumbnail.
+
+### `inputProps`
+
+_optional_
+
+Pass props to the component that you have specified using the `component` prop. The Typescript definition takes the shape of the props that you have given to your `component`. Default `undefined`.
+
+### `style`
+
+_optional_
+
+A regular `style` prop for a HTMLDivElement. You can pass a different height and width if you would like different dimensions for the player than the original composition dimensions.
+
+### `durationInFrames`
+
+The duration of the video in frames. Must be an integer and greater than 0.
+
+### `fps`
+
+The frame rate of the video. Must be a number.
+
diff --git a/packages/player-example/src/ThumbnailDemo.tsx b/packages/player-example/src/ThumbnailDemo.tsx
index 291edf9fdd..4775c6d66f 100644
--- a/packages/player-example/src/ThumbnailDemo.tsx
+++ b/packages/player-example/src/ThumbnailDemo.tsx
@@ -5,9 +5,7 @@ import {Thumbnail} from "@remotion/player";
export const ThumbnailDemo: React.FC = () => {
return <div style={{margin: '2rem'}}>
- <Thumbnail inputProps={{
-
- }} targetHeight={500} targetWidth={500} component={VideoautoplayDemo} compositionWidth={500}
+ <Thumbnail inputProps={{}} component={VideoautoplayDemo} compositionWidth={500}
compositionHeight={432} frameToDisplay={1} durationInFrames={2700} fps={30}/>
</div>
}
diff --git a/packages/player/src/Thumbnail.tsx b/packages/player/src/Thumbnail.tsx
index 46fb2aa477..59c2c7b043 100644
--- a/packages/player/src/Thumbnail.tsx
+++ b/packages/player/src/Thumbnail.tsx
@@ -1,134 +1,133 @@
import type {ComponentType, CSSProperties, LazyExoticComponent} from 'react';
import {Suspense, useMemo, useState} from 'react';
import type {
- CompositionManagerContext,
- CompProps,
- TimelineContextValue,
+ CompositionManagerContext,
+ CompProps,
+ TimelineContextValue,
} from 'remotion';
import {Internals, random} from 'remotion';
import type {PropsIfHasProps} from './utils/props-if-has-props';
type ThumbnailProps<T> = PropsIfHasProps<T> &
- CompProps<T> & {
- targetHeight: number;
- targetWidth: number;
- frameToDisplay: number;
- style?: CSSProperties;
- durationInFrames: number;
- compositionWidth: number;
- compositionHeight: number;
- inputProps: number;
- fps: number;
- };
+ CompProps<T> & {
+ frameToDisplay: number;
+ style?: CSSProperties;
+ durationInFrames: number;
+ compositionWidth: number;
+ compositionHeight: number;
+ inputProps?: unknown;
+ fps: number;
+};
-export const Thumbnail = <T,>({
- targetWidth,
- targetHeight,
- frameToDisplay,
- style,
- inputProps,
- compositionHeight,
- compositionWidth,
- durationInFrames,
- fps,
- ...componentProps
-}: ThumbnailProps<T>) => {
- const [thumbnailId] = useState(() => String(random(null)));
+export const Thumbnail = <T, >({
+ frameToDisplay,
+ style,
+ inputProps,
+ compositionHeight,
+ compositionWidth,
+ durationInFrames,
+ fps,
+ ...componentProps
+ }: ThumbnailProps<T>) => {
+ const [thumbnailId] = useState(() => String(random(null)));
- const container: CSSProperties = useMemo(() => {
- return {
- width: targetWidth,
- height: targetHeight,
- backgroundColor: 'rgba(0, 0, 0, 0.4)',
- display: 'flex',
- justifyContent: 'center',
- alignItems: 'center',
- ...style,
- };
- }, [targetHeight, targetWidth, style]);
+ const container: CSSProperties = useMemo(() => {
+ return {
+ width: compositionWidth,
+ height: compositionHeight,
+ backgroundColor: 'rgba(0, 0, 0, 0.4)',
+ display: 'flex',
+ justifyContent: 'center',
+ alignItems: 'center',
+ position: 'relative',
+ ...style,
+ };
+ }, [compositionWidth, compositionHeight, style]);
- const timelineState: TimelineContextValue = useMemo(() => {
- return {
- playing: false,
- frame: frameToDisplay,
- rootId: thumbnailId,
- imperativePlaying: {
- current: false,
- },
- playbackRate: 1,
- setPlaybackRate: () => {
- throw new Error('thumbnail');
- },
- audioAndVideoTags: {current: []},
- };
- }, [frameToDisplay, thumbnailId]);
+ const timelineState: TimelineContextValue = useMemo(() => {
+ return {
+ playing: false,
+ frame: frameToDisplay,
+ rootId: thumbnailId,
+ imperativePlaying: {
+ current: false,
+ },
+ playbackRate: 1,
+ setPlaybackRate: () => {
+ throw new Error('thumbnail');
+ },
+ audioAndVideoTags: {current: []},
+ };
+ }, [frameToDisplay, thumbnailId]);
- const props = useMemo(() => {
- return (inputProps ?? {}) as unknown as {};
- }, [inputProps]);
+ const props = useMemo(() => {
+ return (inputProps ?? {}) as unknown as {};
+ }, [inputProps]);
- const Component = Internals.useLazyComponent(
- componentProps
- ) as LazyExoticComponent<ComponentType<unknown>>;
+ const Component = Internals.useLazyComponent(
+ componentProps
+ ) as LazyExoticComponent<ComponentType<unknown>>;
- const compositionManagerContext: CompositionManagerContext = useMemo(() => {
- return {
- compositions: [
- {
- component: Component as React.LazyExoticComponent<
- ComponentType<unknown>
- >,
- durationInFrames,
- height: compositionHeight,
- width: compositionWidth,
- fps,
- id: 'player-comp',
- props: inputProps as unknown,
- nonce: 777,
- scale: 1,
- folderName: null,
- defaultProps: undefined,
- parentFolderName: null,
- },
- ],
- folders: [],
- registerFolder: () => undefined,
- unregisterFolder: () => undefined,
- currentComposition: 'player-comp',
- registerComposition: () => undefined,
- registerSequence: () => undefined,
- sequences: [],
- setCurrentComposition: () => undefined,
- unregisterComposition: () => undefined,
- unregisterSequence: () => undefined,
- registerAsset: () => undefined,
- unregisterAsset: () => undefined,
- currentCompositionMetadata: null,
- setCurrentCompositionMetadata: () => undefined,
- assets: [],
- };
- }, [
- Component,
- durationInFrames,
- compositionHeight,
- compositionWidth,
- fps,
- inputProps,
- ]);
+ const compositionManagerContext: CompositionManagerContext = useMemo(() => {
+ return {
+ compositions: [
+ {
+ component: Component as React.LazyExoticComponent<ComponentType<unknown>>,
+ durationInFrames,
+ height: compositionHeight,
+ width: compositionWidth,
+ fps,
+ id: 'player-comp',
+ props: inputProps as unknown,
+ nonce: 777,
+ scale: 1,
+ folderName: null,
+ defaultProps: undefined,
+ parentFolderName: null,
+ },
+ ],
+ folders: [],
+ registerFolder: () => undefined,
+ unregisterFolder: () => undefined,
+ currentComposition: 'player-comp',
+ registerComposition: () => undefined,
+ registerSequence: () => undefined,
+ sequences: [],
+ setCurrentComposition: () => undefined,
+ unregisterComposition: () => undefined,
+ unregisterSequence: () => undefined,
+ registerAsset: () => undefined,
+ unregisterAsset: () => undefined,
+ currentCompositionMetadata: null,
+ setCurrentCompositionMetadata: () => undefined,
+ assets: [],
+ };
+ }, [
+ Component,
+ durationInFrames,
+ compositionHeight,
+ compositionWidth,
+ fps,
+ inputProps,
+ ]);
- return (
- <Internals.CanUseRemotionHooksProvider>
- <div style={container}>
- <Suspense fallback={null}>
- <Internals.Timeline.TimelineContext.Provider value={timelineState}>
- <Internals.CompositionManager.Provider
- value={compositionManagerContext}
- >
- <Component {...props} />
- </Internals.CompositionManager.Provider>
- </Internals.Timeline.TimelineContext.Provider>
- </Suspense>
- </div>
- </Internals.CanUseRemotionHooksProvider>
- );
+ return (
+ <Internals.CanUseRemotionHooksProvider>
+ <Internals.Timeline.TimelineContext.Provider value={timelineState}>
+ <Internals.CompositionManager.Provider
+ value={compositionManagerContext}
+ >
+ <Internals.PrefetchProvider>
+ <Internals.DurationsContextProvider>
+ <div style={container}>
+ <Suspense fallback={null}>
+ <Component {...props} />
+ </Suspense>
+ </div>
+ </Internals.DurationsContextProvider>
+ </Internals.PrefetchProvider>
+ </Internals.CompositionManager.Provider>
+ </Internals.Timeline.TimelineContext.Provider>
+ </Internals.CanUseRemotionHooksProvider>
+ );
};