Skip to content

🎥 Make videos programmatically with React

Docs: How to support multiple frame rates

Jonny Burger5cf7491737d3
diff --git a/.prettierignore b/.prettierignore
deleted file mode 100644
index ef69b7871f..0000000000
--- a/.prettierignore
+++ /dev/null
@@ -1,4 +0,0 @@
-# Prettier for docs is temporarily disabled:
-# https://github.com/prettier/prettier/issues/12209
-packages/docs/docs/**.mdx
-packages/docs/docs/**/*.mdx
diff --git a/packages/docs/docs/miscellaneous/snippets/fps-converter.mdx b/packages/docs/docs/miscellaneous/snippets/fps-converter.mdx
deleted file mode 100644
index fc373eba61..0000000000
--- a/packages/docs/docs/miscellaneous/snippets/fps-converter.mdx
+++ /dev/null
@@ -1,88 +0,0 @@
----
-image: /generated/articles-docs-miscellaneous-snippets-fps-converter.png
-title: "FPS converter"
-crumb: "Snippets"
----
-
-This snippet is useful if you have designed your video with a different frame rate than you want to render in the end. Wrap your markup in the `<FpsConverter>` component override the time of it's children to achieve a different FPS.
-
-```tsx twoslash title="FpsConverter.tsx"
-import React, { useContext, useMemo } from "react";
-import { Internals, TimelineContextValue, useVideoConfig } from "remotion";
-
-export const FpsConverter: React.FC<{
- originalFps: number;
- newFps: number;
- children: React.ReactNode;
-}> = ({ originalFps, newFps, children }) => {
- const context = useContext(Internals.Timeline.TimelineContext);
- const ratio = originalFps / newFps;
- const { id } = useVideoConfig();
-
- const value: TimelineContextValue = useMemo(() => {
- return {
- ...context,
- // Remotion 4.0
- frame: { [id]: (context.frame[id] ?? 0) * ratio },
- // Remotion 3.0
- // frame: context.frame * ratio,
- };
- }, [context, ratio]);
-
- return (
- <Internals.Timeline.TimelineContext.Provider value={value}>
- {children}
- </Internals.Timeline.TimelineContext.Provider>
- );
-};
-```
-
-```tsx twoslash title="Usage"
-// @filename: "MyComp.tsx"
-export const MyComp = () => {
- return null;
-};
-
-// @filename: "FpsConverter.tsx"
-import React, { useContext, useMemo } from "react";
-import { Internals, TimelineContextValue, useVideoConfig } from "remotion";
-
-export const FpsConverter: React.FC<{
- originalFps: number;
- newFps: number;
- children: React.ReactNode;
-}> = ({ originalFps, newFps, children }) => {
- const context = useContext(Internals.Timeline.TimelineContext);
- const ratio = originalFps / newFps;
- const { id } = useVideoConfig();
-
- const value: TimelineContextValue = useMemo(() => {
- return {
- ...context,
- // Remotion 4.0
- frame: { [id]: (context.frame[id] ?? 0) * ratio },
- // Remotion 3.0
- // frame: context.frame * ratio,
- };
- }, [context, ratio]);
-
- return (
- <Internals.Timeline.TimelineContext.Provider value={value}>
- {children}
- </Internals.Timeline.TimelineContext.Provider>
- );
-};
-
-// @filename: "index.tsx"
-import { FpsConverter } from "./FpsConverter";
-import { MyComp } from "./MyComp";
-// ---cut---
-
-export const Converted: React.FC = () => {
- return (
- <FpsConverter newFps={29.97} originalFps={60}>
- <MyComp></MyComp>
- </FpsConverter>
- );
-};
-```
diff --git a/packages/docs/docs/multiple-fps.mdx b/packages/docs/docs/multiple-fps.mdx
new file mode 100644
index 0000000000..15a4337b65
--- /dev/null
+++ b/packages/docs/docs/multiple-fps.mdx
@@ -0,0 +1,114 @@
+---
+image: /generated/articles-docs-multiple-fps.png
+title: Supporting multiple frame rates
+crumb: Building apps
+---
+
+You may want to support multiple frame rates for a composition.
+For example, to build an option allow the user to export the video at either 30 FPS or 60 FPS.
+This document outlines the best practices for doing so.
+
+## Write animations frame-rate-independently
+
+When animating over time, use the `fps` value from [`useVideoConfig()`](/docs/use-video-config) to calculate the current time.
+
+The following animation will change the speed if the frame rate is changed:
+
+```ts twoslash
+import {interpolate, useCurrentFrame} from 'remotion';
+const frame = useCurrentFrame();
+// ---cut---
+// Animate from second 1 to second 2
+const animationProgress = interpolate(frame, [30, 60], [0, 1], {
+ extrapolateLeft: 'clamp',
+ extrapolateRight: 'clamp',
+});
+```
+
+It is better to make it frame-rate-dependnt:
+
+```ts twoslash
+import {useVideoConfig, interpolate, useCurrentFrame} from 'remotion';
+const frame = useCurrentFrame();
+// ---cut---
+// Animate from second 1 to second 2
+const {fps} = useVideoConfig();
+const animationProgress = interpolate(frame, [1 * fps, 2 * fps], [0, 1], {
+ extrapolateLeft: 'clamp',
+ extrapolateRight: 'clamp',
+});
+```
+
+Also use the `fps` value when specifying `from` and `durationInFrames` of a `<Sequence>`:
+
+```tsx twoslash
+import React from 'react';
+import {interpolate, useCurrentFrame, Sequence, useVideoConfig} from 'remotion';
+const frame = useCurrentFrame();
+const {fps, durationInFrames} = useVideoConfig();
+// ---cut---
+// Show for 3 seconds
+<Sequence durationInFrames={3 * fps}>
+ <div />
+</Sequence>;
+```
+
+And when passing `fps`, `delay` and `durationInFrames` of a `spring()`:
+
+```ts twoslash
+import {spring, useCurrentFrame, useVideoConfig} from 'remotion';
+const frame = useCurrentFrame();
+const {fps} = useVideoConfig();
+// ---cut---
+const animationProgress = spring({
+ frame,
+ fps,
+ durationInFrames: 2 * fps,
+ delay: 1 * fps,
+});
+```
+
+## Change frame rate dynamically
+
+Here is how you could switch the `fps` of a [`<Composition>`](/docs/composition) based on an [input prop](/docs/terminology/input-props):
+
+```tsx twoslash
+import {Composition, useCurrentFrame, useVideoConfig} from 'remotion';
+
+const VariableFps: React.FC<{
+ frameRate: '30fps' | '60fps';
+}> = () => {
+ const {fps} = useVideoConfig();
+ return <div>{fps} FPS</div>;
+};
+
+export const Root: React.FC = () => {
+ return (
+ <Composition
+ id="VariableFps"
+ component={VariableFps}
+ width={1080}
+ height={1080}
+ durationInFrames={100}
+ calculateMetadata={({props}) => {
+ return {
+ fps: props.frameRate === '60fps' ? 60 : 30,
+ };
+ }}
+ defaultProps={{
+ frameRate: '30fps',
+ }}
+ />
+ );
+};
+```
+
+## FPS Convert is discouraged
+
+Previously, this page showed a FPS converter snippet.
+It's usage is not recommended because it does not work with media tags (`<Video>`, `<Audio>`, `<OffthreadVideo>`, etc.).
+
+## See also
+
+- [Dynamic FPS, duration and dimensions](/docs/dynamic-metadata)
+- [`<Composition>`](/docs/composition)
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index b72495f222..e15db5317e 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -814,6 +814,7 @@ module.exports = {
'video-uploads',
'presigned-urls',
'font-picker',
+ 'multiple-fps',
],
},
{
@@ -871,7 +872,6 @@ module.exports = {
items: [
'miscellaneous/snippets/accelerated-video',
'miscellaneous/snippets/player-in-iframe',
- 'miscellaneous/snippets/fps-converter',
'miscellaneous/snippets/use-delay-render',
'miscellaneous/snippets/offthread-video-while-rendering',
'miscellaneous/snippets/hls',
diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.ts
index 920b42bbbf..116c82dc53 100644
--- a/packages/docs/src/data/articles.ts
+++ b/packages/docs/src/data/articles.ts
@@ -1875,13 +1875,6 @@ export const articles = [
compId: 'articles-docs-miscellaneous-snippets-combine-compositions',
crumb: 'FAQ',
},
- {
- id: 'miscellaneous/snippets/fps-converter',
- title: 'FPS converter',
- relativePath: 'docs/miscellaneous/snippets/fps-converter.mdx',
- compId: 'articles-docs-miscellaneous-snippets-fps-converter',
- crumb: 'Snippets',
- },
{
id: 'hls',
title: 'HLS support (HTTP Live Streaming)',
@@ -1968,6 +1961,13 @@ export const articles = [
compId: 'articles-docs-motion-blur-trail',
crumb: "'Motion blur'",
},
+ {
+ id: 'multiple-fps',
+ title: 'Supporting multiple frame rates',
+ relativePath: 'docs/multiple-fps.mdx',
+ compId: 'articles-docs-multiple-fps',
+ crumb: 'Building apps',
+ },
{
id: 'noise/index',
title: '@remotion/noise',
diff --git a/packages/docs/static/generated/articles-docs-miscellaneous-snippets-fps-converter.png b/packages/docs/static/generated/articles-docs-miscellaneous-snippets-fps-converter.png
deleted file mode 100644
index 8b8f5e93a8..0000000000
Binary files a/packages/docs/static/generated/articles-docs-miscellaneous-snippets-fps-converter.png and /dev/null differ
diff --git a/vercel.json b/vercel.json
index 0f81302686..43d43ed2b5 100644
--- a/vercel.json
+++ b/vercel.json
@@ -346,6 +346,11 @@
"source": "/leads",
"destination": "https://github.com/orgs/remotion-dev/projects/15/views/1",
"permanent": false
+ },
+ {
+ "source": "/docs/snippets/fps-converter",
+ "destination": "/docs/multiple-fps",
+ "permanent": false
}
]
}