Skip to content

🎥 Make videos programmatically with React

isolate FpsCounter

jeetiss86e2c62d41c8
diff --git a/packages/cli/src/editor/components/FpsCounter.tsx b/packages/cli/src/editor/components/FpsCounter.tsx
index a1f2d0dc69..36c6e0b661 100644
--- a/packages/cli/src/editor/components/FpsCounter.tsx
+++ b/packages/cli/src/editor/components/FpsCounter.tsx
@@ -1,38 +1,68 @@
-import React, {useEffect, useMemo, useState} from 'react';
+import React, {useEffect, useLayoutEffect, useRef, useState} from 'react';
import {Internals} from 'remotion';
import styled from 'styled-components';
-import {getLastFrames} from '../state/last-frames';
const Label = styled.div`
color: white;
font-family: Arial, Helvetica, sans-serif;
`;
+const pushWithMaxSize = (arr: number[], value: number, maxSize: number): number[] => {
+ arr.push(value);
+ return arr.slice(-maxSize);
+};
+
export const FpsCounter: React.FC = () => {
- const [now, setNow] = useState(0);
const videoConfig = Internals.useUnsafeVideoConfig();
+ const [playing] = Internals.Timeline.usePlayingState();
+ const frame = Internals.Timeline.useTimelinePosition();
+
+ const [marker, rerender] = useState({});
+ const [fps, setFps] = useState(0);
+
+ const previousUpdates = useRef<number[]>([]);
+ const fpsRef = useRef<number>(0);
+ const playingRef = useRef<boolean>(playing);
+
+ useLayoutEffect(() => {
+ fpsRef.current = 0;
+ previousUpdates.current = [];
+ playingRef.current = playing;
+ }, [playing]);
+
+ useLayoutEffect(() => {
+ if (playingRef.current === false) return;
+
+ previousUpdates.current = pushWithMaxSize(
+ previousUpdates.current,
+ performance.now(),
+ 15
+ );
+
+ if (previousUpdates.current.length < 2) return;
+
+ const diff =
+ Math.max(...previousUpdates.current) -
+ Math.min(...previousUpdates.current);
+ const averageDistanceBetween = diff / (previousUpdates.current.length - 1);
+ fpsRef.current = 1000 / averageDistanceBetween;
+
+ if (previousUpdates.current.length == 2) setFps(fpsRef.current);
+ }, [frame]);
useEffect(() => {
const t = setTimeout(() => {
- setNow(Date.now());
+ rerender({});
+ setFps(fpsRef.current);
}, 1000);
return () => clearTimeout(t);
- }, [now]);
-
- const lastFrames = getLastFrames();
+ }, [marker]);
- const diff = Math.max(...lastFrames) - Math.min(...lastFrames);
- const averageDistanceBetween = diff / (lastFrames.length - 1);
- const fps = 1000 / averageDistanceBetween;
-
- const style = useMemo((): React.CSSProperties => {
- if (!videoConfig) {
- return {};
- }
- return {color: fps < videoConfig.fps * 0.9 ? 'red' : 'white'};
- }, [fps, videoConfig]);
+ if (fps === 0) {
+ return null;
+ }
- if (lastFrames.length === 0) {
+ if (playing === false) {
return null;
}
@@ -40,5 +70,6 @@ export const FpsCounter: React.FC = () => {
return null;
}
- return <Label style={style}>{String(fps.toFixed(1))} FPS</Label>;
+ const style = {color: fps < videoConfig.fps * 0.9 ? 'red' : 'white'};
+ return <Label style={style}>{fps.toFixed(1)} FPS</Label>;
};
diff --git a/packages/cli/src/editor/components/PlayPause.tsx b/packages/cli/src/editor/components/PlayPause.tsx
index bbb0de0b41..46df3eb960 100644
--- a/packages/cli/src/editor/components/PlayPause.tsx
+++ b/packages/cli/src/editor/components/PlayPause.tsx
@@ -4,7 +4,6 @@ import {Pause} from '../icons/pause';
import {Play} from '../icons/play';
import {StepBack} from '../icons/step-back';
import {StepForward} from '../icons/step-forward';
-import {getLastFrames, setLastFrames} from '../state/last-frames';
import {ControlButton} from './ControlButton';
export const PlayPause: React.FC = () => {
@@ -19,9 +18,6 @@ export const PlayPause: React.FC = () => {
return null;
}
setPlaying((p) => {
- if (p) {
- setLastFrames([]);
- }
return !p;
});
}, [video, setPlaying]);
@@ -103,7 +99,6 @@ export const PlayPause: React.FC = () => {
(Math.round(time / (1000 / config.fps)) + startedFrame) %
config.durationInFrames;
if (calculatedFrame !== frameRef.current) {
- setLastFrames([...getLastFrames(), Date.now()]);
setFrame(calculatedFrame);
}
if (!hasBeenStopped) {
diff --git a/packages/cli/src/editor/state/last-frames.ts b/packages/cli/src/editor/state/last-frames.ts
deleted file mode 100644
index 75f68d8100..0000000000
--- a/packages/cli/src/editor/state/last-frames.ts
+++ /dev/null
@@ -1,9 +0,0 @@
-let lastFrames: number[] = [];
-
-export const setLastFrames = (n: number[]) => {
- lastFrames = n;
- lastFrames = lastFrames.slice(Math.max(lastFrames.length - 15, 0));
-};
-export const getLastFrames = () => {
- return lastFrames;
-};