Skip to content

🎥 Make videos programmatically with React

Add feature sub range for clip

Ankesh Paramanik1a708493a371
diff --git a/packages/cli/src/editor/components/Editor.tsx b/packages/cli/src/editor/components/Editor.tsx
index ce7388dc44..4bbefc43c7 100644
--- a/packages/cli/src/editor/components/Editor.tsx
+++ b/packages/cli/src/editor/components/Editor.tsx
@@ -4,6 +4,8 @@ import {
Internals,
MediaVolumeContextValue,
SetMediaVolumeContextValue,
+ SetTimelineInOutContextValue,
+ TimelineInOutContextValue,
} from 'remotion';
import {BACKGROUND} from '../helpers/colors';
import {noop} from '../helpers/noop';
@@ -83,6 +85,8 @@ export const Editor: React.FC = () => {
},
[]
);
+ const [inFrame, setInFrame] = useState<number | null>(null);
+ const [outFrame, setOutFrame] = useState<number | null>(null);
const [mediaMuted, setMediaMuted] = useState<boolean>(false);
const [mediaVolume, setMediaVolume] = useState<number>(1);
const [modalContextType, setModalContextType] = useState<ModalState | null>(
@@ -108,6 +112,20 @@ export const Editor: React.FC = () => {
};
}, [richTimeline, setRichTimeline]);
+ const timelineInOutContextValue = useMemo((): TimelineInOutContextValue => {
+ return {
+ inFrame,
+ outFrame,
+ };
+ }, [inFrame, outFrame]);
+
+ const setTimelineInOutContextValue = useMemo((): SetTimelineInOutContextValue => {
+ return {
+ setInFrame,
+ setOutFrame,
+ };
+ }, [setInFrame, setOutFrame]);
+
const mediaVolumeContextValue = useMemo((): MediaVolumeContextValue => {
return {
mediaMuted,
@@ -139,42 +157,50 @@ export const Editor: React.FC = () => {
<CheckerboardContext.Provider value={checkerboardCtx}>
<PreviewSizeContext.Provider value={previewSizeCtx}>
<ModalsContext.Provider value={modalsContext}>
- <Internals.MediaVolumeContext.Provider
- value={mediaVolumeContextValue}
+ <Internals.Timeline.TimelineInOutContext.Provider
+ value={timelineInOutContextValue}
>
- <Internals.SetMediaVolumeContext.Provider
- value={setMediaVolumeContextValue}
+ <Internals.Timeline.SetTimelineInOutContext.Provider
+ value={setTimelineInOutContextValue}
>
- <PlayerInternals.PlayerEventEmitterContext.Provider
- value={emitter}
+ <Internals.MediaVolumeContext.Provider
+ value={mediaVolumeContextValue}
>
- <HighestZIndexProvider>
- <HigherZIndex onEscape={noop} onOutsideClick={noop}>
- <div style={background}>
- <Root />
- <FramePersistor />
- <EditorContent />
- <GlobalKeybindings />
- </div>
- {modalContextType &&
- modalContextType.type === 'new-comp' && (
- <NewComposition
- initialCompType={modalContextType.compType}
- />
- )}
- {modalContextType &&
- modalContextType.type === 'update' && (
- <UpdateModal info={modalContextType.info} />
- )}
- {modalContextType &&
- modalContextType.type === 'shortcuts' && (
- <KeyboardShortcuts />
- )}
- </HigherZIndex>
- </HighestZIndexProvider>
- </PlayerInternals.PlayerEventEmitterContext.Provider>
- </Internals.SetMediaVolumeContext.Provider>
- </Internals.MediaVolumeContext.Provider>
+ <Internals.SetMediaVolumeContext.Provider
+ value={setMediaVolumeContextValue}
+ >
+ <PlayerInternals.PlayerEventEmitterContext.Provider
+ value={emitter}
+ >
+ <HighestZIndexProvider>
+ <HigherZIndex onEscape={noop} onOutsideClick={noop}>
+ <div style={background}>
+ <Root />
+ <FramePersistor />
+ <EditorContent />
+ <GlobalKeybindings />
+ </div>
+ {modalContextType &&
+ modalContextType.type === 'new-comp' && (
+ <NewComposition
+ initialCompType={modalContextType.compType}
+ />
+ )}
+ {modalContextType &&
+ modalContextType.type === 'update' && (
+ <UpdateModal info={modalContextType.info} />
+ )}
+ {modalContextType &&
+ modalContextType.type === 'shortcuts' && (
+ <KeyboardShortcuts />
+ )}
+ </HigherZIndex>
+ </HighestZIndexProvider>
+ </PlayerInternals.PlayerEventEmitterContext.Provider>
+ </Internals.SetMediaVolumeContext.Provider>
+ </Internals.MediaVolumeContext.Provider>
+ </Internals.Timeline.SetTimelineInOutContext.Provider>
+ </Internals.Timeline.TimelineInOutContext.Provider>
</ModalsContext.Provider>
</PreviewSizeContext.Provider>
</CheckerboardContext.Provider>
diff --git a/packages/cli/src/editor/components/PreviewToolbar.tsx b/packages/cli/src/editor/components/PreviewToolbar.tsx
index a2c5c72d36..a6fdd4eb2d 100644
--- a/packages/cli/src/editor/components/PreviewToolbar.tsx
+++ b/packages/cli/src/editor/components/PreviewToolbar.tsx
@@ -6,6 +6,7 @@ import {Flex} from './layout';
import {PlayPause} from './PlayPause';
import {RichTimelineToggle} from './RichTimelineToggle';
import {SizeSelector} from './SizeSelector';
+import {TimelineInOutPointToggle} from './TimelineInOutToggle';
import {TimeValue} from './TimeValue';
const container: React.CSSProperties = {
@@ -42,6 +43,7 @@ export const PreviewToolbar: React.FC = () => {
<PlayPause />
<CheckboardToggle />
<RichTimelineToggle />
+ <TimelineInOutPointToggle />
<Flex />
<div style={sideContainer}>
<Flex />
diff --git a/packages/cli/src/editor/components/Timeline/Timeline.tsx b/packages/cli/src/editor/components/Timeline/Timeline.tsx
index da88bad27d..86635c0867 100644
--- a/packages/cli/src/editor/components/Timeline/Timeline.tsx
+++ b/packages/cli/src/editor/components/Timeline/Timeline.tsx
@@ -19,6 +19,7 @@ import {isTrackHidden} from './is-collapsed';
import {MAX_TIMELINE_TRACKS} from './MaxTimelineTracks';
import {timelineStateReducer} from './timeline-state-reducer';
import {TimelineDragHandler} from './TimelineDragHandler';
+import { TimelineInOutPointer } from './TimelineInOutPointer';
import {TimelineList} from './TimelineList';
import {TimelineSlider} from './TimelineSlider';
import {TimelineTracks} from './TimelineTracks';
@@ -113,6 +114,7 @@ export const Timeline: React.FC = () => {
hasBeenCut={withoutHidden.length > shown.length}
/>
<TimelineSlider />
+ <TimelineInOutPointer />
<TimelineDragHandler />
</SplitterElement>
</SplitterContainer>
diff --git a/packages/cli/src/editor/components/Timeline/TimelineInOutPointer.tsx b/packages/cli/src/editor/components/Timeline/TimelineInOutPointer.tsx
new file mode 100644
index 0000000000..3ce87e8feb
--- /dev/null
+++ b/packages/cli/src/editor/components/Timeline/TimelineInOutPointer.tsx
@@ -0,0 +1,96 @@
+import {PlayerInternals} from '@remotion/player';
+import React, {useEffect} from 'react';
+import {Internals} from 'remotion';
+import {TIMELINE_PADDING} from '../../helpers/timeline-layout';
+import {sliderAreaRef} from './timeline-refs';
+import {
+ TimelineInPointerHandle,
+ TimelineOutPointerHandle,
+} from './TimelineInOutPointerHandle';
+
+const container: React.CSSProperties = {
+ position: 'absolute',
+ bottom: 0,
+ top: 0,
+};
+
+const areaHighlight: React.CSSProperties = {
+ position: 'absolute',
+ backgroundColor: '#448cc059',
+ height: '100%',
+ bottom: 0,
+ top: 0,
+};
+
+export const TimelineInOutPointer: React.FC = () => {
+ const timelinePosition = Internals.Timeline.useTimelinePosition();
+ const {
+ inFrame,
+ outFrame,
+ } = Internals.Timeline.useTimelineInOutFramePosition();
+ const {
+ setInFrame,
+ setOutFrame,
+ } = Internals.Timeline.useTimelineSetInOutFramePosition();
+ const videoConfig = Internals.useUnsafeVideoConfig();
+ const {playing} = PlayerInternals.usePlayer();
+ const size = PlayerInternals.useElementSize(sliderAreaRef, {
+ triggerOnWindowResize: false,
+ });
+ const width = size?.width ?? 0;
+
+ useEffect(() => {
+ if (playing) {
+ if (inFrame && timelinePosition < inFrame) {
+ setInFrame(inFrame);
+ } else if (outFrame && timelinePosition > outFrame) {
+ setOutFrame(outFrame);
+ }
+ }
+ }, [timelinePosition, playing, inFrame, outFrame, setInFrame, setOutFrame]);
+
+ if (!videoConfig) {
+ return null;
+ }
+
+ const getLeft = (frame: number) =>
+ (frame / (videoConfig.durationInFrames - 1)) *
+ (width - TIMELINE_PADDING * 2) +
+ TIMELINE_PADDING;
+
+ return (
+ <>
+ {(inFrame !== null || outFrame !== null) && (
+ <div
+ style={{
+ ...areaHighlight,
+ left: getLeft(inFrame ?? 0),
+ width:
+ getLeft(outFrame ?? videoConfig.durationInFrames - 1) -
+ getLeft(inFrame ?? 0),
+ }}
+ />
+ )}
+ {inFrame !== null && (
+ <div
+ style={{
+ ...container,
+ transform: `translateX(${getLeft(inFrame)}px)`,
+ }}
+ >
+ <TimelineInPointerHandle />
+ </div>
+ )}
+ {outFrame !== null && (
+ <div
+ style={{
+ ...container,
+ transform: `translateX(${getLeft(outFrame)}px)`,
+ }}
+ >
+ <TimelineOutPointerHandle />
+ </div>
+ )}
+ </>
+ );
+};
diff --git a/packages/cli/src/editor/components/Timeline/TimelineInOutPointerHandle.tsx b/packages/cli/src/editor/components/Timeline/TimelineInOutPointerHandle.tsx
new file mode 100644
index 0000000000..b85a576c34
--- /dev/null
+++ b/packages/cli/src/editor/components/Timeline/TimelineInOutPointerHandle.tsx
@@ -0,0 +1,35 @@
+import React from 'react';
+
+const line: React.CSSProperties = {
+ height: '100%',
+ width: 2,
+ position: 'fixed',
+ backgroundColor: '#06d639',
+};
+
+const dash: React.CSSProperties = {
+ width: 10,
+ height: 2,
+ position: 'fixed',
+ backgroundColor: '#06d639',
+};
+
+export const TimelineInPointerHandle: React.FC = () => {
+ return (
+ <>
+ <div style={{...dash, top: 0, left: 0}} />
+ <div style={line} />
+ <div style={{...dash, bottom: 0, left: 0}} />
+ </>
+ );
+};
+
+export const TimelineOutPointerHandle: React.FC = () => {
+ return (
+ <>
+ <div style={{...dash, top: 0, right: 0}} />
+ <div style={line} />
+ <div style={{...dash, bottom: 0, right: 0}} />
+ </>
+ );
+};
diff --git a/packages/cli/src/editor/components/TimelineInOutToggle.tsx b/packages/cli/src/editor/components/TimelineInOutToggle.tsx
new file mode 100644
index 0000000000..8fe99df609
--- /dev/null
+++ b/packages/cli/src/editor/components/TimelineInOutToggle.tsx
@@ -0,0 +1,146 @@
+import React, {useCallback, useContext, useEffect, useState} from 'react';
+import {Internals} from 'remotion';
+import {TimelineInOutPointer} from '../icons/timelineInOutPointer';
+import {ControlButton} from './ControlButton';
+
+const MIN_FRAMES_BETWEEN_POINTS = 1;
+const getTooltipText = (pointType: string) => `Mark ${pointType}`;
+
+export const TimelineInOutPointToggle: React.FC = () => {
+ const [inFrameBtnActive, setInFrameBtnActiveState] = useState(false);
+ const [outFrameBtnActive, setOutFrameBtnActiveState] = useState(false);
+ const timelinePosition = Internals.Timeline.useTimelinePosition();
+ const {currentComposition} = useContext(Internals.CompositionManager);
+ const {
+ inFrame,
+ outFrame,
+ } = Internals.Timeline.useTimelineInOutFramePosition();
+ const {
+ setInFrame,
+ setOutFrame,
+ } = Internals.Timeline.useTimelineSetInOutFramePosition();
+ const videoConfig = Internals.useUnsafeVideoConfig();
+
+ const getValidFrame = useCallback(
+ (type: 'inFrame' | 'outFrame') => {
+ let validInFrame = null;
+ let validOutFrame = null;
+
+ if (!videoConfig) {
+ return {validInFrame, validOutFrame};
+ }
+
+ if (type === 'inFrame') {
+ validInFrame = timelinePosition;
+ if (outFrame !== null && timelinePosition <= outFrame) {
+ validOutFrame =
+ timelinePosition === outFrame
+ ? timelinePosition + MIN_FRAMES_BETWEEN_POINTS
+ : outFrame;
+ if (validOutFrame > videoConfig.durationInFrames - 1) {
+ validInFrame = timelinePosition - MIN_FRAMES_BETWEEN_POINTS;
+ validOutFrame = videoConfig.durationInFrames - 1;
+ }
+ }
+ } else {
+ validOutFrame = timelinePosition;
+ if (inFrame !== null && timelinePosition >= inFrame) {
+ validInFrame =
+ timelinePosition === inFrame
+ ? timelinePosition - MIN_FRAMES_BETWEEN_POINTS
+ : inFrame;
+ if (validInFrame < 0) {
+ validInFrame = 0;
+ validOutFrame = timelinePosition + MIN_FRAMES_BETWEEN_POINTS;
+ }
+ }
+ }
+
+ return {validInFrame, validOutFrame};
+ },
+ [inFrame, outFrame, timelinePosition, videoConfig]
+ );
+
+ const onInFrameBtnClick = useCallback(() => {
+ if (!videoConfig) {
+ return null;
+ }
+
+ const {validInFrame, validOutFrame} = getValidFrame('inFrame');
+ setInFrame(inFrameBtnActive ? null : validInFrame);
+ setOutFrame(inFrameBtnActive ? outFrame : validOutFrame);
+ setInFrameBtnActiveState(!inFrameBtnActive);
+ if (validOutFrame === null) {
+ setOutFrameBtnActiveState(inFrameBtnActive ? outFrameBtnActive : false);
+ }
+ }, [
+ getValidFrame,
+ inFrameBtnActive,
+ outFrame,
+ outFrameBtnActive,
+ setInFrame,
+ setOutFrame,
+ videoConfig,
+ ]);
+
+ const onOutFrameBtnClick = useCallback(() => {
+ if (!videoConfig) {
+ return null;
+ }
+
+ const {validInFrame, validOutFrame} = getValidFrame('outFrame');
+ setInFrame(outFrameBtnActive ? inFrame : validInFrame);
+ setOutFrame(outFrameBtnActive ? null : validOutFrame);
+ setOutFrameBtnActiveState(!outFrameBtnActive);
+ if (validInFrame === null) {
+ setInFrameBtnActiveState(outFrameBtnActive ? inFrameBtnActive : false);
+ }
+ }, [
+ getValidFrame,
+ inFrame,
+ inFrameBtnActive,
+ outFrameBtnActive,
+ setInFrame,
+ setOutFrame,
+ videoConfig,
+ ]);
+
+ useEffect(() => {
+ setInFrame(null);
+ setInFrameBtnActiveState(false);
+ setOutFrame(null);
+ setOutFrameBtnActiveState(false);
+ }, [currentComposition, setInFrame, setOutFrame]);
+
+ return (
+ <>
+ <ControlButton
+ title={getTooltipText('In')}
+ aria-label={getTooltipText('In')}
+ onClick={onInFrameBtnClick}
+ >
+ <TimelineInOutPointer
+ style={{
+ width: 16,
+ height: 16,
+ color: inFrameBtnActive ? 'var(--blue)' : 'white',
+ }}
+ />
+ </ControlButton>
+ <ControlButton
+ title={getTooltipText('Out')}
+ aria-label={getTooltipText('Out')}
+ onClick={onOutFrameBtnClick}
+ >
+ <TimelineInOutPointer
+ style={{
+ width: 16,
+ height: 16,
+ transform: 'rotate(180deg)',
+ color: outFrameBtnActive ? 'var(--blue)' : 'white',
+ }}
+ />
+ </ControlButton>
+ </>
+ );
+};
diff --git a/packages/cli/src/editor/icons/timelineInOutPointer.tsx b/packages/cli/src/editor/icons/timelineInOutPointer.tsx
new file mode 100644
index 0000000000..482d875588
--- /dev/null
+++ b/packages/cli/src/editor/icons/timelineInOutPointer.tsx
@@ -0,0 +1,12 @@
+import React, {SVGProps} from 'react';
+
+export const TimelineInOutPointer: React.FC<SVGProps<SVGSVGElement>> = (props) => {
+ return (
+ <svg viewBox="0 0 50 150" {...props}>
+ <polygon
+ fill={props.style?.color}
+ points="26.012 8.916 43.845 8.916 43.845 0 17.096 0 17.096 60.339 7.222 67.494 0.802 72.153 0 72.733 0.201 73 0 73.267 0.802 73.847 7.222 78.506 17.096 85.661 17.096 146 43.845 146 43.845 137.084 26.012 137.084 26.012 81.114 14.823 73 26.012 64.886 26.012 8.916"
+ />
+ </svg>
+ );
+};
diff --git a/packages/core/src/internals.ts b/packages/core/src/internals.ts
index e31d4861b4..9ee368dba4 100644
--- a/packages/core/src/internals.ts
+++ b/packages/core/src/internals.ts
@@ -85,7 +85,12 @@ import {
ENV_VARIABLES_LOCAL_STORAGE_KEY,
setupEnvVariables,
} from './setup-env-variables';
-import * as Timeline from './timeline-position-state';
+import * as TimelineInOutPosition from './timeline-inout-position-state';
+import {
+ SetTimelineInOutContextValue,
+ TimelineInOutContextValue,
+} from './timeline-inout-position-state';
+import * as TimelinePosition from './timeline-position-state';
import {
SetTimelineContextValue,
TimelineContextValue,
@@ -117,6 +122,8 @@ import {
useRemotionContexts,
} from './wrap-remotion-context';
+const Timeline = {...TimelinePosition, ...TimelineInOutPosition};
+
// Mark them as Internals so use don't assume this is public
// API and are less likely to use it
export const Internals = {
@@ -204,6 +211,8 @@ export type {
RenderAssetInfo,
TimelineContextValue,
SetTimelineContextValue,
+ TimelineInOutContextValue,
+ SetTimelineInOutContextValue,
CompProps,
CompositionManagerContext,
MediaVolumeContextValue,
diff --git a/packages/core/src/timeline-inout-position-state.ts b/packages/core/src/timeline-inout-position-state.ts
new file mode 100644
index 0000000000..6229a0739e
--- /dev/null
+++ b/packages/core/src/timeline-inout-position-state.ts
@@ -0,0 +1,37 @@
+import {createContext, useContext} from 'react';
+
+export type TimelineInOutContextValue = {
+ inFrame: number | null;
+ outFrame: number | null;
+};
+
+export type SetTimelineInOutContextValue = {
+ setInFrame: (u: React.SetStateAction<number | null>) => void;
+ setOutFrame: (u: React.SetStateAction<number | null>) => void;
+};
+
+export const TimelineInOutContext = createContext<TimelineInOutContextValue>({
+ inFrame: null,
+ outFrame: null,
+});
+
+export const SetTimelineInOutContext = createContext<
+ SetTimelineInOutContextValue
+>({
+ setInFrame: () => {
+ throw new Error('default');
+ },
+ setOutFrame: () => {
+ throw new Error('default');
+ },
+});
+
+export const useTimelineInOutFramePosition = (): TimelineInOutContextValue => {
+ const state = useContext(TimelineInOutContext);
+ return state;
+};
+
+export const useTimelineSetInOutFramePosition = (): SetTimelineInOutContextValue => {
+ const {setInFrame, setOutFrame} = useContext(SetTimelineInOutContext);
+ return {setInFrame, setOutFrame};
+};
diff --git a/packages/player/src/use-playback.ts b/packages/player/src/use-playback.ts
index 7814adc0e2..9979590b79 100644
--- a/packages/player/src/use-playback.ts
+++ b/packages/player/src/use-playback.ts
@@ -7,6 +7,10 @@ export const usePlayback = ({loop}: {loop: boolean}) => {
const config = Internals.useUnsafeVideoConfig();
const {playing, pause, emitter} = usePlayer();
const setFrame = Internals.Timeline.useTimelineSetFrame();
+ const {
+ inFrame,
+ outFrame,
+ } = Internals.Timeline.useTimelineInOutFramePosition();
const frameRef = useRef(frame);
frameRef.current = frame;
@@ -22,10 +26,33 @@ export const usePlayback = ({loop}: {loop: boolean}) => {
return;
}
+ const getFrameInRange = (nextFrame: number) => {
+ if (
+ (inFrame && nextFrame < inFrame) ||
+ (inFrame && outFrame && nextFrame > outFrame)
+ ) {
+ return inFrame;
+ }
+
+ if (outFrame && nextFrame > outFrame) {
+ return 0;
+ }
+
+ return nextFrame;
+ };
+
let hasBeenStopped = false;
let reqAnimFrameCall: number | null = null;
const startedTime = performance.now();
- const startedFrame = frameRef.current;
+ const startedFrame = getFrameInRange(frameRef.current);
+
+ const durationInFrames = inFrame
+ ? outFrame
+ ? outFrame - inFrame + 1
+ : config.durationInFrames - inFrame
+ : outFrame
+ ? outFrame + 1
+ : config.durationInFrames;
const stop = () => {
hasBeenStopped = true;
@@ -36,7 +63,8 @@ export const usePlayback = ({loop}: {loop: boolean}) => {
const callback = () => {
const time = performance.now() - startedTime;
- const nextFrame = Math.round(time / (1000 / config.fps)) + startedFrame;
+ const nextFrame =
+ Math.round(time / (1000 / config.fps)) + startedFrame - (inFrame ?? 0);
if (nextFrame === config.durationInFrames && !loop) {
stop();
pause();
@@ -44,7 +72,8 @@ export const usePlayback = ({loop}: {loop: boolean}) => {
return;
}
- const actualNextFrame = nextFrame % config.durationInFrames;
+ const actualNextFrame = (nextFrame % durationInFrames) + (inFrame ?? 0);
+
if (actualNextFrame !== frameRef.current) {
setFrame(actualNextFrame);
}
@@ -59,7 +88,7 @@ export const usePlayback = ({loop}: {loop: boolean}) => {
return () => {
stop();
};
- }, [config, loop, pause, playing, setFrame, emitter]);
+ }, [config, loop, pause, playing, setFrame, emitter, inFrame, outFrame]);
useEffect(() => {
const interval = setInterval(() => {