Skip to content

🎥 Make videos programmatically with React

fix timing offset

JonnyBurgerbab28a7cd056
diff --git a/packages/media/src/audio/audio-for-rendering.tsx b/packages/media/src/audio/audio-for-rendering.tsx
index 811f9edd9b..00bb382a5e 100644
--- a/packages/media/src/audio/audio-for-rendering.tsx
+++ b/packages/media/src/audio/audio-for-rendering.tsx
@@ -110,6 +110,7 @@ export const AudioForRendering: React.FC<AudioProps> = ({
audioStreamIndex: audioStreamIndex ?? 0,
endAt: trimAfter,
startFrom: trimBefore,
+ fps,
})
.then((result) => {
if (result.type === 'unknown-container-format') {
diff --git a/packages/media/src/extract-frame-and-audio.ts b/packages/media/src/extract-frame-and-audio.ts
index c0a309b040..e3fc9cd520 100644
--- a/packages/media/src/extract-frame-and-audio.ts
+++ b/packages/media/src/extract-frame-and-audio.ts
@@ -15,6 +15,7 @@ export const extractFrameAndAudio = async ({
audioStreamIndex,
endAt,
startFrom,
+ fps,
}: {
src: string;
timeInSeconds: number;
@@ -27,6 +28,7 @@ export const extractFrameAndAudio = async ({
audioStreamIndex: number;
endAt: number | undefined;
startFrom: number | undefined;
+ fps: number;
}): Promise<ExtractFrameViaBroadcastChannelResult> => {
try {
const [frame, audio] = await Promise.all([
@@ -39,6 +41,7 @@ export const extractFrameAndAudio = async ({
endAt,
playbackRate,
startFrom,
+ fps,
})
: null,
includeAudio
diff --git a/packages/media/src/test/browser.test.ts b/packages/media/src/test/browser.test.ts
index d6b8170bfc..c366901371 100644
--- a/packages/media/src/test/browser.test.ts
+++ b/packages/media/src/test/browser.test.ts
@@ -18,6 +18,7 @@ test('Should be able to extract a frame', async () => {
audioStreamIndex: 0,
endAt: undefined,
startFrom: undefined,
+ fps: 30,
});
if (result.type === 'cannot-decode') {
@@ -67,6 +68,7 @@ test('Should be able to extract the last frame', async () => {
audioStreamIndex: 0,
endAt: undefined,
startFrom: undefined,
+ fps: 30,
});
if (result.type === 'cannot-decode') {
@@ -107,6 +109,7 @@ test('Should manage the cache', async () => {
audioStreamIndex: 0,
endAt: undefined,
startFrom: undefined,
+ fps: 30,
});
}
@@ -130,6 +133,7 @@ test('Should be apply volume correctly', async () => {
audioStreamIndex: 0,
endAt: undefined,
startFrom: undefined,
+ fps: 30,
});
if (result.type === 'cannot-decode') {
@@ -177,6 +181,7 @@ test('Should be able to loop', async () => {
audioStreamIndex: 0,
endAt: undefined,
startFrom: undefined,
+ fps: 30,
});
if (result.type === 'cannot-decode') {
diff --git a/packages/media/src/video-extraction/extract-frame-via-broadcast-channel.ts b/packages/media/src/video-extraction/extract-frame-via-broadcast-channel.ts
index 18b7cbd543..c7678d7f9d 100644
--- a/packages/media/src/video-extraction/extract-frame-via-broadcast-channel.ts
+++ b/packages/media/src/video-extraction/extract-frame-via-broadcast-channel.ts
@@ -16,6 +16,7 @@ type ExtractFrameRequest = {
audioStreamIndex: number;
endAt: number | undefined;
startFrom: number | undefined;
+ fps: number;
};
type ExtractFrameResponse =
@@ -65,6 +66,7 @@ if (window.remotion_broadcastChannel && window.remotion_isMainTab) {
audioStreamIndex: data.audioStreamIndex,
endAt: data.endAt,
startFrom: data.startFrom,
+ fps: data.fps,
});
if (result.type === 'cannot-decode') {
@@ -160,6 +162,7 @@ export const extractFrameViaBroadcastChannel = ({
audioStreamIndex,
endAt,
startFrom,
+ fps,
}: {
src: string;
timeInSeconds: number;
@@ -173,6 +176,7 @@ export const extractFrameViaBroadcastChannel = ({
audioStreamIndex: number;
endAt: number | undefined;
startFrom: number | undefined;
+ fps: number;
}): Promise<ExtractFrameViaBroadcastChannelResult> => {
if (isClientSideRendering || window.remotion_isMainTab) {
return extractFrameAndAudio({
@@ -187,6 +191,7 @@ export const extractFrameViaBroadcastChannel = ({
audioStreamIndex,
endAt,
startFrom,
+ fps,
});
}
@@ -291,6 +296,7 @@ export const extractFrameViaBroadcastChannel = ({
audioStreamIndex,
endAt,
startFrom,
+ fps,
};
window.remotion_broadcastChannel!.postMessage(request);
diff --git a/packages/media/src/video-extraction/extract-frame.ts b/packages/media/src/video-extraction/extract-frame.ts
index eef2d308d8..cbc24bf353 100644
--- a/packages/media/src/video-extraction/extract-frame.ts
+++ b/packages/media/src/video-extraction/extract-frame.ts
@@ -20,6 +20,7 @@ const getTimeInSeconds = ({
endAt,
startFrom,
playbackRate,
+ fps,
}: {
loop: boolean;
mediaDurationInSeconds: number | null;
@@ -28,6 +29,7 @@ const getTimeInSeconds = ({
endAt: number | undefined;
startFrom: number | undefined;
playbackRate: number;
+ fps: number;
}) => {
if (!loop) {
return unloopedTimeinSeconds;
@@ -39,12 +41,13 @@ const getTimeInSeconds = ({
);
}
- const loopDuration = Internals.calculateLoopDuration({
- endAt,
- mediaDurationInFrames: mediaDurationInSeconds,
- playbackRate,
- startFrom,
- });
+ const loopDuration =
+ Internals.calculateLoopDuration({
+ endAt,
+ mediaDurationInFrames: mediaDurationInSeconds * fps,
+ playbackRate,
+ startFrom,
+ }) / fps;
const timeInSeconds = unloopedTimeinSeconds % loopDuration;
return timeInSeconds + (startFrom ?? 0);
@@ -58,6 +61,7 @@ export const extractFrame = async ({
endAt,
startFrom,
playbackRate,
+ fps,
}: {
src: string;
timeInSeconds: number;
@@ -66,6 +70,7 @@ export const extractFrame = async ({
endAt: number | undefined;
startFrom: number | undefined;
playbackRate: number;
+ fps: number;
}): Promise<ExtractFrameResult> => {
const sink = await getSinkWeak(src, logLevel);
@@ -99,6 +104,7 @@ export const extractFrame = async ({
endAt,
playbackRate,
startFrom,
+ fps,
});
const keyframeBank = await keyframeManager.requestKeyframeBank({
diff --git a/packages/media/src/video/video-for-rendering.tsx b/packages/media/src/video/video-for-rendering.tsx
index cc015a1443..c261069874 100644
--- a/packages/media/src/video/video-for-rendering.tsx
+++ b/packages/media/src/video/video-for-rendering.tsx
@@ -158,6 +158,7 @@ export const VideoForRendering: React.FC<InnerVideoProps> = ({
audioStreamIndex,
endAt: trimAfterValue,
startFrom: trimBeforeValue,
+ fps,
})
.then((result) => {
if (result.type === 'unknown-container-format') {