Skip to content

🎥 Make videos programmatically with React

differentiate between tag, not file extension when seeking and default to mp4 behavior

Jonny Burger82373a4e1c64
diff --git a/packages/core/src/test/get-current-time.test.ts b/packages/core/src/test/get-current-time.test.ts
index 218a343a44..fd195db16e 100644
--- a/packages/core/src/test/get-current-time.test.ts
+++ b/packages/core/src/test/get-current-time.test.ts
@@ -13,6 +13,7 @@ describe('correctly calculate media time of video element', () => {
playbackRate: 1,
src: 'video.mp4',
startFrom: 0,
+ mediaType: 'video',
})
).toBeCloseTo(1, mp4Precision);
});
@@ -25,6 +26,7 @@ describe('correctly calculate media time of video element', () => {
playbackRate: 2,
src: 'video.mp4',
startFrom: 0,
+ mediaType: 'video',
})
).toBeCloseTo(2, mp4Precision);
});
@@ -38,6 +40,7 @@ describe('correctly calculate media time of video element', () => {
playbackRate: 2,
src: 'video.mp4',
startFrom: 30,
+ mediaType: 'video',
})
).toBeCloseTo(3, mp4Precision);
});
@@ -52,6 +55,7 @@ describe('correctly calculate media time of video element', () => {
playbackRate: 1,
src: 'video.webm',
startFrom: 0,
+ mediaType: 'video',
})
).toBeCloseTo(1, webmPrecision);
});
@@ -64,6 +68,7 @@ describe('correctly calculate media time of video element', () => {
playbackRate: 2,
src: 'video.webm',
startFrom: 0,
+ mediaType: 'video',
})
).toBeCloseTo(2, webmPrecision);
});
@@ -77,6 +82,7 @@ describe('correctly calculate media time of video element', () => {
playbackRate: 2,
src: 'video.webm',
startFrom: 30,
+ mediaType: 'video',
})
).toBeCloseTo(3, webmPrecision);
});
diff --git a/packages/core/src/use-media-playback.ts b/packages/core/src/use-media-playback.ts
index 3b6a4806ec..dbf6f4df73 100644
--- a/packages/core/src/use-media-playback.ts
+++ b/packages/core/src/use-media-playback.ts
@@ -56,6 +56,7 @@ export const useMediaPlayback = ({
src,
playbackRate: localPlaybackRate,
startFrom: -mediaStartsAt,
+ mediaType,
});
const isTime = mediaRef.current.currentTime;
diff --git a/packages/core/src/video/VideoForRendering.tsx b/packages/core/src/video/VideoForRendering.tsx
index 4a6abdf16e..ca7b2b4104 100644
--- a/packages/core/src/video/VideoForRendering.tsx
+++ b/packages/core/src/video/VideoForRendering.tsx
@@ -112,6 +112,7 @@ const VideoForRenderingForwardFunction: React.ForwardRefRenderFunction<
src: props.src as string,
playbackRate: playbackRate || 1,
startFrom: -mediaStartsAt,
+ mediaType: 'video',
});
})();
const handle = delayRender(`Rendering <Video /> with src="${props.src}"`);
diff --git a/packages/core/src/video/get-current-time.ts b/packages/core/src/video/get-current-time.ts
index be7d42bc5b..ef9946fbb0 100644
--- a/packages/core/src/video/get-current-time.ts
+++ b/packages/core/src/video/get-current-time.ts
@@ -24,12 +24,14 @@ export const getMediaTime = ({
src,
playbackRate,
startFrom,
+ mediaType,
}: {
fps: number;
frame: number;
src: string;
playbackRate: number;
startFrom: number;
+ mediaType: 'video' | 'audio';
}) => {
const expectedFrame = getExpectedMediaFrameUncorrected({
frame,
@@ -37,13 +39,6 @@ export const getMediaTime = ({
startFrom,
});
- if (src.endsWith('mp4')) {
- // In Chrome, for MP4s, if 30fps, the first frame is still displayed at 0.033333
- // even though after that it increases by 0.033333333 each.
- // So frame = 0 in Remotion is like frame = 1 for the browser
- return (expectedFrame + 1) / fps;
- }
-
if (src.endsWith('webm')) {
// For WebM videos, we need to add a little bit of shift to get the right frame.
const msPerFrame = 1000 / fps;
@@ -51,6 +46,13 @@ export const getMediaTime = ({
return (expectedFrame * msPerFrame + msShift) / 1000;
}
+ if (mediaType === 'video') {
+ // In Chrome, for MP4s, if 30fps, the first frame is still displayed at 0.033333
+ // even though after that it increases by 0.033333333 each.
+ // So frame = 0 in Remotion is like frame = 1 for the browser
+ return (expectedFrame + 1) / fps;
+ }
+
// For audio, we don't do any shift correction
return expectedFrame / fps;
};