π₯ Make videos programmatically with React
Docs: Recommend `parseMedia()` over `getVideoMetadata()`
Jonny Burger0293056a1f2a
diff --git a/packages/docs/docs/dynamic-metadata.mdx b/packages/docs/docs/dynamic-metadata.mdxindex a403dab0d3..eb7950f33b 100644--- a/packages/docs/docs/dynamic-metadata.mdx+++ b/packages/docs/docs/dynamic-metadata.mdx@@ -16,7 +16,7 @@ Consider a scenario where a video is dynamically specified as a background and tPass a [`calculateMetadata`](/docs/composition#calculatemetadata) callback function to a [`<Composition>`](/docs/composition). This function should take the [combined props](/docs/props-resolution) and calculate the metadata.```tsx twoslash title="src/Root.tsx"-import {getVideoMetadata} from '@remotion/media-utils';+import {parseMedia} from '@remotion/media-parser';import {Composition, Video} from 'remotion';type MyCompProps = {@@ -40,10 +40,16 @@ export const Root: React.FC = () => {src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',}}calculateMetadata={async ({props}) => {- const data = await getVideoMetadata(props.src);+ const {durationInSeconds} = await parseMedia({+ src: props.src,+ fields: {durationInSeconds: true},+ });+ if (durationInSeconds === null) {+ throw new Error('Could not read duration of video');+ }return {- durationInFrames: Math.floor(data.durationInSeconds * 30),+ durationInFrames: Math.floor(durationInSeconds * 30),};}}/>@@ -60,7 +66,7 @@ It is also possible to transform the props [passed to the component by returning## With `useEffect()` and `getInputProps()`-In the following example, Remotion is instructed to wait for the [`getVideoMetadata()`](/docs/get-video-metadata) promise to resolve before evaluating the composition.+In the following example, Remotion is instructed to wait for the [`parseMedia()`](/docs/media-parser/parse-media) promise to resolve before evaluating the composition.By calling [`delayRender()`](/docs/delay-render), Remotion will be blocked from proceeding until [`continueRender()`](/docs/continue-render) is called.@@ -69,17 +75,24 @@ import {useEffect, useState} from 'react';import {Composition, continueRender, delayRender} from 'remotion';const VideoTesting: React.FC = () => null;// ---cut----import {getVideoMetadata} from '@remotion/media-utils';+import {parseMedia} from '@remotion/media-parser';export const Index: React.FC = () => {const [handle] = useState(() => delayRender());const [duration, setDuration] = useState(1);useEffect(() => {- getVideoMetadata(- 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',- )+ parseMedia({+ src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',+ fields: {+ durationInSeconds: true,+ },+ }).then(({durationInSeconds}) => {+ if (durationInSeconds === null) {+ throw new Error('Could not read duration of video');+ }+setDuration(Math.round(durationInSeconds * 30));continueRender(handle);})@@ -139,7 +152,7 @@ The [`<Player>`](/docs/player) will react if the metadata being passed to it cha```tsx twoslash title="MyApp.tsx"const VideoTesting: React.FC = () => null;// ---cut----import {getVideoMetadata} from '@remotion/media-utils';+import {parseMedia} from '@remotion/media-parser';import {useEffect, useState} from 'react';import {Player} from '@remotion/player';@@ -147,10 +160,17 @@ export const Index: React.FC = () => {const [duration, setDuration] = useState(1);useEffect(() => {- getVideoMetadata(- 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',- )+ parseMedia({+ src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',+ fields: {+ durationInSeconds: true,+ },+ }).then(({durationInSeconds}) => {+ if (durationInSeconds === null) {+ throw new Error('Could not read duration of video');+ }+setDuration(Math.round(durationInSeconds * 30));}).catch((err) => {diff --git a/packages/docs/docs/miscellaneous/parse-media-vs-get-video-metadata.mdx b/packages/docs/docs/miscellaneous/parse-media-vs-get-video-metadata.mdxnew file mode 100644index 0000000000..ba0fddda02--- /dev/null+++ b/packages/docs/docs/miscellaneous/parse-media-vs-get-video-metadata.mdx@@ -0,0 +1,59 @@+---+title: parseMedia() vs. getVideoMetadata()+crumb: 'FAQ'+sidebar_label: parseMedia() vs. getVideoMetadata()+---++There are multiple ways to retrieve information about a media such as:++- Duration+- Width and height+- Codec, container, framerate++## Recommendation: `parseMedia()`++[`parseMedia()`](/docs/media-parser/parse-media) is a library developed by Remotion that parses video files using JavaScript.++### Advantages++β It supports more containers than [`getVideoMetadata()`](/docs/get-video-metadata): MP4, MOV, WebM, AVI, MKV+β It supports getting get dimensions of videos shot on iPhones, a common pitfall when using [`getVideoMetadata()`](/docs/get-video-metadata) on Linux+β It also works on the server in Node.js and Bun.+β It only fetches the information you request, needing to read less bytes to get the information you need.++### Disadvantages++β The API is still unstable, although the signature for simple use cases should not change.+β When using in the browser: Assets need to come from the same origin or be CORS-enabled.++## `getVideoMetadata()` from `@remotion/media-utils`++[`getVideoMetadata()`](/docs/get-video-metadata) will mount a `<video>` tag in the browser and returns the metadata after it is available.++### Advantages++β In the browser, it does not require the assets to be CORS-enabled or be on the same origin.++### Disadvantages++β It only works in the browser.+β It does not support some codecs, such as the ones used by iPhone videos.++## `getVideoMetadata()` from `@remotion/renderer`++[`getVideoMetadata()`](/docs/renderer/get-video-metadata) will read metadata using FFmpeg through a Rust interface.++### Advantages++β The widest format compatibility.++### Disadvantages++β It only works in Node.js and Bun, so you cannot use it in [`calculateMetadata()`](/docs/calculate-metadata).+β It invokes a Rust binary, so bundling it is not straightforward.++## See also++- [`parseMedia()`](/docs/media-parser/parse-media)+- [`@remotion/media-utils`: `getVideoMetadata()`](/docs/get-video-metadata)+- [`@remotion/renderer`: `getVideoMetadata()`](/docs/renderer/get-video-metadata)diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.jsindex 9bf96bfe48..edd08ecfba 100644--- a/packages/docs/sidebars.js+++ b/packages/docs/sidebars.js@@ -1112,6 +1112,7 @@ module.exports = {'miscellaneous/embed-studio','miscellaneous/absolute-paths','miscellaneous/live-streaming',+ 'parse-media-vs-get-video-metadata','compare/motion-canvas',],},diff --git a/packages/studio/package.json b/packages/studio/package.jsonindex 1a77c1c960..35ae4e41ef 100644--- a/packages/studio/package.json+++ b/packages/studio/package.json@@ -24,6 +24,7 @@"remotion": "workspace:*","@remotion/player": "workspace:*","@remotion/media-utils": "workspace:*",+ "@remotion/media-parser": "workspace:*","@remotion/renderer": "workspace:*","@remotion/studio-shared": "workspace:*","memfs": "3.4.3",diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex a10d654a2e..f25e57ac26 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -1822,6 +1822,9 @@ importers:packages/studio:dependencies:+ '@remotion/media-parser':+ specifier: workspace:*+ version: link:../media-parser'@remotion/media-utils':specifier: workspace:*version: link:../media-utils