Skip to content

🎥 Make videos programmatically with React

Docs: Better jump cuts documentation

JonnyBurger06c87d85804f
diff --git a/packages/docs/components/InlineStep.tsx b/packages/docs/components/InlineStep.tsx
index c9ba5385f8..d929961286 100644
--- a/packages/docs/components/InlineStep.tsx
+++ b/packages/docs/components/InlineStep.tsx
@@ -33,7 +33,8 @@ export const TitleStep: React.FC<{
export const InlineStep: React.FC<{
readonly children: React.ReactNode;
readonly error?: boolean;
-}> = ({children, error}) => {
+ readonly style?: React.CSSProperties;
+}> = ({children, error, style}) => {
return (
<div
style={{
@@ -49,6 +50,7 @@ export const InlineStep: React.FC<{
fontWeight: 'bold',
verticalAlign: 'top',
fontFeatureSettings: '"ss03" on',
+ ...(style ?? {}),
}}
>
{children}
diff --git a/packages/docs/docs/miscellaneous/snippets/jumpcuts.mdx b/packages/docs/docs/miscellaneous/snippets/jumpcuts.mdx
index 0a6d6d5d8f..752b5c6651 100644
--- a/packages/docs/docs/miscellaneous/snippets/jumpcuts.mdx
+++ b/packages/docs/docs/miscellaneous/snippets/jumpcuts.mdx
@@ -5,8 +5,32 @@ sidebar_label: Jump Cuts
crumb: 'Snippets'
---
-You might wanna use a "jump cut" to skip parts of a video.
-Use the following snippet to skip certain sections of a video, without re-mounting it.
+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:
+
+<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>
+
+:::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
+
+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/player/props#acceptabletimeshiftinseconds) prop for force a seek even if it is a tiny jump.
```tsx twoslash
import React, {useMemo} from 'react';
@@ -49,35 +73,48 @@ export const calculateMetadata: CalculateMetadataFunction<Props> = ({props}) =>
export const JumpCuts: React.FC<Props> = ({sections}) => {
const frame = useCurrentFrame();
- const trimBefore = useMemo(() => {
+ const cut = useMemo(() => {
let summedUpDurations = 0;
for (const section of sections) {
summedUpDurations += section.trimAfter - section.trimBefore;
if (summedUpDurations > frame) {
- return section.trimAfter - summedUpDurations;
+ const trimBefore = section.trimAfter - summedUpDurations;
+ const offset = section.trimBefore - frame - trimBefore;
+
+ return {
+ trimBefore,
+ firstFrameOfSection: offset === 0,
+ };
}
}
return null;
}, [frame, sections]);
- if (trimBefore === null) {
+ if (cut === null) {
return null;
}
return (
<OffthreadVideo
pauseWhenBuffering
- trimBefore={trimBefore}
+ 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}
/>
);
};
```
+## 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.
+
## See also
- [Different segments at different speeds](/docs/miscellaneous/snippets/different-segments-at-different-speeds)
diff --git a/packages/example/src/JumpCuts.tsx b/packages/example/src/JumpCuts.tsx
new file mode 100644
index 0000000000..4fd7b27b04
--- /dev/null
+++ b/packages/example/src/JumpCuts.tsx
@@ -0,0 +1,83 @@
+import React, {useMemo} from 'react';
+import {
+ CalculateMetadataFunction,
+ OffthreadVideo,
+ useCurrentFrame,
+} from 'remotion';
+
+const fps = 30;
+
+type Section = {
+ trimBefore: number;
+ trimAfter: number;
+};
+
+export const SAMPLE_SECTIONS: Section[] = [
+ {trimBefore: 0, trimAfter: 5 * fps},
+ {
+ trimBefore: 5.2 * fps,
+ trimAfter: 10 * fps,
+ },
+ {
+ trimBefore: 13 * fps,
+ trimAfter: 18 * fps,
+ },
+];
+
+type Props = {
+ sections: Section[];
+};
+
+export const calculateMetadataJumpCuts: CalculateMetadataFunction<Props> = ({
+ props,
+}) => {
+ const durationInFrames = props.sections.reduce((acc, section) => {
+ return acc + section.trimAfter - section.trimBefore;
+ }, 0);
+
+ return {
+ fps,
+ durationInFrames,
+ };
+};
+
+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;
+ }
+
+ 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={`https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4#t=0,`}
+ // Force Remotion to seek when it jumps even just a tiny bit
+ acceptableTimeShiftInSeconds={
+ cut.firstFrameOfSection ? 0.000001 : undefined
+ }
+ />
+ );
+};
diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsx
index fad2a9130b..57485472b4 100644
--- a/packages/example/src/Root.tsx
+++ b/packages/example/src/Root.tsx
@@ -128,6 +128,7 @@ import {
} from './Compose/WhatIsRemotion';
import {EdgeBlur} from './EdgeBlur/EdgeBlur';
import {Empty} from './Empty';
+import {JumpCuts, SAMPLE_SECTIONS, calculateMetadataJumpCuts} from './JumpCuts';
import {NewVideoExample} from './NewVideo/NewVideo';
import {LoopedOffthreadRemoteVideo} from './OffthreadRemoteVideo/LoopedOffthreadRemoteVideo';
import {MultiChannelAudio} from './OffthreadRemoteVideo/MultiChannelAudio';
@@ -796,6 +797,18 @@ export const Index: React.FC = () => {
fps={30}
durationInFrames={600}
/>
+ <Composition
+ id="jump-cuts"
+ component={JumpCuts}
+ width={1280}
+ height={720}
+ fps={30}
+ durationInFrames={600}
+ calculateMetadata={calculateMetadataJumpCuts}
+ defaultProps={{
+ sections: SAMPLE_SECTIONS,
+ }}
+ />
<Composition
id="corrupt-video"
component={CorruptVideo}