🎥 Make videos programmatically with React
remotion: Add durationInFrames to legacy media
JonnyBurger8b61aa42c658
diff --git a/packages/core/src/audio/html5-audio.tsx b/packages/core/src/audio/html5-audio.tsxindex 6492411fa7..07e0bde733 100644--- a/packages/core/src/audio/html5-audio.tsx+++ b/packages/core/src/audio/html5-audio.tsx@@ -1,7 +1,10 @@/* eslint-disable @typescript-eslint/no-use-before-define */import React, {forwardRef, useCallback, useContext} from 'react';import {getAbsoluteSrc} from '../absolute-src.js';-import {calculateMediaDuration} from '../calculate-media-duration.js';+import {+ calculateMediaDuration,+ getMediaTrimAfter,+} from '../calculate-media-duration.js';import {cancelRender} from '../cancel-render.js';import {addSequenceStackTraces} from '../enable-sequence-stack-traces.js';import {Loop} from '../loop/index.js';@@ -15,6 +18,7 @@ import {resolveTrimProps,validateMediaTrimProps,} from '../validate-start-from-props.js';+import {validateDurationInFrames} from '../validation/validate-duration-in-frames.js';import {DurationsContext} from '../video/duration-state.js';import {AudioForPreview} from './AudioForPreview.js';import {AudioForRendering} from './AudioForRendering.js';@@ -39,6 +43,7 @@ const AudioRefForwardingFunction: React.ForwardRefRenderFunction<endAt,trimBefore,trimAfter,+ durationInFrames,name,_remotionInternalStack,pauseWhenBuffering,@@ -112,6 +117,12 @@ const AudioRefForwardingFunction: React.ForwardRefRenderFunction<durations[getAbsoluteSrc(props.src)];validateMediaTrimProps({startFrom, endAt, trimBefore, trimAfter});+ if (durationInFrames !== undefined) {+ validateDurationInFrames(durationInFrames, {+ component: 'of the <Html5Audio /> component',+ allowFloats: true,+ });+ }const {trimBeforeValue, trimAfterValue} = resolveTrimProps({startFrom,@@ -119,11 +130,16 @@ const AudioRefForwardingFunction: React.ForwardRefRenderFunction<trimBefore,trimAfter,});+ const effectiveTrimAfter = getMediaTrimAfter({+ durationInFrames,+ trimAfter: trimAfterValue,+ trimBefore: trimBeforeValue,+ });// An explicit trim end defines the loop before metadata has loaded. Waiting// for metadata would leave the media unmounted when seeking into later loops.const loopDuration =- trimAfterValue ??+ effectiveTrimAfter ??(durationFetched === undefined ? undefined : durationFetched * fps);if (loop && loopDuration !== undefined) {if (!Number.isFinite(loopDuration)) {@@ -140,7 +156,7 @@ const AudioRefForwardingFunction: React.ForwardRefRenderFunction<<Looplayout="none"durationInFrames={calculateMediaDuration({- trimAfter: trimAfterValue,+ trimAfter: effectiveTrimAfter,mediaDurationInFrames: loopDuration,playbackRate: props.playbackRate ?? 1,trimBefore: trimBeforeValue,@@ -157,7 +173,7 @@ const AudioRefForwardingFunction: React.ForwardRefRenderFunction<if (typeof trimBeforeValue !== 'undefined' ||- typeof trimAfterValue !== 'undefined'+ typeof effectiveTrimAfter !== 'undefined') {return (<Html5MediaTrimContext.Provider value={trimBeforeValue ?? 0}>@@ -166,10 +182,10 @@ const AudioRefForwardingFunction: React.ForwardRefRenderFunction<from={0 - (trimBeforeValue ?? 0)}showInTimeline={false}durationInFrames={- trimAfterValue === undefined+ effectiveTrimAfter === undefined? undefined: (trimBeforeValue ?? 0) +- (trimAfterValue - (trimBeforeValue ?? 0)) /+ (effectiveTrimAfter - (trimBeforeValue ?? 0)) /(props.playbackRate ?? 1)}name={name}diff --git a/packages/core/src/audio/props.ts b/packages/core/src/audio/props.tsindex bcab14ff92..f412540d05 100644--- a/packages/core/src/audio/props.ts+++ b/packages/core/src/audio/props.ts@@ -14,9 +14,13 @@ export type RemotionMainAudioProps = {* Trim of the audio from the left (start) in frames.*/trimBefore?: number;+ /**+ * How many source frames should be played, starting at `trimBefore`.+ */+ durationInFrames?: number;/*** Trim of the audio from the right (end) in frames.- * @deprecated Wrap the component in a `<Sequence>` and use `durationInFrames` instead. See https://github.com/remotion-dev/remotion/pull/11685+ * @deprecated Use `durationInFrames` instead. See https://github.com/remotion-dev/remotion/pull/11685*/trimAfter?: number;};diff --git a/packages/core/src/calculate-media-duration.ts b/packages/core/src/calculate-media-duration.tsindex fc3c04432e..2e1bbdbe57 100644--- a/packages/core/src/calculate-media-duration.ts+++ b/packages/core/src/calculate-media-duration.ts@@ -23,3 +23,25 @@ export const calculateMediaDuration = ({return duration / playbackRate;};++export const getMediaTrimAfter = ({+ durationInFrames,+ trimAfter,+ trimBefore,+}: {+ readonly durationInFrames: number | undefined;+ readonly trimAfter: number | undefined;+ readonly trimBefore: number | undefined;+}) => {+ const durationAsTrimAfter =+ durationInFrames === undefined+ ? undefined+ : (trimBefore ?? 0) + durationInFrames;+ if (durationAsTrimAfter === undefined) {+ return trimAfter;+ }++ return trimAfter === undefined+ ? durationAsTrimAfter+ : Math.min(trimAfter, durationAsTrimAfter);+};diff --git a/packages/core/src/video/OffthreadVideo.tsx b/packages/core/src/video/OffthreadVideo.tsxindex f716039db0..bc080b89dc 100644--- a/packages/core/src/video/OffthreadVideo.tsx+++ b/packages/core/src/video/OffthreadVideo.tsx@@ -1,5 +1,6 @@import React, {useCallback} from 'react';import {Html5MediaTrimContext} from '../audio/use-audio-frame.js';+import {getMediaTrimAfter} from '../calculate-media-duration.js';import {addSequenceStackTraces} from '../enable-sequence-stack-traces.js';import {Sequence} from '../Sequence.js';import {useRemotionEnvironment} from '../use-remotion-environment.js';@@ -9,6 +10,7 @@ import {resolveTrimProps,validateMediaTrimProps,} from '../validate-start-from-props.js';+import {validateDurationInFrames} from '../validation/validate-duration-in-frames.js';import {OffthreadVideoForRendering} from './OffthreadVideoForRendering.js';import type {AllOffthreadVideoProps,@@ -26,6 +28,7 @@ export const InnerOffthreadVideo: React.FC<AllOffthreadVideoProps> = (endAt,trimBefore,trimAfter,+ durationInFrames,name,pauseWhenBuffering,_remotionInternalStack,@@ -52,6 +55,12 @@ export const InnerOffthreadVideo: React.FC<AllOffthreadVideoProps> = (}validateMediaTrimProps({startFrom, endAt, trimBefore, trimAfter});+ if (durationInFrames !== undefined) {+ validateDurationInFrames(durationInFrames, {+ component: 'of the <OffthreadVideo /> component',+ allowFloats: true,+ });+ }const {trimBeforeValue, trimAfterValue} = resolveTrimProps({startFrom,@@ -59,10 +68,15 @@ export const InnerOffthreadVideo: React.FC<AllOffthreadVideoProps> = (trimBefore,trimAfter,});+ const effectiveTrimAfter = getMediaTrimAfter({+ durationInFrames,+ trimAfter: trimAfterValue,+ trimBefore: trimBeforeValue,+ });if (typeof trimBeforeValue !== 'undefined' ||- typeof trimAfterValue !== 'undefined'+ typeof effectiveTrimAfter !== 'undefined') {return (<Html5MediaTrimContext.Provider value={trimBeforeValue ?? 0}>@@ -71,10 +85,10 @@ export const InnerOffthreadVideo: React.FC<AllOffthreadVideoProps> = (from={0 - (trimBeforeValue ?? 0)}showInTimeline={false}durationInFrames={- trimAfterValue === undefined+ effectiveTrimAfter === undefined? undefined: (trimBeforeValue ?? 0) +- (trimAfterValue - (trimBeforeValue ?? 0)) /+ (effectiveTrimAfter - (trimBeforeValue ?? 0)) /(props.playbackRate ?? 1)}name={name}@@ -83,6 +97,7 @@ export const InnerOffthreadVideo: React.FC<AllOffthreadVideoProps> = (pauseWhenBuffering={shouldPauseWhenBuffering}{...otherProps}trimAfter={undefined}+ durationInFrames={undefined}name={undefined}showInTimeline={showInTimeline}trimBefore={undefined}@@ -103,6 +118,7 @@ export const InnerOffthreadVideo: React.FC<AllOffthreadVideoProps> = (pauseWhenBuffering={shouldPauseWhenBuffering}{...otherProps}trimAfter={undefined}+ durationInFrames={undefined}name={undefined}showInTimeline={showInTimeline}trimBefore={undefined}@@ -168,6 +184,7 @@ export const OffthreadVideo: React.FC<RemotionOffthreadVideoProps> = ({toneMapped,transparent,trimAfter,+ durationInFrames,trimBefore,useWebAudioApi,volume,@@ -214,6 +231,7 @@ export const OffthreadVideo: React.FC<RemotionOffthreadVideoProps> = ({toneMapped={toneMapped ?? true}transparent={transparent ?? false}trimAfter={trimAfter}+ durationInFrames={durationInFrames}trimBefore={trimBefore}useWebAudioApi={useWebAudioApi ?? false}volume={volume}diff --git a/packages/core/src/video/html5-video.tsx b/packages/core/src/video/html5-video.tsxindex fcb141e7e0..f91e4bb5fd 100644--- a/packages/core/src/video/html5-video.tsx+++ b/packages/core/src/video/html5-video.tsx@@ -2,7 +2,10 @@import React, {forwardRef, useCallback, useContext} from 'react';import {getAbsoluteSrc} from '../absolute-src.js';import {Html5MediaTrimContext} from '../audio/use-audio-frame.js';-import {calculateMediaDuration} from '../calculate-media-duration.js';+import {+ calculateMediaDuration,+ getMediaTrimAfter,+} from '../calculate-media-duration.js';import {addSequenceStackTraces} from '../enable-sequence-stack-traces.js';import {Loop} from '../loop/index.js';import {usePreload} from '../prefetch.js';@@ -15,6 +18,7 @@ import {resolveTrimProps,validateMediaTrimProps,} from '../validate-start-from-props.js';+import {validateDurationInFrames} from '../validation/validate-duration-in-frames.js';import {DurationsContext} from './duration-state.js';import type {RemotionMainVideoProps, RemotionVideoProps} from './props';import {VideoForPreview} from './VideoForPreview.js';@@ -35,6 +39,7 @@ const VideoForwardingFunction: React.ForwardRefRenderFunction<endAt,trimBefore,trimAfter,+ durationInFrames,name,pauseWhenBuffering,_remotionInternalStack,@@ -83,17 +88,29 @@ const VideoForwardingFunction: React.ForwardRefRenderFunction<durations[getAbsoluteSrc(props.src)];validateMediaTrimProps({startFrom, endAt, trimBefore, trimAfter});+ if (durationInFrames !== undefined) {+ validateDurationInFrames(durationInFrames, {+ component: 'of the <Html5Video /> component',+ allowFloats: true,+ });+ }+const {trimBeforeValue, trimAfterValue} = resolveTrimProps({startFrom,endAt,trimBefore,trimAfter,});+ const effectiveTrimAfter = getMediaTrimAfter({+ durationInFrames,+ trimAfter: trimAfterValue,+ trimBefore: trimBeforeValue,+ });// An explicit trim end defines the loop before metadata has loaded. Waiting// for metadata would leave the media unmounted when seeking into later loops.const loopDuration =- trimAfterValue ??+ effectiveTrimAfter ??(durationFetched === undefined ? undefined : durationFetched * fps);if (loop && loopDuration !== undefined) {if (!Number.isFinite(loopDuration)) {@@ -110,7 +127,7 @@ const VideoForwardingFunction: React.ForwardRefRenderFunction<return (<LoopdurationInFrames={calculateMediaDuration({- trimAfter: trimAfterValue,+ trimAfter: effectiveTrimAfter,mediaDurationInFrames: loopDuration,playbackRate: props.playbackRate ?? 1,trimBefore: trimBeforeValue,@@ -131,7 +148,7 @@ const VideoForwardingFunction: React.ForwardRefRenderFunction<if (typeof trimBeforeValue !== 'undefined' ||- typeof trimAfterValue !== 'undefined'+ typeof effectiveTrimAfter !== 'undefined') {return (<Html5MediaTrimContext.Provider value={trimBeforeValue ?? 0}>@@ -140,10 +157,10 @@ const VideoForwardingFunction: React.ForwardRefRenderFunction<from={0 - (trimBeforeValue ?? 0)}showInTimeline={false}durationInFrames={- trimAfterValue === undefined+ effectiveTrimAfter === undefined? undefined: (trimBeforeValue ?? 0) +- (trimAfterValue - (trimBeforeValue ?? 0)) /+ (effectiveTrimAfter - (trimBeforeValue ?? 0)) /(props.playbackRate ?? 1)}name={name}diff --git a/packages/core/src/video/props.ts b/packages/core/src/video/props.tsindex b1f60738bc..2d96175f17 100644--- a/packages/core/src/video/props.ts+++ b/packages/core/src/video/props.ts@@ -15,9 +15,13 @@ export type CommonVideoProps = {* Trim of the video from the left (start) in frames.*/trimBefore: number | undefined;+ /**+ * How many source frames should be played, starting at `trimBefore`.+ */+ durationInFrames: number | undefined;/*** Trim of the video from the right (end) in frames.- * @deprecated Wrap the component in a `<Sequence>` and use `durationInFrames` instead. See https://github.com/remotion-dev/remotion/pull/11685+ * @deprecated Use `durationInFrames` instead. See https://github.com/remotion-dev/remotion/pull/11685*/trimAfter: number | undefined;/**diff --git a/packages/docs/docs/html5-audio.mdx b/packages/docs/docs/html5-audio.mdxindex d6f3799b83..f605ef2261 100644--- a/packages/docs/docs/html5-audio.mdx+++ b/packages/docs/docs/html5-audio.mdx@@ -33,6 +33,10 @@ export const MyVideo = () => {};```+### `durationInFrames?`<AvailableFrom v="4.0.530" />++Selects this many source frames starting at `trimBefore`. The audio occupies `durationInFrames / playbackRate` frames on the parent timeline, or uses this range as its loop when `loop` is enabled.+### `volume?`Allows you to control the volume of the audio in it's entirety or frame by frame. Read the page on [using audio](/docs/using-audio) to learn more.@@ -76,19 +80,17 @@ Removes a portion of the audio at the beginning (left side).In the following example, we assume that the [`fps`](/docs/composition#fps) of the composition is `30`.By passing `trimBefore={60}`, the playback starts immediately, but with the first 2 seconds of the audio trimmed away.-By wrapping the audio in a [`<Sequence>`](/docs/sequence) with `durationInFrames={60}`, it plays for 60 frames.+By passing `durationInFrames={60}`, the audio plays for 60 source frames.The audio will play the range from `00:02:00` to `00:04:00`, meaning the audio will play for 2 seconds.```tsx twoslash-import {AbsoluteFill, Html5Audio, Sequence, staticFile} from 'remotion';+import {AbsoluteFill, Html5Audio, staticFile} from 'remotion';export const MyVideo = () => {return (<AbsoluteFill>- <Sequence durationInFrames={60}>- <Html5Audio src={staticFile('audio.mp3')} trimBefore={60} />- </Sequence>+ <Html5Audio src={staticFile('audio.mp3')} trimBefore={60} durationInFrames={60} /></AbsoluteFill>);};@@ -97,7 +99,7 @@ export const MyVideo = () => {### ~~`trimAfter?`~~<AvailableFrom v="4.0.319"/>:::info Deprecated-Wrap the component in a [`<Sequence>`](/docs/sequence) and use [`durationInFrames`](/docs/sequence#durationinframes) instead. See [PR #11685](https://github.com/remotion-dev/remotion/pull/11685).+Use [`durationInFrames`](#durationinframes) instead. See [PR #11685](https://github.com/remotion-dev/remotion/pull/11685).:::Removes a portion of the audio at the end (right side).diff --git a/packages/docs/docs/html5-video.mdx b/packages/docs/docs/html5-video.mdxindex 7ed1b4cb89..f68cce7bf1 100644--- a/packages/docs/docs/html5-video.mdx+++ b/packages/docs/docs/html5-video.mdx@@ -55,6 +55,10 @@ export const MyComposition = () => {The URL of the video to be rendered. Can be a remote URL or a local file referenced with [`staticFile()`](/docs/staticfile).+### `durationInFrames?`<AvailableFrom v="4.0.530" />++Selects this many source frames starting at `trimBefore`. The video occupies `durationInFrames / playbackRate` frames on the parent timeline, or uses this range as its loop when `loop` is enabled.+### `trimBefore?`<AvailableFrom v="4.0.319"/>Will remove a portion of the video at the beginning (left side).@@ -62,22 +66,20 @@ Will remove a portion of the video at the beginning (left side).In the following example, we assume that the [`fps`](/docs/composition#fps) of the composition is `30`.By passing `trimBefore={60}`, the playback starts immediately, but with the first 2 seconds of the video trimmed away.-By wrapping the video in a [`<Sequence>`](/docs/sequence) with `durationInFrames={60}`, it plays for 60 frames.+By passing `durationInFrames={60}`, the video plays for 60 source frames.The video will play the range from `00:02:00` to `00:04:00`, meaning the video will play for 2 seconds.For exact behavior, see [Timing and trimming](/docs/timing).```tsx twoslash-import {AbsoluteFill, Html5Video, Sequence, staticFile} from 'remotion';+import {AbsoluteFill, Html5Video, staticFile} from 'remotion';// ---cut---export const MyComposition = () => {return (<AbsoluteFill>- <Sequence durationInFrames={60}>- <Html5Video src={staticFile('video.webm')} trimBefore={60} />- </Sequence>+ <Html5Video src={staticFile('video.webm')} trimBefore={60} durationInFrames={60} /></AbsoluteFill>);};@@ -86,7 +88,7 @@ export const MyComposition = () => {### ~~`trimAfter?`~~<AvailableFrom v="4.0.319"/>:::info Deprecated-Wrap the component in a [`<Sequence>`](/docs/sequence) and use [`durationInFrames`](/docs/sequence#durationinframes) instead. See [PR #11685](https://github.com/remotion-dev/remotion/pull/11685).+Use [`durationInFrames`](#durationinframes) instead. See [PR #11685](https://github.com/remotion-dev/remotion/pull/11685).:::Removes a portion of the video at the end (right side). See [`trimBefore`](#trimbefore) for an explanation.diff --git a/packages/docs/docs/offthreadvideo.mdx b/packages/docs/docs/offthreadvideo.mdxindex 45692ee4dc..4083c651ed 100644--- a/packages/docs/docs/offthreadvideo.mdx+++ b/packages/docs/docs/offthreadvideo.mdx@@ -51,6 +51,10 @@ export const MyComposition = () => {The URL of the video to be rendered. Can be a remote URL or a local file referenced with [`staticFile()`](/docs/staticfile).+### `durationInFrames?`<AvailableFrom v="4.0.530" />++Selects this many source frames starting at `trimBefore`. The video occupies `durationInFrames / playbackRate` frames on the parent timeline.+### `trimBefore?`<AvailableFrom v="4.0.319"/>Will remove a portion of the video at the beginning (left side).@@ -58,22 +62,20 @@ Will remove a portion of the video at the beginning (left side).In the following example, we assume that the [`fps`](/docs/composition#fps) of the composition is `30`.By passing `trimBefore={60}`, the playback starts immediately, but with the first 2 seconds of the video trimmed away.-By wrapping the video in a [`<Sequence>`](/docs/sequence) with `durationInFrames={60}`, it plays for 60 frames.+By passing `durationInFrames={60}`, the video plays for 60 source frames.The video will play the range from `00:02:00` to `00:04:00`, meaning the video will play for 2 seconds.For exact behavior, see [Timing and trimming](/docs/timing).```tsx twoslash-import {AbsoluteFill, OffthreadVideo, Sequence, staticFile} from 'remotion';+import {AbsoluteFill, OffthreadVideo, staticFile} from 'remotion';// ---cut---export const MyComposition = () => {return (<AbsoluteFill>- <Sequence durationInFrames={60}>- <OffthreadVideo src={staticFile('video.webm')} trimBefore={60} />- </Sequence>+ <OffthreadVideo src={staticFile('video.webm')} trimBefore={60} durationInFrames={60} /></AbsoluteFill>);};@@ -82,7 +84,7 @@ export const MyComposition = () => {### ~~`trimAfter?`~~<AvailableFrom v="4.0.319"/>:::info Deprecated-Wrap the component in a [`<Sequence>`](/docs/sequence) and use [`durationInFrames`](/docs/sequence#durationinframes) instead. See [PR #11685](https://github.com/remotion-dev/remotion/pull/11685).+Use [`durationInFrames`](#durationinframes) instead. See [PR #11685](https://github.com/remotion-dev/remotion/pull/11685).:::Removes a portion of the video at the end (right side). See [`trimBefore`](/docs/offthreadvideo#trimbefore) for an explanation.diff --git a/packages/media/src/video/video-for-rendering.tsx b/packages/media/src/video/video-for-rendering.tsxindex e65f0c982f..8a0751e2b6 100644--- a/packages/media/src/video/video-for-rendering.tsx+++ b/packages/media/src/video/video-for-rendering.tsx@@ -510,6 +510,7 @@ export const VideoForRendering: React.FC<InnerVideoProps> = ({fallbackOffthreadVideoProps?.pauseWhenBuffering ?? false}trimAfter={trimAfterValue}+ durationInFrames={undefined}trimBefore={trimBeforeValue}useWebAudioApi={fallbackOffthreadVideoProps?.useWebAudioApi ?? false}preservePitch={fallbackOffthreadVideoProps?.preservePitch ?? true}