-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
@remotion/renderer: More sound calculation to ensure even dimensions for H.264 #5715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
); | ||
actualWidth = Math.round(actualWidth); | ||
let heightEvenDimensions = height; | ||
while (Math.round(heightEvenDimensions * scale) % 2 !== 0) { |
There was a problem hiding this comment.
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.
No description provided.