Skip to content

🎥 Make videos programmatically with React

add playbackrate

Anik Dhabal Babu7a8b2f176f0e
diff --git a/packages/docs/docs/gif/gif.md b/packages/docs/docs/gif/gif.md
index a52a7da63e..77542ed56f 100644
--- a/packages/docs/docs/gif/gif.md
+++ b/packages/docs/docs/gif/gif.md
@@ -75,6 +75,16 @@ The looping behavior of the GIF. Can be one of these values:
You can add a [React ref](https://react.dev/learn/manipulating-the-dom-with-refs) to `<Gif>`. If you use TypeScript, you need to type it with `HTMLCanvasElement`.
+### playbackRate
+
+The `playbackRate` prop controls the playback speed of the GIF animation within your Remotion video. It enables you to adjust how fast or slow the GIF animation plays, allowing for precise synchronization with your video content.
+
+Default: 1 (Normal speed)
+Values:
+- `1`: Plays the GIF at normal speed.
+- `< 1`: Slows down the GIF speed (e.g., 0.5 plays it at half speed).
+- `> 1:` Speeds up the GIF speed (e.g., 2.0 plays it at double speed).
+
## Example
```tsx twoslash
@@ -94,6 +104,7 @@ export const MyComponent: React.FC = () => {
width={width}
height={height}
fit="fill"
+ playbackRate={2} {/* Play the GIF at double speed */}
/>
);
};
diff --git a/packages/gif/src/GifForDevelopment.tsx b/packages/gif/src/GifForDevelopment.tsx
index 38c4356cb2..d7a803702a 100644
--- a/packages/gif/src/GifForDevelopment.tsx
+++ b/packages/gif/src/GifForDevelopment.tsx
@@ -19,6 +19,7 @@ export const GifForDevelopment = forwardRef<
height,
onError,
loopBehavior = 'loop',
+ playbackRate = 1,
onLoad,
fit = 'fill',
...props
@@ -104,7 +105,7 @@ export const GifForDevelopment = forwardRef<
);
}
- const index = useCurrentGifIndex(state.delays, loopBehavior);
+ const index = useCurrentGifIndex(state.delays, loopBehavior,playbackRate);
if (index === -1) {
return null;
diff --git a/packages/gif/src/GifForRendering.tsx b/packages/gif/src/GifForRendering.tsx
index 019e3a483d..3ef9a5b576 100644
--- a/packages/gif/src/GifForRendering.tsx
+++ b/packages/gif/src/GifForRendering.tsx
@@ -17,6 +17,7 @@ export const GifForRendering = forwardRef<HTMLCanvasElement, RemotionGifProps>(
onLoad,
onError,
loopBehavior = 'loop',
+ playbackRate = 1,
fit = 'fill',
...props
},
@@ -49,7 +50,7 @@ export const GifForRendering = forwardRef<HTMLCanvasElement, RemotionGifProps>(
};
}, [id]);
- const index = useCurrentGifIndex(state.delays, loopBehavior);
+ const index = useCurrentGifIndex(state.delays, loopBehavior, playbackRate);
const currentOnLoad = useRef(onLoad);
const currentOnError = useRef(onError);
currentOnLoad.current = onLoad;
diff --git a/packages/gif/src/props.ts b/packages/gif/src/props.ts
index 01f5b77647..42331d6813 100644
--- a/packages/gif/src/props.ts
+++ b/packages/gif/src/props.ts
@@ -12,9 +12,11 @@ export type RemotionGifProps = {
height: number;
delays: number[];
frames: ImageData[];
+
}) => void;
onError?: (error: Error) => void;
fit?: GifFillMode;
+ playbackRate?: number;
style?: React.CSSProperties;
loopBehavior?: GifLoopBehavior;
id?: string;
diff --git a/packages/gif/src/useCurrentGifIndex.tsx b/packages/gif/src/useCurrentGifIndex.tsx
index c882960334..8538b35aa4 100644
--- a/packages/gif/src/useCurrentGifIndex.tsx
+++ b/packages/gif/src/useCurrentGifIndex.tsx
@@ -4,7 +4,8 @@ import type {GifLoopBehavior} from './props';
export function useCurrentGifIndex(
delays: number[],
- loopBehavior: GifLoopBehavior
+ loopBehavior: GifLoopBehavior,
+ playbackRate = 1
): number {
const currentFrame = useCurrentFrame();
const videoConfig = useVideoConfig();
@@ -24,7 +25,9 @@ export function useCurrentGifIndex(
return 0;
}
- const time = (currentFrame / videoConfig.fps) * 1000;
+ const updatedFrame = (currentFrame / playbackRate);
+
+ const time = (updatedFrame / videoConfig.fps) * 1000;
if (loopBehavior === 'pause-after-finish' && time >= duration) {
return delays.length - 1;