How do I make the composition the same duration as my video?
If you have a component rendering a video:
MyComp.tsxtsx
importReact from 'react';import {OffthreadVideo ,staticFile } from 'remotion';export constMyComp :React .FC = () => {return <OffthreadVideo src ={staticFile ('video.mp4')} />;};
MyComp.tsxtsx
importReact from 'react';import {OffthreadVideo ,staticFile } from 'remotion';export constMyComp :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:
MyComp.tsxtsx
importReact from 'react';import {OffthreadVideo ,staticFile } from 'remotion';typeMyCompProps = {src : string;};export constMyComp :React .FC <MyCompProps > = ({src }) => {return <OffthreadVideo src ={src } />;};
MyComp.tsxtsx
importReact from 'react';import {OffthreadVideo ,staticFile } from 'remotion';typeMyCompProps = {src : string;};export constMyComp :React .FC <MyCompProps > = ({src }) => {return <OffthreadVideo src ={src } />;};
Then, define a calculateMetadata()
function that calculates the duration of the composition based on the video.
Install @remotion/media-utils
if necessary.
MyComp.tsxtsx
import {CalculateMetadataFunction } from 'remotion';import {getVideoMetadata } from '@remotion/media-utils';export constcalculateMetadata :CalculateMetadataFunction <MyCompProps > = async ({props }) => {constdata = awaitgetVideoMetadata (props .src );constfps = 30;return {durationInFrames :Math .floor (data .durationInSeconds *fps ),fps ,};};
MyComp.tsxtsx
import {CalculateMetadataFunction } from 'remotion';import {getVideoMetadata } from '@remotion/media-utils';export constcalculateMetadata :CalculateMetadataFunction <MyCompProps > = async ({props }) => {constdata = awaitgetVideoMetadata (props .src );constfps = 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:
Root.tsxtsx
importReact from 'react';import {Composition } from 'remotion';import {MyComp ,calculateMetadata } from './MyComp';export constRoot :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 }/>);};
Root.tsxtsx
importReact from 'react';import {Composition } from 'remotion';import {MyComp ,calculateMetadata } from './MyComp';export constRoot :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()
, use getAudioDurationInSeconds()
from @remotion/media-utils
to calculate the duration of the composition based on the audio file.