Skip to content

🎥 Make videos programmatically with React

instead of clamping to startTimestamp, introduce tolerance of 0.1s

Igor Samokhovets9a7f0f9390f1
diff --git a/packages/media/src/video-extraction/keyframe-bank.ts b/packages/media/src/video-extraction/keyframe-bank.ts
index 1f2b1e1503..04182f3668 100644
--- a/packages/media/src/video-extraction/keyframe-bank.ts
+++ b/packages/media/src/video-extraction/keyframe-bank.ts
@@ -140,17 +140,32 @@ export const makeKeyframeBank = ({
): Promise<VideoSample | null> => {
lastUsed = Date.now();
- // If the requested timestamp is before the start of this bank, clamp it to the start. This handles videos that don't start at timestamp 0 (issue #5915)
+ // Videos may start slightly after timestamp 0 due to encoding, but if the requested timestamp is too far before the bank start, something is likely wrong.
+ const maxClampToleranceInSeconds = 0.1;
+
+ // If the requested timestamp is before the start of this bank, clamp it to the start if within tolerance. This handles videos that don't start at timestamp 0.
+
// For example, requesting frame at 0sec when video starts at 0.04sec should return the frame at 0.04sec.
- const adjustedTimestamp = Math.max(
- timestampInSeconds,
- startTimestampInSeconds,
- );
+ let adjustedTimestamp = timestampInSeconds;
+
+ if (timestampInSeconds < startTimestampInSeconds) {
+ const differenceInSeconds = startTimestampInSeconds - timestampInSeconds;
+
+ if (differenceInSeconds <= maxClampToleranceInSeconds) {
+ adjustedTimestamp = startTimestampInSeconds;
+ } else {
+ return Promise.reject(
+ new Error(
+ `Timestamp is before start timestamp (requested: ${timestampInSeconds}sec, start: ${startTimestampInSeconds}sec, difference: ${differenceInSeconds.toFixed(3)}sec exceeds tolerance of ${maxClampToleranceInSeconds}sec)`,
+ ),
+ );
+ }
+ }
if (adjustedTimestamp > endTimestampInSeconds) {
return Promise.reject(
new Error(
- `Timestamp is after end timestamp (requested: ${timestampInSeconds}sec, end: ${endTimestampInSeconds})`,
+ `Timestamp is after end timestamp (requested: ${timestampInSeconds}sec, end: ${endTimestampInSeconds}sec)`,
),
);
}