Skip to content

🎥 Make videos programmatically with React

get current fps

Jonny Burgerf201b16ad26e
diff --git a/packages/cli/src/editor/components/PlayPause.tsx b/packages/cli/src/editor/components/PlayPause.tsx
index 749de22453..884dad6bad 100644
--- a/packages/cli/src/editor/components/PlayPause.tsx
+++ b/packages/cli/src/editor/components/PlayPause.tsx
@@ -1,6 +1,5 @@
import {PlayerInternals} from '@remotion/player';
import React, {useCallback, useEffect} from 'react';
-import {Internals} from 'remotion';
import {useIsStill} from '../helpers/is-current-selected-still';
import {useKeybinding} from '../helpers/use-keybinding';
import {JumpToStart} from '../icons/jump-to-start';
@@ -10,7 +9,11 @@ import {StepBack} from '../icons/step-back';
import {StepForward} from '../icons/step-forward';
import {useTimelineInOutFramePosition} from '../state/in-out';
import {ControlButton} from './ControlButton';
-import {getCurrentFrame} from './Timeline/imperative-state';
+import {
+ getCurrentDuration,
+ getCurrentFps,
+ getCurrentFrame,
+} from './Timeline/imperative-state';
import {ensureFrameIsInViewport} from './Timeline/timeline-scroll-logic';
const forwardBackStyle = {
@@ -23,9 +26,7 @@ export const PlayPause: React.FC<{
loop: boolean;
}> = ({playbackRate, loop}) => {
const {inFrame, outFrame} = useTimelineInOutFramePosition();
- const frame = Internals.Timeline.useTimelinePosition();
- const video = Internals.useVideo();
PlayerInternals.usePlayback({
loop,
playbackRate,
@@ -43,6 +44,7 @@ export const PlayPause: React.FC<{
seek,
frameForward,
isLastFrame,
+ isFirstFrame,
} = PlayerInternals.usePlayer();
const isStill = useIsStill();
@@ -77,82 +79,67 @@ export const PlayPause: React.FC<{
[pauseAndReturnToPlayStart, playing]
);
- const videoFps = video?.fps ?? null;
- const durationInFrames = video?.durationInFrames ?? null;
-
const onArrowLeft = useCallback(
(e: KeyboardEvent) => {
- if (!videoFps) {
- return null;
- }
-
- if (durationInFrames === null) {
- return null;
- }
-
e.preventDefault();
if (e.altKey) {
seek(0);
ensureFrameIsInViewport({
direction: 'fit-left',
- durationInFrames,
+ durationInFrames: getCurrentDuration(),
frame: 0,
});
} else if (e.shiftKey) {
- frameBack(videoFps);
+ frameBack(getCurrentFps());
ensureFrameIsInViewport({
direction: 'fit-left',
- durationInFrames,
- frame: Math.max(0, getCurrentFrame() - videoFps),
+ durationInFrames: getCurrentDuration(),
+ frame: Math.max(0, getCurrentFrame() - getCurrentFps()),
});
} else {
frameBack(1);
ensureFrameIsInViewport({
direction: 'fit-left',
- durationInFrames,
+ durationInFrames: getCurrentDuration(),
frame: Math.max(0, getCurrentFrame() - 1),
});
}
},
- [durationInFrames, frameBack, seek, videoFps]
+ [frameBack, seek]
);
const onArrowRight = useCallback(
(e: KeyboardEvent) => {
- if (!video) {
- return null;
- }
-
if (e.altKey) {
- seek(video.durationInFrames - 1);
+ seek(getCurrentDuration() - 1);
ensureFrameIsInViewport({
direction: 'fit-right',
- durationInFrames: video.durationInFrames - 1,
- frame: video.durationInFrames - 1,
+ durationInFrames: getCurrentDuration() - 1,
+ frame: getCurrentDuration() - 1,
});
} else if (e.shiftKey) {
- frameForward(video.fps);
+ frameForward(getCurrentFps());
ensureFrameIsInViewport({
direction: 'fit-right',
- durationInFrames: video.durationInFrames,
+ durationInFrames: getCurrentDuration(),
frame: Math.min(
- video.durationInFrames - 1,
- getCurrentFrame() + video.fps
+ getCurrentDuration() - 1,
+ getCurrentFrame() + getCurrentFps()
),
});
} else {
frameForward(1);
ensureFrameIsInViewport({
direction: 'fit-right',
- durationInFrames: video.durationInFrames,
+ durationInFrames: getCurrentDuration(),
frame: getCurrentFrame() + 1,
});
}
e.preventDefault();
},
- [frameForward, seek, video]
+ [frameForward, seek]
);
const oneFrameBack = useCallback(() => {
@@ -168,12 +155,8 @@ export const PlayPause: React.FC<{
}, [seek]);
const jumpToEnd = useCallback(() => {
- if (!video) {
- return;
- }
-
- seek(video.durationInFrames - 1);
- }, [seek, video]);
+ seek(getCurrentDuration() - 1);
+ }, [seek]);
const keybindings = useKeybinding();
@@ -242,7 +225,7 @@ export const PlayPause: React.FC<{
<ControlButton
aria-label="Jump to beginning"
title="Jump to beginning"
- disabled={frame === 0}
+ disabled={isFirstFrame}
onClick={jumpToStart}
>
<JumpToStart style={forwardBackStyle} />
@@ -250,7 +233,7 @@ export const PlayPause: React.FC<{
<ControlButton
aria-label="Step back one frame"
title="Step back one frame"
- disabled={frame === 0}
+ disabled={isFirstFrame}
onClick={oneFrameBack}
>
<StepBack style={forwardBackStyle} />
@@ -259,7 +242,6 @@ export const PlayPause: React.FC<{
<ControlButton
aria-label={playing ? 'Pause' : 'Play'}
title={playing ? 'Pause' : 'Play'}
- disabled={!video}
onClick={playing ? pause : play}
>
{playing ? (
diff --git a/packages/cli/src/editor/components/Timeline/TimelinePlayCursorSyncer.tsx b/packages/cli/src/editor/components/Timeline/TimelinePlayCursorSyncer.tsx
index f86f349694..1001bece1b 100644
--- a/packages/cli/src/editor/components/Timeline/TimelinePlayCursorSyncer.tsx
+++ b/packages/cli/src/editor/components/Timeline/TimelinePlayCursorSyncer.tsx
@@ -7,6 +7,7 @@ import {
getCurrentFrame,
getCurrentZoom,
setCurrentDuration,
+ setCurrentFps,
setCurrentFrame,
setCurrentZoom,
} from './imperative-state';
@@ -35,6 +36,7 @@ export const TimelinePlayCursorSyncer: React.FC = () => {
setCurrentFrame(timelineContext.frame);
setCurrentZoom(zoom);
setCurrentDuration(video?.durationInFrames ?? 1);
+ setCurrentFps(video?.fps ?? 1);
const playing = timelineContext.playing ?? false;
diff --git a/packages/cli/src/editor/components/Timeline/imperative-state.ts b/packages/cli/src/editor/components/Timeline/imperative-state.ts
index d0dbff280d..c85293b8a1 100644
--- a/packages/cli/src/editor/components/Timeline/imperative-state.ts
+++ b/packages/cli/src/editor/components/Timeline/imperative-state.ts
@@ -1,6 +1,7 @@
export let currentFrame = 0;
export let currentZoom = 1;
export let currentDuration = 1;
+export let currentFps = 1;
export const getCurrentZoom = () => {
return currentZoom;
@@ -25,3 +26,11 @@ export const getCurrentDuration = () => {
export const setCurrentDuration = (d: number) => {
currentDuration = d;
};
+
+export const getCurrentFps = () => {
+ return currentFps;
+};
+
+export const setCurrentFps = (d: number) => {
+ currentFps = d;
+};
diff --git a/packages/player/src/use-player.ts b/packages/player/src/use-player.ts
index 4e6b8140c2..686247d1a3 100644
--- a/packages/player/src/use-player.ts
+++ b/packages/player/src/use-player.ts
@@ -8,6 +8,7 @@ type UsePlayerMethods = {
frameBack: (frames: number) => void;
frameForward: (frames: number) => void;
isLastFrame: boolean;
+ isFirstFrame: boolean;
emitter: PlayerEmitter;
playing: boolean;
play: (e?: SyntheticEvent) => void;
@@ -36,6 +37,7 @@ export const usePlayer = (): UsePlayerMethods => {
const lastFrame = (config?.durationInFrames ?? 1) - 1;
const isLastFrame = frame === lastFrame;
+ const isFirstFrame = frame === 0;
if (!emitter) {
throw new TypeError('Expected Player event emitter context');
@@ -151,22 +153,23 @@ export const usePlayer = (): UsePlayerMethods => {
play,
pause,
seek,
-
+ isFirstFrame,
getCurrentFrame: () => frameRef.current as number,
isPlaying: () => imperativePlaying.current as boolean,
pauseAndReturnToPlayStart,
};
}, [
- emitter,
frameBack,
frameForward,
- imperativePlaying,
isLastFrame,
- pause,
- play,
+ emitter,
playing,
- pauseAndReturnToPlayStart,
+ play,
+ pause,
seek,
+ isFirstFrame,
+ pauseAndReturnToPlayStart,
+ imperativePlaying,
]);
return returnValue;