From 04a13efd6a16385eaf5ea5d374da4cf733dd8090 Mon Sep 17 00:00:00 2001 From: Walker Date: Wed, 11 Feb 2026 11:12:14 -1000 Subject: [PATCH 1/2] refactor: simplify thumbnail URL step --- src/primitives/thumbnails.ts | 42 +++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/primitives/thumbnails.ts b/src/primitives/thumbnails.ts index 60f288f2..8d9487e5 100644 --- a/src/primitives/thumbnails.ts +++ b/src/primitives/thumbnails.ts @@ -30,37 +30,39 @@ export async function getThumbnailUrls( ): Promise { "use step"; const { interval = 10, width = 640, shouldSign = false, maxSamples, credentials } = options; - let timestamps: number[] = []; + const timestamps: number[] = []; + // Calculate expected number of timestamps before generating + let expectedCount: number; if (duration <= 50) { - const spacing = duration / 6; - for (let i = 1; i <= 5; i++) { - timestamps.push(Math.round(i * spacing)); - } + expectedCount = 5; } else { - for (let time = 0; time < duration; time += interval) { - timestamps.push(time); - } + expectedCount = Math.floor(duration / interval) + 1; } - // Apply maxSamples cap if specified and we have more timestamps than the limit - if (maxSamples !== undefined && timestamps.length > maxSamples) { - const newTimestamps: number[] = []; + // If maxSamples is set and would be exceeded, generate evenly-spaced samples directly + if (maxSamples !== undefined && expectedCount > maxSamples) { + timestamps.push(0); // Always include first frame - // Always include first frame - newTimestamps.push(0); - - // If maxSamples >= 2, add evenly distributed middle frames and last frame if (maxSamples >= 2) { const spacing = duration / (maxSamples - 1); for (let i = 1; i < maxSamples - 1; i++) { - newTimestamps.push(spacing * i); + timestamps.push(spacing * i); + } + timestamps.push(duration); // Always include last frame + } + } else { + // Generate timestamps based on duration and interval + if (duration <= 50) { + const spacing = duration / 6; + for (let i = 1; i <= 5; i++) { + timestamps.push(Math.round(i * spacing)); + } + } else { + for (let time = 0; time < duration; time += interval) { + timestamps.push(time); } - // Always include last frame - newTimestamps.push(duration); } - - timestamps = newTimestamps; } const baseUrl = `https://image.mux.com/${playbackId}/thumbnail.png`; From 98b5894ae7cf7b76257289858b228b23af78ad8a Mon Sep 17 00:00:00 2001 From: Walker Date: Wed, 11 Feb 2026 11:30:02 -1000 Subject: [PATCH 2/2] add hardcode --- src/primitives/thumbnails.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/primitives/thumbnails.ts b/src/primitives/thumbnails.ts index 8d9487e5..fed144dd 100644 --- a/src/primitives/thumbnails.ts +++ b/src/primitives/thumbnails.ts @@ -1,6 +1,9 @@ import { signUrl } from "@mux/ai/lib/url-signing"; import type { WorkflowCredentialsInput } from "@mux/ai/types"; +const MIN_DURATION = 50; +const MIN_SAMPLES = 2; + export interface ThumbnailOptions { /** Interval between thumbnails in seconds (default: 10) */ interval?: number; @@ -34,7 +37,7 @@ export async function getThumbnailUrls( // Calculate expected number of timestamps before generating let expectedCount: number; - if (duration <= 50) { + if (duration <= MIN_DURATION) { expectedCount = 5; } else { expectedCount = Math.floor(duration / interval) + 1; @@ -44,7 +47,7 @@ export async function getThumbnailUrls( if (maxSamples !== undefined && expectedCount > maxSamples) { timestamps.push(0); // Always include first frame - if (maxSamples >= 2) { + if (maxSamples >= MIN_SAMPLES) { const spacing = duration / (maxSamples - 1); for (let i = 1; i < maxSamples - 1; i++) { timestamps.push(spacing * i); @@ -53,7 +56,7 @@ export async function getThumbnailUrls( } } else { // Generate timestamps based on duration and interval - if (duration <= 50) { + if (duration <= MIN_DURATION) { const spacing = duration / 6; for (let i = 1; i <= 5; i++) { timestamps.push(Math.round(i * spacing));