Skip to content

🎥 Make videos programmatically with React

fix accelerated video after URL timestamp hashes were implemented

Jonny Burger1f20c978ad15
diff --git a/packages/core/src/video/VideoForDevelopment.tsx b/packages/core/src/video/VideoForDevelopment.tsx
index 43ab497938..46bbf52c4c 100644
--- a/packages/core/src/video/VideoForDevelopment.tsx
+++ b/packages/core/src/video/VideoForDevelopment.tsx
@@ -22,7 +22,7 @@ import {
useMediaVolumeState,
} from '../volume-position-state';
import type {RemotionVideoProps} from './props';
-import {appendVideoFragment} from './video-fragment';
+import {useAppendVideoFragment} from './video-fragment';
type VideoForDevelopmentProps = RemotionVideoProps & {
onlyWarnForMediaSeekingError: boolean;
@@ -96,7 +96,7 @@ const VideoForDevelopmentRefForwardingFunction: React.ForwardRefRenderFunction<
? Math.min(parentSequence.durationInFrames, durationInFrames)
: durationInFrames;
- const actualSrc = appendVideoFragment({
+ const actualSrc = useAppendVideoFragment({
actualSrc: usePreload(src as string),
actualFrom,
duration,
diff --git a/packages/core/src/video/video-fragment.ts b/packages/core/src/video/video-fragment.ts
index 00d9e617a0..87b341d019 100644
--- a/packages/core/src/video/video-fragment.ts
+++ b/packages/core/src/video/video-fragment.ts
@@ -1,7 +1,52 @@
+import {useRef} from 'react';
+
const toSeconds = (time: number, fps: number) => {
return Math.round((time / fps) * 100) / 100;
};
+const isSubsetOfDuration = (
+ prevStartFrom: number,
+ newStartFrom: number,
+ prevDuration: number,
+ newDuration: number
+) => {
+ return (
+ prevStartFrom <= newStartFrom &&
+ prevStartFrom + prevDuration >= newStartFrom + newDuration
+ );
+};
+
+export const useAppendVideoFragment = ({
+ actualSrc: initialActualSrc,
+ actualFrom: initialActualFrom,
+ duration: initialDuration,
+ fps,
+}: {
+ actualSrc: string;
+ actualFrom: number;
+ duration: number;
+ fps: number;
+}) => {
+ const actualFromRef = useRef(initialActualFrom);
+ const actualDuration = useRef(initialDuration);
+ const actualSrc = useRef(initialActualSrc);
+
+ if (!isSubsetOfDuration || initialActualSrc !== actualSrc.current) {
+ actualFromRef.current = initialActualFrom;
+ actualDuration.current = initialDuration;
+ actualSrc.current = initialActualSrc;
+ }
+
+ const appended = appendVideoFragment({
+ actualSrc: actualSrc.current,
+ actualFrom: actualFromRef.current,
+ duration: actualDuration.current,
+ fps,
+ });
+
+ return appended;
+};
+
export const appendVideoFragment = ({
actualSrc,
actualFrom,