Skip to content

Conversation

JonnyBurger
Copy link
Member

No description provided.

Copy link
Contributor

vercel bot commented Oct 3, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
bugs Ready Ready Preview Comment Oct 5, 2025 0:32am
remotion Ready Ready Preview Comment Oct 5, 2025 0:32am

);
actualWidth = Math.round(actualWidth);
let heightEvenDimensions = height;
while (Math.round(heightEvenDimensions * scale) % 2 !== 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The while loop algorithm can potentially produce zero or negative dimensions when the scale factor is small, which would result in invalid video dimensions.

View Details
📝 Patch Details
diff --git a/packages/renderer/src/validate-even-dimensions-with-codec.ts b/packages/renderer/src/validate-even-dimensions-with-codec.ts
index 55b27343ee..3043c6b7fe 100644
--- a/packages/renderer/src/validate-even-dimensions-with-codec.ts
+++ b/packages/renderer/src/validate-even-dimensions-with-codec.ts
@@ -39,14 +39,34 @@ export const validateEvenDimensionsWithCodec = ({
 	}
 
 	let heightEvenDimensions = height;
-	while (Math.round(heightEvenDimensions * scale) % 2 !== 0) {
+	while (Math.round(heightEvenDimensions * scale) % 2 !== 0 && heightEvenDimensions > 1) {
 		heightEvenDimensions--;
 	}
+	// If we can't find a dimension that produces an even scaled result, find the smallest that does
+	if (Math.round(heightEvenDimensions * scale) % 2 !== 0) {
+		// Search upward from 2 to find the first dimension that produces an even scaled result
+		for (let dim = 2; dim <= Math.max(height, 10); dim++) {
+			if (Math.round(dim * scale) % 2 === 0) {
+				heightEvenDimensions = dim;
+				break;
+			}
+		}
+	}
 
 	let widthEvenDimensions = width;
-	while (Math.round(widthEvenDimensions * scale) % 2 !== 0) {
+	while (Math.round(widthEvenDimensions * scale) % 2 !== 0 && widthEvenDimensions > 1) {
 		widthEvenDimensions--;
 	}
+	// If we can't find a dimension that produces an even scaled result, find the smallest that does
+	if (Math.round(widthEvenDimensions * scale) % 2 !== 0) {
+		// Search upward from 2 to find the first dimension that produces an even scaled result
+		for (let dim = 2; dim <= Math.max(width, 10); dim++) {
+			if (Math.round(dim * scale) % 2 === 0) {
+				widthEvenDimensions = dim;
+				break;
+			}
+		}
+	}
 
 	if (widthEvenDimensions !== width) {
 		Log.verbose(

Analysis

Algorithm can produce zero dimensions with small scale factors

What fails: validateEvenDimensionsWithCodec() in packages/renderer/src/validate-even-dimensions-with-codec.ts decrements dimensions without bounds checking, resulting in zero width or height when the scale factor is small

How to reproduce:

validateEvenDimensionsWithCodec({
  width: 1, height: 100, scale: 0.6, codec: 'h264', 
  wantsImageSequence: false, indent: false, logLevel: 'info'
})
// Returns: { actualWidth: 0, actualHeight: 100 }

Result: Zero width dimension returned, which would create invalid video files that cannot be encoded by H.264

Expected: All dimensions should remain above zero. Algorithm should find valid dimensions that produce even scaled results without going to zero, as confirmed by Stack Overflow documentation that H.264 requires even dimensions but has no documented minimum above zero.

@vercel vercel bot temporarily deployed to Preview – remotion October 5, 2025 12:11 Inactive
@JonnyBurger JonnyBurger merged commit 7700915 into main Oct 5, 2025
11 of 14 checks passed
@JonnyBurger JonnyBurger deleted the sound-scale-calculation branch October 5, 2025 12:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using scale option leading to uneven dimensions still leads to crashes even in latest version
1 participant