Skip to content

🎥 Make videos programmatically with React

Docs: Visualize media trimAfter and looping

JonnyBurger9e6f60532f01
diff --git a/packages/docs/components/TimingTimeline.tsx b/packages/docs/components/TimingTimeline.tsx
index 7d0fecbfef..1367258dc4 100644
--- a/packages/docs/components/TimingTimeline.tsx
+++ b/packages/docs/components/TimingTimeline.tsx
@@ -4,16 +4,26 @@ export const TimingTimeline: React.FC<{
readonly from: number;
readonly durationInFrames?: number;
readonly trimBefore: number;
+ readonly trimAfter: number | null;
readonly playbackRate: number;
-}> = ({from, durationInFrames, trimBefore, playbackRate}) => {
+ readonly loop: boolean;
+}> = ({from, durationInFrames, trimBefore, trimAfter, playbackRate, loop}) => {
const [frame, setFrame] = useState(from);
const scrubbingPointer = useRef<number | null>(null);
- const totalFrames = 90;
+ const totalFrames = loop ? 120 : 90;
const fps = 30;
- const displayedDuration = durationInFrames ?? totalFrames - from;
+ const sourceRange = trimAfter === null ? null : trimAfter - trimBefore;
+ const cycleDuration =
+ sourceRange === null ? null : sourceRange / playbackRate;
+ const displayedDuration = Math.min(
+ durationInFrames ?? totalFrames - from,
+ loop || cycleDuration === null ? Infinity : cycleDuration,
+ );
const localFrame =
frame >= from && frame < from + displayedDuration
- ? (frame - from) * playbackRate + trimBefore
+ ? (loop && sourceRange !== null
+ ? ((frame - from) * playbackRate) % sourceRange
+ : (frame - from) * playbackRate) + trimBefore
: null;
const seconds = Math.floor(frame / fps);
const timecode = `00:${String(seconds).padStart(2, '0')}.${String(frame % fps).padStart(2, '0')}`;
@@ -124,7 +134,7 @@ export const TimingTimeline: React.FC<{
}}
>
<div style={{position: 'relative', height: '100%', margin: '0 16px'}}>
- {Array.from({length: 7}, (_, index) => {
+ {Array.from({length: totalFrames / 15 + 1}, (_, index) => {
const tickFrame = index * 15;
return (
<div
@@ -141,12 +151,13 @@ export const TimingTimeline: React.FC<{
backgroundColor: 'rgba(255, 255, 255, 0.15)',
}}
/>
- {tickFrame % fps === 0 && tickFrame < totalFrames ? (
+ {tickFrame % fps === 0 &&
+ (tickFrame < totalFrames || loop) ? (
<span
style={{
position: 'absolute',
top: 7,
- left: 8,
+ left: tickFrame === totalFrames ? -32 : 8,
color: '#A6A7A9',
fontSize: 12,
whiteSpace: 'nowrap',
@@ -180,6 +191,29 @@ export const TimingTimeline: React.FC<{
paddingLeft: 5,
}}
>
+ {loop && cycleDuration !== null
+ ? Array.from(
+ {
+ length: Math.max(
+ 0,
+ Math.ceil(displayedDuration / cycleDuration) - 1,
+ ),
+ },
+ (_, index) => (
+ <div
+ key={index}
+ style={{
+ position: 'absolute',
+ left: `${(((index + 1) * cycleDuration) / displayedDuration) * 100}%`,
+ top: 0,
+ bottom: 0,
+ width: 1,
+ backgroundColor: 'rgba(255, 255, 255, 0.22)',
+ }}
+ />
+ ),
+ )
+ : null}
{localFrame === null ? null : (
<span
style={{
diff --git a/packages/docs/docs/timing.mdx b/packages/docs/docs/timing.mdx
index ec82dd100d..721e55da04 100644
--- a/packages/docs/docs/timing.mdx
+++ b/packages/docs/docs/timing.mdx
@@ -20,7 +20,7 @@ The way timing works is standardized across these components:
`from` moves an item on its parent timeline. By default, its children start at frame `0` when it appears:
-<TimingTimeline from={30} trimBefore={0} playbackRate={1} />
+<TimingTimeline from={30} trimBefore={0} trimAfter={null} playbackRate={1} loop={false} />
```tsx twoslash title="MyComposition.tsx"
import {Interactive, useCurrentFrame} from 'remotion';
@@ -40,7 +40,7 @@ At composition frame `30`, the children of `<Interactive.Div>` start at their lo
`durationInFrames` sets how long an item stays on its parent timeline, starting at `from`.
-<TimingTimeline from={30} durationInFrames={45} trimBefore={0} playbackRate={1} />
+<TimingTimeline from={30} durationInFrames={45} trimBefore={0} trimAfter={null} playbackRate={1} loop={false} />
```tsx twoslash title="MyComposition.tsx"
import {Interactive, useCurrentFrame} from 'remotion';
@@ -58,7 +58,7 @@ export const MyComposition = () => (
`trimBefore` skips the beginning of an item's content without moving it from its `from` position.
-<TimingTimeline from={30} trimBefore={15} playbackRate={1} />
+<TimingTimeline from={30} trimBefore={15} trimAfter={null} playbackRate={1} loop={false} />
```tsx twoslash title="MyComposition.tsx"
import {Interactive, useCurrentFrame} from 'remotion';
@@ -76,7 +76,7 @@ export const MyComposition = () => (
`playbackRate` on `<Interactive.Div>` changes how quickly its children's frame advances.
-<TimingTimeline from={30} trimBefore={15} playbackRate={2} />
+<TimingTimeline from={30} trimBefore={15} trimAfter={null} playbackRate={2} loop={false} />
```tsx twoslash title="MyComposition.tsx"
import {Interactive, useCurrentFrame} from 'remotion';
@@ -100,6 +100,8 @@ export const MyComposition = () => (
At 30 composition FPS, `trimBefore={45}` and `trimAfter={105}` select the source from 1.5 to 3.5 seconds.
+<TimingTimeline from={30} trimBefore={45} trimAfter={105} playbackRate={2} loop={false} />
+
```tsx twoslash title="MyComposition.tsx"
import {staticFile} from 'remotion';
import {Video} from '@remotion/media';
@@ -121,6 +123,8 @@ Without `loop`, playback stops when it reaches `trimAfter`.
`loop` on `<Audio>` and `<Video>` repeats the source range selected by `trimBefore` and `trimAfter`. If `trimAfter` is omitted, the range ends at the end of the source. Use `durationInFrames` to limit how long the loop remains on the composition timeline:
+<TimingTimeline from={30} durationInFrames={90} trimBefore={45} trimAfter={105} playbackRate={2} loop />
+
```tsx twoslash title="MyComposition.tsx"
import {staticFile} from 'remotion';
import {Video} from '@remotion/media';