Skip to content

Commit 47e1f76

Browse files
committed
fix: cap Pexels b-roll at 1080p to prevent Lambda disk overflow
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.
1 parent 35fb02c commit 47e1f76

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

lib/services/pexels.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ function sleep(ms: number): Promise<void> {
149149
* orientation.
150150
*
151151
* Prefers HD resolution (1920×1080 for landscape, 1080×1920 for portrait).
152-
* Falls back to the file with the highest total pixel count.
152+
* Falls back to the closest resolution at or below the target to avoid
153+
* sending massive 4K files to Lambda (which has limited disk space).
153154
*
154155
* @param video - The Pexels video to pick a file from.
155156
* @param orientation - Desired orientation.
@@ -179,11 +180,25 @@ function selectBestFile(
179180
const hdWidthMatch = files.find((f) => f.width === targetWidth);
180181
if (hdWidthMatch) return hdWidthMatch;
181182

182-
// Fall back to the highest resolution file available
183+
// Fall back to the best file at or below target resolution.
184+
// This avoids selecting 4K UHD files (4096×2160, 100-500MB) that
185+
// exceed Lambda's 2GB disk limit.
186+
const atOrBelowTarget = files.filter((f) => f.width <= targetWidth);
187+
188+
if (atOrBelowTarget.length > 0) {
189+
// Pick the highest resolution that's still at or below target
190+
return atOrBelowTarget.reduce((best, current) => {
191+
const bestPixels = best.width * best.height;
192+
const currentPixels = current.width * current.height;
193+
return currentPixels > bestPixels ? current : best;
194+
});
195+
}
196+
197+
// All files exceed target resolution — pick the smallest one
183198
return files.reduce((best, current) => {
184199
const bestPixels = best.width * best.height;
185200
const currentPixels = current.width * current.height;
186-
return currentPixels > bestPixels ? current : best;
201+
return currentPixels < bestPixels ? current : best;
187202
});
188203
}
189204

0 commit comments

Comments
 (0)