@@ -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