Skip to content

Commit 7700915

Browse files
authored
Merge pull request #5715 from remotion-dev/sound-scale-calculation
2 parents e1d1012 + 33e6083 commit 7700915

File tree

5 files changed

+73
-68
lines changed

5 files changed

+73
-68
lines changed

packages/eslint-plugin/src/rules/even-dimensions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type Options = [];
99
type MessageIds = 'EvenDimensions';
1010

1111
const EvenDimensions =
12-
"Videos rendered in H264/H265 codec do not support dimensions that are not divisible by 2. Make the number even to resolve this warning. Ignore this warning if you don't plan on rendering this video with a H264 or H265 codec.";
12+
"Videos rendered in H264/H265 codec do not support dimensions that are not divisible by 2. Remotion will round down your dimensions to satisfy this constraint. Ignore this warning if you don't plan on rendering this video with a H264 or H265 codec.";
1313

1414
export default createRule<Options, MessageIds>({
1515
name: 'even-dimensions',

packages/renderer/src/render-media.ts

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -443,33 +443,19 @@ const internalRenderMediaRaw = ({
443443
onCtrlCExit(`Delete ${workingDir}`, () => deleteDirectory(workingDir));
444444
}
445445

446-
const {
447-
actualWidth: widthEvenDimensionsUndivided,
448-
actualHeight: heightEvenDimensionsUndivided,
449-
} = validateEvenDimensionsWithCodec({
450-
codec,
451-
height: compositionWithPossibleUnevenDimensions.height,
452-
scale,
453-
width: compositionWithPossibleUnevenDimensions.width,
454-
wantsImageSequence: false,
455-
indent,
456-
logLevel,
457-
});
458-
459-
const heightEvenDimensions = Math.round(
460-
heightEvenDimensionsUndivided / scale,
461-
);
462-
const widthEvenDimensions = Math.round(widthEvenDimensionsUndivided / scale);
446+
const {actualWidth: widthEvenDimensions, actualHeight: heightEvenDimensions} =
447+
validateEvenDimensionsWithCodec({
448+
codec,
449+
height: compositionWithPossibleUnevenDimensions.height,
450+
scale,
451+
width: compositionWithPossibleUnevenDimensions.width,
452+
wantsImageSequence: false,
453+
indent,
454+
logLevel,
455+
});
456+
const actualWidth = widthEvenDimensions * scale;
457+
const actualHeight = heightEvenDimensions * scale;
463458

464-
const {actualWidth, actualHeight} = validateEvenDimensionsWithCodec({
465-
codec,
466-
height: compositionWithPossibleUnevenDimensions.height,
467-
scale,
468-
width: compositionWithPossibleUnevenDimensions.width,
469-
wantsImageSequence: false,
470-
indent,
471-
logLevel,
472-
});
473459
const composition = {
474460
...compositionWithPossibleUnevenDimensions,
475461
height: heightEvenDimensions,
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {expect, test} from 'bun:test';
2+
import {validateEvenDimensionsWithCodec} from '../validate-even-dimensions-with-codec';
3+
4+
test('should eventually result in even dimensions', () => {
5+
const scale = 2.3275862069;
6+
const {actualWidth, actualHeight} = validateEvenDimensionsWithCodec({
7+
codec: 'h264',
8+
width: 464,
9+
height: 832,
10+
indent: false,
11+
logLevel: 'info',
12+
scale,
13+
wantsImageSequence: false,
14+
});
15+
expect(actualHeight).toBe(831);
16+
expect(actualWidth).toBe(464);
17+
expect(Math.round(actualHeight * scale)).toBe(1934);
18+
expect(Math.round(actualWidth * scale)).toBe(1080);
19+
});
20+
21+
test('default case', () => {
22+
const scale = 2;
23+
const {actualWidth, actualHeight} = validateEvenDimensionsWithCodec({
24+
codec: 'h264',
25+
width: 464,
26+
height: 832,
27+
indent: false,
28+
logLevel: 'info',
29+
scale,
30+
wantsImageSequence: false,
31+
});
32+
expect(actualHeight).toBe(832);
33+
expect(actualWidth).toBe(464);
34+
expect(Math.round(actualHeight * scale)).toBe(1664);
35+
expect(Math.round(actualWidth * scale)).toBe(928);
36+
});

packages/renderer/src/test/validate-even-dimensions-with-codec.test.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('validateEvenDimensionsWithCodec', () => {
99
};
1010

1111
describe('H264/H265 codec behavior with odd dimensions', () => {
12-
test('should round down odd width to even when using H264', () => {
12+
test.only('should round down odd width to even when using H264', () => {
1313
const result = validateEvenDimensionsWithCodec({
1414
width: 4000,
1515
height: 2592,
@@ -18,8 +18,8 @@ describe('validateEvenDimensionsWithCodec', () => {
1818
...defaultConfig,
1919
});
2020

21-
expect(result.actualWidth).toBe(1400); // 4000 * 0.35 = 1400 (even)
22-
expect(result.actualHeight).toBe(906); // 2592 * 0.35 = 907.2 -> 907 (odd) -> 906 (even)
21+
expect(result.actualWidth).toBe(4000); // 4000 * 0.35 = 1400 (even)
22+
expect(result.actualHeight).toBe(2590); // 2592 * 0.35 = 907.2 -> 907 (odd) -> 906 (even)
2323
});
2424

2525
test('should round down odd width to even when using H265', () => {
@@ -111,8 +111,8 @@ describe('validateEvenDimensionsWithCodec', () => {
111111
...defaultConfig,
112112
});
113113

114-
expect(result.actualWidth).toBe(500);
115-
expect(result.actualHeight).toBe(300);
114+
expect(result.actualWidth).toBe(1000);
115+
expect(result.actualHeight).toBe(600);
116116
});
117117
});
118118

@@ -185,8 +185,9 @@ describe('validateEvenDimensionsWithCodec', () => {
185185
});
186186

187187
// 1000 * 0.333 = 333, which is odd, should become 332
188-
expect(result.actualWidth).toBe(332);
189-
expect(result.actualHeight).toBe(332);
188+
// 998 * 0.3333 -> 332.334 will be rounded down
189+
expect(result.actualWidth).toBe(998);
190+
expect(result.actualHeight).toBe(998);
190191
});
191192
});
192193
});

packages/renderer/src/validate-even-dimensions-with-codec.ts

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@ export const validateEvenDimensionsWithCodec = ({
1919
indent: boolean;
2020
logLevel: LogLevel;
2121
}) => {
22-
let actualWidth = width * scale;
23-
let actualHeight = height * scale;
2422
if (wantsImageSequence) {
2523
return {
26-
actualWidth,
27-
actualHeight,
24+
actualWidth: width,
25+
actualHeight: height,
2826
};
2927
}
3028

@@ -35,53 +33,37 @@ export const validateEvenDimensionsWithCodec = ({
3533
codec !== 'h264-ts'
3634
) {
3735
return {
38-
actualWidth,
39-
actualHeight,
36+
actualWidth: width,
37+
actualHeight: height,
4038
};
4139
}
4240

43-
if (
44-
actualWidth % 1 !== 0 &&
45-
(actualWidth % 1 < 0.005 || actualWidth % 1 > 0.005)
46-
) {
47-
Log.verbose(
48-
{indent, logLevel},
49-
`Rounding width to an even number from ${actualWidth} to ${Math.round(actualWidth)}`,
50-
);
51-
actualWidth = Math.round(actualWidth);
41+
let heightEvenDimensions = height;
42+
while (Math.round(heightEvenDimensions * scale) % 2 !== 0) {
43+
heightEvenDimensions--;
5244
}
5345

54-
if (
55-
actualHeight % 1 !== 0 &&
56-
(actualHeight % 1 < 0.005 || actualHeight % 1 > 0.005)
57-
) {
58-
Log.verbose(
59-
{indent, logLevel},
60-
`Rounding height to an even number from ${actualHeight} to ${Math.round(actualHeight)}`,
61-
);
62-
actualHeight = Math.round(actualHeight);
46+
let widthEvenDimensions = width;
47+
while (Math.round(widthEvenDimensions * scale) % 2 !== 0) {
48+
widthEvenDimensions--;
6349
}
6450

65-
const displayName = codec === 'h265' ? 'H265' : 'H264';
66-
67-
if (actualWidth % 2 !== 0) {
51+
if (widthEvenDimensions !== width) {
6852
Log.verbose(
6953
{indent, logLevel},
70-
`Rounding width down to an even number from ${actualWidth} to ${actualWidth - 1} for ${displayName} codec compatibility`,
54+
`Rounding width to an even number from ${width} to ${widthEvenDimensions}`,
7155
);
72-
actualWidth -= 1;
7356
}
7457

75-
if (actualHeight % 2 !== 0) {
58+
if (heightEvenDimensions !== height) {
7659
Log.verbose(
7760
{indent, logLevel},
78-
`Rounding height down to an even number from ${actualHeight} to ${actualHeight - 1} for ${displayName} codec compatibility`,
61+
`Rounding height to an even number from ${height} to ${heightEvenDimensions}`,
7962
);
80-
actualHeight -= 1;
8163
}
8264

8365
return {
84-
actualWidth,
85-
actualHeight,
66+
actualWidth: widthEvenDimensions,
67+
actualHeight: heightEvenDimensions,
8668
};
8769
};

0 commit comments

Comments
 (0)