Skip to content

🎥 Make videos programmatically with React

less floating point inaccuracy

Jonny Burger439c62973ec9
diff --git a/packages/lambda/src/functions/launch.ts b/packages/lambda/src/functions/launch.ts
index dbc5ae7bbd..3e30473a72 100644
--- a/packages/lambda/src/functions/launch.ts
+++ b/packages/lambda/src/functions/launch.ts
@@ -288,7 +288,7 @@ const innerLaunchHandler = async ({
deleteAfter: params.deleteAfter,
colorSpace: params.colorSpace,
preferLossless: params.preferLossless,
- compositionStart: realFrameRange[0] / fps,
+ compositionStart: realFrameRange[0],
};
return payload;
});
@@ -411,7 +411,7 @@ const innerLaunchHandler = async ({
framesPerLambda,
binariesDirectory: null,
preferLossless: params.preferLossless,
- compositionStart: realFrameRange[0] / fps,
+ compositionStart: realFrameRange[0],
});
return postRenderData;
diff --git a/packages/renderer/src/get-extra-frames-to-capture.ts b/packages/renderer/src/get-extra-frames-to-capture.ts
index 679efcbbe3..5c03df0b7b 100644
--- a/packages/renderer/src/get-extra-frames-to-capture.ts
+++ b/packages/renderer/src/get-extra-frames-to-capture.ts
@@ -31,7 +31,7 @@ export const getExtraFramesToCapture = ({
};
}
- const chunkStart = realFrameRange[0] / fps;
+ const chunkStart = realFrameRange[0];
if (chunkStart < compositionStart) {
throw new Error('chunkStart may not be below compositionStart');
@@ -43,25 +43,24 @@ export const getExtraFramesToCapture = ({
}
const realRightEnd =
- realLeftEnd + (realFrameRange[1] - realFrameRange[0] + 1) / fps;
+ realLeftEnd + (realFrameRange[1] - realFrameRange[0] + 1);
const aacAdjustedLeftEnd = Math.max(
0,
- getClosestAlignedTime(realLeftEnd) - 2 * (1024 / DEFAULT_SAMPLE_RATE),
+ getClosestAlignedTime(realLeftEnd / fps) - 2 * (1024 / DEFAULT_SAMPLE_RATE),
);
const aacAdjustedRightEnd =
- getClosestAlignedTime(realRightEnd) + 2 * (1024 / DEFAULT_SAMPLE_RATE);
+ getClosestAlignedTime(realRightEnd / fps) +
+ 2 * (1024 / DEFAULT_SAMPLE_RATE);
const startTimeWithoutOffset = Math.floor(aacAdjustedLeftEnd * fps);
- // TODO: Prevent floating point issues by dividing and then multiplying by FPS
- const startFrame = startTimeWithoutOffset + compositionStart * fps;
+ const startFrame = startTimeWithoutOffset + compositionStart;
const trimLeftOffset =
(aacAdjustedLeftEnd * fps - startTimeWithoutOffset) / fps;
- const endFrame =
- Math.ceil(aacAdjustedRightEnd * fps) + compositionStart * fps;
+ const endFrame = Math.ceil(aacAdjustedRightEnd * fps) + compositionStart;
const trimRightOffset =
(aacAdjustedRightEnd * fps - Math.ceil(aacAdjustedRightEnd * fps)) / fps;
diff --git a/packages/renderer/src/test/extra-frames-to-capture.test.ts b/packages/renderer/src/test/extra-frames-to-capture.test.ts
index 8c05aaf238..768a9b2b42 100644
--- a/packages/renderer/src/test/extra-frames-to-capture.test.ts
+++ b/packages/renderer/src/test/extra-frames-to-capture.test.ts
@@ -15,7 +15,7 @@ test('Extra frames to capture 0', () => {
test('Extra frames to capture 1', () => {
const {extraFramesToCaptureAssets} = getExtraFramesToCapture({
fps: 30,
- compositionStart: 3.3333333333333335,
+ compositionStart: 100,
realFrameRange: [100, 116],
forSeamlessAacConcatenation: true,
});
@@ -26,7 +26,7 @@ test('Extra frames to capture 1', () => {
test('Extra frames to capture 2', () => {
const {extraFramesToCaptureAssets} = getExtraFramesToCapture({
fps: 30,
- compositionStart: 3.3333333333333335,
+ compositionStart: 100,
realFrameRange: [151, 167],
forSeamlessAacConcatenation: true,
});
@@ -42,7 +42,7 @@ test('Extra frames to capture 3', () => {
chunkLengthInSeconds,
} = getExtraFramesToCapture({
fps: 30,
- compositionStart: 3.3333333333333335,
+ compositionStart: 100,
realFrameRange: [134, 150],
forSeamlessAacConcatenation: true,
});