Skip to content

🎥 Make videos programmatically with React

Docs: How to make a composition the same length as a video

Jonny Burger751da72df5de
diff --git a/packages/docs/docs/dynamic-metadata.md b/packages/docs/docs/dynamic-metadata.md
index bce9812288..0c70201637 100644
--- a/packages/docs/docs/dynamic-metadata.md
+++ b/packages/docs/docs/dynamic-metadata.md
@@ -238,3 +238,4 @@ export const Index: React.FC = () => {
- [How props get resolved](/docs/props-resolution)
- [Data fetching](/docs/data-fetching)
+- [How do I make the composition the same duration as my video?](/docs/miscellaneous/snippets/align-duration)
diff --git a/packages/docs/docs/miscellaneous/snippets/align-duration.mdx b/packages/docs/docs/miscellaneous/snippets/align-duration.mdx
new file mode 100644
index 0000000000..1f5ef259f2
--- /dev/null
+++ b/packages/docs/docs/miscellaneous/snippets/align-duration.mdx
@@ -0,0 +1,117 @@
+---
+title: How do I make the composition the same duration as my video?
+sidebar_label: Same composition duration as video
+crumb: "FAQ"
+---
+
+If you have a component rendering a video:
+
+```tsx twoslash title="MyComp.tsx"
+import React from "react";
+import { OffthreadVideo, staticFile } from "remotion";
+
+export const MyComp: React.FC = () => {
+ return <OffthreadVideo src={staticFile("video.mp4")} />;
+};
+```
+
+and you want to make the composition the same duration as the video, first make the video source a React prop:
+
+```tsx twoslash title="MyComp.tsx"
+import React from "react";
+import { OffthreadVideo, staticFile } from "remotion";
+
+type MyCompProps = {
+ src: string;
+};
+
+export const MyComp: React.FC<MyCompProps> = ({ src }) => {
+ return <OffthreadVideo src={src} />;
+};
+```
+
+Then, define a [`calculateMetadata()`](/docs/calculate-metadata) function that calculates the duration of the composition based on the video.
+[Install `@remotion/media-utils`](/docs/media-utils) if necessary.
+
+```tsx twoslash title="MyComp.tsx"
+type MyCompProps = {
+ src: string;
+};
+
+// ---cut---
+
+import { CalculateMetadataFunction } from "remotion";
+import { getVideoMetadata } from "@remotion/media-utils";
+
+export const calculateMetadata: CalculateMetadataFunction<
+ MyCompProps
+> = async ({ props }) => {
+ const data = await getVideoMetadata(props.src);
+ const fps = 30;
+
+ return {
+ durationInFrames: Math.floor(data.durationInSeconds * fps),
+ fps,
+ };
+};
+```
+
+Finally, pass the `calculateMetadata` function to the `Composition` component and define the previously hardcoded `src` as a default prop:
+
+```tsx twoslash title="Root.tsx"
+// @filename: MyComp.tsx
+import React from "react";
+import { CalculateMetadataFunction } from "remotion";
+import { getVideoMetadata } from "@remotion/media-utils";
+
+export const MyComp: React.FC<MyCompProps> = () => {
+ return null;
+};
+type MyCompProps = {
+ src: string;
+};
+
+export const calculateMetadata: CalculateMetadataFunction<
+ MyCompProps
+> = async ({ props }) => {
+ const data = await getVideoMetadata(props.src);
+ const fps = 30;
+
+ return {
+ durationInFrames: Math.floor(data.durationInSeconds * fps),
+ fps,
+ };
+};
+
+// @filename: Root.tsx
+// ---cut---
+
+import React from "react";
+import { Composition } from "remotion";
+import { MyComp, calculateMetadata } from "./MyComp";
+
+export const Root: React.FC = () => {
+ return (
+ <Composition
+ id="MyComp"
+ component={MyComp}
+ durationInFrames={300}
+ fps={30}
+ width={1920}
+ height={1080}
+ defaultProps={{
+ src: "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4",
+ }}
+ calculateMetadata={calculateMetadata}
+ />
+ );
+};
+```
+
+## How do I make the composition the same duration as my audio?
+
+Follow the same steps, but instead if [`getVideoMetadata()`](/docs/get-video-metadata), use [`getAudioDurationInSeconds()`](/docs/get-audio-duration-in-seconds) from `@remotion/media-utils` to calculate the duration of the composition based on the audio file.
+
+## See Also
+
+- [Variable duration](/docs/dynamic-metadata)
diff --git a/packages/docs/docs/miscellaneous/snippets/combine-compositions.mdx b/packages/docs/docs/miscellaneous/snippets/combine-compositions.mdx
index 86ecc7a5c1..03be63a4b1 100644
--- a/packages/docs/docs/miscellaneous/snippets/combine-compositions.mdx
+++ b/packages/docs/docs/miscellaneous/snippets/combine-compositions.mdx
@@ -171,3 +171,9 @@ As more scenes are added, consider:
- using [`calculateMetadata()`](/docs/calculate-metadata) to calculate the duration of a composition based on its props.
- creating an array of scenes and using JavaScripts [`.map()`](https://www.freecodecamp.org/news/how-to-render-lists-in-react/) function to render them.
+
+## See also
+
+- [`<Series>` component](/docs/series)
+- [`<TransitionSeries>` component](/docs/transitions/transitionseries)
+- [Making components reusable](/docs/reusability)
diff --git a/packages/docs/docs/sequences.md b/packages/docs/docs/sequences.md
index 5bff926bb9..1b4119ba3b 100644
--- a/packages/docs/docs/sequences.md
+++ b/packages/docs/docs/sequences.md
@@ -79,3 +79,4 @@ Sequences by default are absolutely positioned - you can use [`layout="none"`](/
## See also
- [`<Sequence>` component](/docs/sequence)
+- [How do I combine compositions?](/docs/miscellaneous/snippets/combine-compositions)
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index 760fd55953..f50f284728 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -800,6 +800,7 @@ module.exports = {
"miscellaneous/snippets/offthread-video-while-rendering",
"miscellaneous/snippets/hls",
"miscellaneous/snippets/combine-compositions",
+ "miscellaneous/snippets/align-duration",
],
},