🎥 Make videos programmatically with React
replace stuff in docs
JonnyBurger3bba708e060a
diff --git a/packages/docs/docs/dynamic-metadata.mdx b/packages/docs/docs/dynamic-metadata.mdxindex 6586b31468..613337f542 100644--- a/packages/docs/docs/dynamic-metadata.mdx+++ b/packages/docs/docs/dynamic-metadata.mdx@@ -84,6 +84,10 @@ export const Root: React.FC = () => {};```+:::note+See [Getting metadata using Mediabunny](/docs/mediabunny/metadata) for the snippet for `get-media-metadata.ts`.+:::+The `props` defaults to the `defaultProps` specified, but may be overriden by [passing input props to the render](/docs/props-resolution).Return an object with `durationInFrames` to change the duration of the video.@@ -93,30 +97,57 @@ 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 [`parseMedia()`](/docs/media-parser/parse-media) promise to resolve before evaluating the composition.+In the following example, Remotion is instructed to wait for the [`getMediaMetadata()`](/docs/mediabunny/metadata) 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.```tsx twoslash title="src/Root.tsx"+// @filename: get-media-metadata.ts+import {Input, ALL_FORMATS, UrlSource} from 'mediabunny';++export const getMediaMetadata = async (src: string) => {+ const input = new Input({+ formats: ALL_FORMATS,+ source: new UrlSource(src, {+ getRetryDelay: () => null,+ }),+ });++ const durationInSeconds = await input.computeDuration();+ const videoTrack = await input.getPrimaryVideoTrack();+ const dimensions = videoTrack+ ? {+ width: videoTrack.displayWidth,+ height: videoTrack.displayHeight,+ }+ : null;+ const packetStats = await videoTrack?.computePacketStats();+ const fps = packetStats?.averagePacketRate ?? null;++ return {+ durationInSeconds,+ dimensions,+ fps,+ };+};++// @filename: index.tsx+// ---cut---import {useEffect, useState} from 'react';import {Composition, continueRender, delayRender} from 'remotion';+import React from 'react';const VideoTesting: React.FC = () => null;// ---cut----import {parseMedia} from '@remotion/media-parser';+import {getMediaMetadata} from './get-media-metadata';export const Index: React.FC = () => {const [handle] = useState(() => delayRender());const [duration, setDuration] = useState(1);useEffect(() => {- parseMedia({- src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',- fields: {- slowDurationInSeconds: true,- },- })- .then(({slowDurationInSeconds}) => {- setDuration(Math.round(slowDurationInSeconds * 30));+ getMediaMetadata('https://remotion.media/video.mp4')+ .then(({durationInSeconds}) => {+ setDuration(Math.round(durationInSeconds * 30));continueRender(handle);}).catch((err) => {@@ -128,6 +159,10 @@ export const Index: React.FC = () => {};```+:::note+See [Getting metadata using Mediabunny](/docs/mediabunny/metadata) for the snippet for `get-media-metadata.ts`.+:::+To dynamically pass a video asset, you [may pass input props](/docs/props-resolution) when rendering and retrieve them inside your React code using [`getInputProps()`](/docs/get-input-props).```tsx twoslash title="src/Root.tsx"@@ -164,24 +199,50 @@ The [`<Player>`](/docs/player) will react if the metadata being passed to it cha</p>```tsx twoslash title="MyApp.tsx"+// @filename: get-media-metadata.ts+import {Input, ALL_FORMATS, UrlSource} from 'mediabunny';++export const getMediaMetadata = async (src: string) => {+ const input = new Input({+ formats: ALL_FORMATS,+ source: new UrlSource(src, {+ getRetryDelay: () => null,+ }),+ });++ const durationInSeconds = await input.computeDuration();+ const videoTrack = await input.getPrimaryVideoTrack();+ const dimensions = videoTrack+ ? {+ width: videoTrack.displayWidth,+ height: videoTrack.displayHeight,+ }+ : null;+ const packetStats = await videoTrack?.computePacketStats();+ const fps = packetStats?.averagePacketRate ?? null;++ return {+ durationInSeconds,+ dimensions,+ fps,+ };+};++// @filename: index.tsxconst VideoTesting: React.FC = () => null;// ---cut----import {parseMedia} from '@remotion/media-parser';+import React from 'react';import {useEffect, useState} from 'react';import {Player} from '@remotion/player';+import {getMediaMetadata} from './get-media-metadata';export const Index: React.FC = () => {const [duration, setDuration] = useState(1);useEffect(() => {- parseMedia({- src: 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',- fields: {- slowDurationInSeconds: true,- },- })- .then(({slowDurationInSeconds}) => {- setDuration(Math.round(slowDurationInSeconds * 30));+ getMediaMetadata('https://remotion.media/video.mp4')+ .then(({durationInSeconds}) => {+ setDuration(Math.round(durationInSeconds * 30));}).catch((err) => {console.log(`Error fetching metadata: ${err}`);diff --git a/packages/docs/docs/media-parser/metadata.mdx b/packages/docs/docs/media-parser/metadata.mdxindex aa931e49f1..50af9df988 100644--- a/packages/docs/docs/media-parser/metadata.mdx+++ b/packages/docs/docs/media-parser/metadata.mdx@@ -7,9 +7,12 @@ sidebar_label: Getting video metadatacrumb: '@remotion/media-parser'----:::warning-[We are phasing out Media Parser and are moving to Mediabunny](/blog/mediabunny)!-:::+## Not recommended anymore++[We are phasing out Media Parser and are moving to Mediabunny](/blog/mediabunny)!+See: [Getting metadata using Mediabunny](/docs/mediabunny/metadata) for the new recommended way.++---Using [`@remotion/media-parser`](/docs/media-parser), you can get metadata from a [variety of media formats](/docs/media-parser/format-support).diff --git a/packages/docs/docs/offthreadvideo.mdx b/packages/docs/docs/offthreadvideo.mdxindex 818ad15f5e..021271474f 100644--- a/packages/docs/docs/offthreadvideo.mdx+++ b/packages/docs/docs/offthreadvideo.mdx@@ -323,9 +323,40 @@ Consider using [another video tag](/docs/video-tags) for looping.You can use the following `<LoopableOffthreadVideo>` component that uses [`@remotion/media-parser`](/docs/media-parser/) to loop a video.```tsx twoslash title="src/LoopableOffthreadVideo.tsx"-import {mediaParserController, parseMedia} from '@remotion/media-parser';+// @filename: get-media-metadata.ts+import {Input, ALL_FORMATS, UrlSource} from 'mediabunny';++export const getMediaMetadata = async (src: string) => {+ const input = new Input({+ formats: ALL_FORMATS,+ source: new UrlSource(src, {+ getRetryDelay: () => null,+ }),+ });++ const durationInSeconds = await input.computeDuration();+ const videoTrack = await input.getPrimaryVideoTrack();+ const dimensions = videoTrack+ ? {+ width: videoTrack.displayWidth,+ height: videoTrack.displayHeight,+ }+ : null;+ const packetStats = await videoTrack?.computePacketStats();+ const fps = packetStats?.averagePacketRate ?? null;++ return {+ durationInSeconds,+ dimensions,+ fps,+ };+};++// @filename: index.tsx+// ---cut---import React, {useEffect, useState} from 'react';import {cancelRender, continueRender, delayRender, Loop, OffthreadVideo, RemotionOffthreadVideoProps, useRemotionEnvironment, useVideoConfig, Html5Video} from 'remotion';+import {getMediaMetadata} from './get-media-metadata';const LoopedOffthreadVideo: React.FC<RemotionOffthreadVideoProps> = (props) => {const [duration, setDuration] = useState<number | null>(null);@@ -333,18 +364,9 @@ const LoopedOffthreadVideo: React.FC<RemotionOffthreadVideoProps> = (props) => {const {fps} = useVideoConfig();useEffect(() => {- const controller = mediaParserController();-- parseMedia({- src: props.src,- acknowledgeRemotionLicense: true,- controller,- fields: {- slowDurationInSeconds: true,- },- })- .then(({slowDurationInSeconds}) => {- setDuration(slowDurationInSeconds);+ getMediaMetadata(props.src)+ .then(({durationInSeconds}) => {+ setDuration(durationInSeconds);continueRender(handle);}).catch((err) => {@@ -353,7 +375,6 @@ const LoopedOffthreadVideo: React.FC<RemotionOffthreadVideoProps> = (props) => {return () => {continueRender(handle);- controller.abort();};}, [handle, props.src]);