Skip to content

🎥 Make videos programmatically with React

Docs: Update jump cuts snippet to recommend premounting pattern (#8385)

Matt Van Horn3bcdaf271414
diff --git a/packages/docs/docs/miscellaneous/snippets/jumpcuts.mdx b/packages/docs/docs/miscellaneous/snippets/jumpcuts.mdx
index 55e177fc41..f4e146d2c8 100644
--- a/packages/docs/docs/miscellaneous/snippets/jumpcuts.mdx
+++ b/packages/docs/docs/miscellaneous/snippets/jumpcuts.mdx
@@ -5,36 +5,16 @@ sidebar_label: Jump Cuts
crumb: 'Snippets'
---
-Sometimes you want to implement a "jump cut" to skip parts of a video (for example to cut out the "uhm"s).
-You have the following considerations to make:
+Sometimes you want to implement a "jump cut" to skip parts of a video (for example to cut out the "uhm"s).
-<div>
- <div>
- <Step>1</Step> The browser might have already preloaded a small portion of the future of the video (around 1-2 seconds). In this case, it would be useful to reuse the same video tag.
- </div>
- <div>
- <Step>2</Step> A jump far into the future will not be loaded yet. In this case, the video tag should not be re-used, rather a second one should be [premounted](/docs/player/premounting) so it can preload.
- </div>
- <div>
- <Step>3</Step> If the jump is tiny (&lt;0.45sec), Remotion might not seek due to the [`acceptableTimeshiftInSeconds`](/docs/offthreadvideo#acceptabletimeshiftinseconds) prop. You might have to decrease it temporarily.
- </div>
-</div>
+## Best practice: Pre-mounting multiple video tags
-:::note
-These considerations are only for smooth previews in the browser.
-During rendering, videos will be frame-perfect even if you do not follow these considerations.
-:::
-
-## Re-using the same video tag
+The recommended approach for smooth jump-cut playback in the browser is to pre-mount a separate [`<Video>` from `@remotion/media`](/docs/media/video) tag for each section using [`<Series>`](/docs/series) with a `premountFor` prop. This gives the browser time to preload each segment before it starts playing.
-For case <InlineStep style={{marginLeft: 5}}>1</InlineStep> where you make a small jump, it makes sense to re-use the same video tag.
-The following snippet shows how to do this.
-
-In case of <InlineStep style={{marginLeft: 5}}>3</InlineStep> we are temporarily disabling the [`acceptableTimeshiftInSeconds`](/docs/offthreadvideo#acceptabletimeshiftinseconds) prop for force a seek even if it is a tiny jump.
-
-```tsx twoslash
-import React, {useMemo} from 'react';
-import {CalculateMetadataFunction, OffthreadVideo, staticFile, useCurrentFrame} from 'remotion';
+```tsx twoslash title="JumpCuts.tsx"
+import {Video} from '@remotion/media';
+import React from 'react';
+import {CalculateMetadataFunction, Series, staticFile, useVideoConfig} from 'remotion';
const fps = 30;
@@ -45,14 +25,8 @@ type Section = {
export const SAMPLE_SECTIONS: Section[] = [
{trimBefore: 0, trimAfter: 5 * fps},
- {
- trimBefore: 7 * fps,
- trimAfter: 10 * fps,
- },
- {
- trimBefore: 13 * fps,
- trimAfter: 18 * fps,
- },
+ {trimBefore: 7 * fps, trimAfter: 10 * fps},
+ {trimBefore: 13 * fps, trimAfter: 18 * fps},
];
type Props = {
@@ -71,51 +45,33 @@ export const calculateMetadata: CalculateMetadataFunction<Props> = ({props}) =>
};
export const JumpCuts: React.FC<Props> = ({sections}) => {
- const frame = useCurrentFrame();
-
- const cut = useMemo(() => {
- let summedUpDurations = 0;
- for (const section of sections) {
- summedUpDurations += section.trimAfter - section.trimBefore;
- if (summedUpDurations > frame) {
- const trimBefore = section.trimAfter - summedUpDurations;
- const offset = section.trimBefore - frame - trimBefore;
-
- return {
- trimBefore,
- firstFrameOfSection: offset === 0,
- };
- }
- }
-
- return null;
- }, [frame, sections]);
-
- if (cut === null) {
- return null;
- }
+ const {fps: videoFps} = useVideoConfig();
return (
- <OffthreadVideo
- pauseWhenBuffering
- trimBefore={cut.trimBefore}
- // Remotion will automatically add a time fragment to the end of the video URL
- // based on `trimBefore` and `trimAfter`. Opt out of this by adding one yourself.
- // https://www.remotion.dev/docs/media-fragments
- src={`${staticFile('time.mp4')}#t=0,`}
- // Force Remotion to seek when it jumps even just a tiny bit
- acceptableTimeShiftInSeconds={cut.firstFrameOfSection ? 0.000001 : undefined}
- />
+ <Series>
+ {sections.map((section, i) => (
+ <Series.Sequence
+ // Premount the next segment for 1.5 seconds so it can preload before it starts playing
+ premountFor={Math.round(1.5 * videoFps)}
+ key={i}
+ durationInFrames={section.trimAfter - section.trimBefore}
+ >
+ <Video trimBefore={section.trimBefore} trimAfter={section.trimAfter} src={staticFile('time.mp4')} />
+ </Series.Sequence>
+ ))}
+ </Series>
);
};
```
-## Pre-mounting a second video tag
-
-In case <InlineStep style={{marginLeft: 5}}>2</InlineStep> where you make a large jump, it makes sense to pre-mount a second video tag.
-See [Playing videos in sequence](/docs/videos/sequence) for how to do this.
+:::note
+These considerations apply only to smooth previews in the browser.
+During rendering, video frames are selected exactly, so premounting is only needed for smoother browser preview playback.
+:::
## See also
+- [Playing videos in sequence](/docs/videos/sequence)
+- [Premounting](/docs/player/premounting)
- [Different segments at different speeds](/docs/miscellaneous/snippets/different-segments-at-different-speeds)
- [Change the speed of a video over time](/docs/miscellaneous/snippets/accelerated-video)