From 47e1f76b9da4cfdc0b6a6e3914a63daed0864f00 Mon Sep 17 00:00:00 2001 From: Alex Patterson Date: Thu, 5 Mar 2026 09:41:45 -0500 Subject: [PATCH] fix: cap Pexels b-roll at 1080p to prevent Lambda disk overflow MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit selectBestFile() was falling back to the highest resolution file when no exact 1080p match existed. This selected 4K UHD files (4096×2160, 100-500MB) that exceeded Lambda's 2GB disk limit, causing all render chunks to fail. Now prefers the highest resolution at or below 1080p. If all files exceed 1080p, picks the smallest available instead of the largest. --- lib/services/pexels.ts | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/services/pexels.ts b/lib/services/pexels.ts index a08d2f11..cbadd894 100644 --- a/lib/services/pexels.ts +++ b/lib/services/pexels.ts @@ -149,7 +149,8 @@ function sleep(ms: number): Promise { * orientation. * * Prefers HD resolution (1920×1080 for landscape, 1080×1920 for portrait). - * Falls back to the file with the highest total pixel count. + * Falls back to the closest resolution at or below the target to avoid + * sending massive 4K files to Lambda (which has limited disk space). * * @param video - The Pexels video to pick a file from. * @param orientation - Desired orientation. @@ -179,11 +180,25 @@ function selectBestFile( const hdWidthMatch = files.find((f) => f.width === targetWidth); if (hdWidthMatch) return hdWidthMatch; - // Fall back to the highest resolution file available + // Fall back to the best file at or below target resolution. + // This avoids selecting 4K UHD files (4096×2160, 100-500MB) that + // exceed Lambda's 2GB disk limit. + const atOrBelowTarget = files.filter((f) => f.width <= targetWidth); + + if (atOrBelowTarget.length > 0) { + // Pick the highest resolution that's still at or below target + return atOrBelowTarget.reduce((best, current) => { + const bestPixels = best.width * best.height; + const currentPixels = current.width * current.height; + return currentPixels > bestPixels ? current : best; + }); + } + + // All files exceed target resolution — pick the smallest one return files.reduce((best, current) => { const bestPixels = best.width * best.height; const currentPixels = current.width * current.height; - return currentPixels > bestPixels ? current : best; + return currentPixels < bestPixels ? current : best; }); }