How do I make the composition the same duration as my video?
If you have a component rendering a video:
MyComp.tsximport {Video } from '@remotion/media'; importReact from 'react'; import {staticFile } from 'remotion'; export constMyComp :React .FC = () => { return <Video 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:
MyComp.tsximport {Video } from '@remotion/media'; importReact from 'react'; typeMyCompProps = {src : string; }; export constMyComp :React .FC <MyCompProps > = ({src }) => { return <Video src ={src } />; };
Then, define a calculateMetadata() function that calculates the duration of the composition based on the video.
Install Mediabunny if necessary.
MyComp.tsximport {ALL_FORMATS ,Input ,UrlSource } from 'mediabunny'; import type {CalculateMetadataFunction } from 'remotion'; export constcalculateMetadata :CalculateMetadataFunction <MyCompProps > = async ({props }) => { constinput = newInput ({formats :ALL_FORMATS ,source : newUrlSource (props .src , {getRetryDelay : () => null, }), }); const [durationInSeconds ,videoTrack ] = awaitPromise .all ([input .computeDuration (),input .getPrimaryVideoTrack (), ]); if (videoTrack === null) { // For example when passing an MP3 file: throw newError ('Not a video file'); } const [width ,height ] = awaitPromise .all ([videoTrack .getDisplayWidth (),videoTrack .getDisplayHeight (), ]); constfps = 30; return {durationInFrames :Math .ceil (durationInSeconds *fps ),fps ,width ,height , }; };
Mediabunny loads the asset using fetch(). Remote URLs must be CORS-enabled; files referenced with staticFile() are served from the same origin.
Finally, pass the calculateMetadata() function to the <Composition> component and define the previously hardcoded src as a default prop:
Root.tsximportReact from 'react'; import {Composition } from 'remotion'; import {calculateMetadata } from './calculate-metadata'; import {MyComp } from './MyComp'; export constRoot :React .FC = () => { return ( <Composition id ="MyComp"component ={MyComp }durationInFrames ={300}fps ={30}width ={1920}height ={1080}defaultProps ={{src : 'https://remotion.media/BigBuckBunny.mp4', }}calculateMetadata ={calculateMetadata } /> ); };
How do I make the composition the same duration as my audio?
Follow the same steps. Mediabunny's computeDuration() also returns the duration of an audio file. Omit the video track check and the width and height fields from the returned metadata.