Skip to content

Commit 4717506

Browse files
committed
fix(parsers): round crop-pack output size down to even
The crop pass does not carry the master's even-dimension pad, so an odd planned size (a 1:1 frame of height 0.123 on 1080p plans 133x133) made libx264 with yuv420p refuse the sibling. outputSizeForCrop now floors both sides to even, and the crop filter scales to that size.
1 parent 574d32d commit 4717506

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

‎packages/engine/src/utils/cropPack.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,28 @@ describe("planCropPackMembersForMaster", () => {
174174
expect(members[0]?.outputHeight).toBe(2160);
175175
});
176176

177+
it("scales an odd crop to an even output size for 4:2:0 encoders", () => {
178+
const [member] = planCropPackMembers(
179+
[{ id: "square", ratio: "1:1", x: 0, y: 0, width: (0.123 * 1080) / 1920, height: 0.123 }],
180+
1920,
181+
1080,
182+
"renders/launch.mp4",
183+
);
184+
expect(member?.crop).toEqual({ x: 0, y: 0, width: 133, height: 133 });
185+
expect(member?.outputWidth).toBe(132);
186+
expect(member?.outputHeight).toBe(132);
187+
const args = buildCropPackArgs({
188+
masterPath: "renders/launch.mp4",
189+
outputPath: "renders/launch.square.mp4",
190+
crop: member!.crop,
191+
outputWidth: member!.outputWidth,
192+
outputHeight: member!.outputHeight,
193+
copyAudio: true,
194+
encode: mp4Encode,
195+
});
196+
expect(args[args.indexOf("-filter:v") + 1]).toBe("crop=133:133:0:0,scale=132:132");
197+
});
198+
177199
it("falls back to the composition size when the probe has no dimensions", async () => {
178200
extractVideoMetadataMock.mockResolvedValue({ width: 0, height: 0 });
179201
const members = await planCropPackMembersForMaster(

‎packages/parsers/src/safeFrames.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ describe("snap and output size", () => {
123123
expect(outputSizeForCrop(crop, "9:16")).toEqual({ x: 0, y: 0, width: 844, height: 1080 });
124124
});
125125

126+
it("rounds an odd crop down to even output dimensions", () => {
127+
const crop = { x: 0, y: 0, width: 133, height: 133 };
128+
expect(outputSizeForCrop(crop, "1:1")).toEqual({ x: 0, y: 0, width: 132, height: 132 });
129+
});
130+
126131
it("returns null when the ratio is more than one pixel off", () => {
127132
expect(snapPixelSizeToRatio(844, 1080, "9:16")).toBeNull();
128133
});

‎packages/parsers/src/safeFrames.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ export function tryPixelRect(frame: SafeFrame, width: number, height: number): P
141141
* Largest integer output that fits `ratio` and does not upscale past the
142142
* cropped source. A 844×1080 crop from a 1920×1080 9:16 window stays
143143
* 844×1080 — cropping is not letterboxing or a second capture.
144+
*
145+
* Both sides round down to even: 4:2:0 encoders (libx264 with yuv420p)
146+
* refuse odd dimensions, and the crop pass has no pad like the master's.
144147
*/
145148
export function outputSizeForCrop(crop: PixelRect, ratio: SafeFrameRatio): PixelRect {
146149
const snapped = snapPixelSizeToRatio(crop.width, crop.height, ratio);
@@ -149,11 +152,15 @@ export function outputSizeForCrop(crop: PixelRect, ratio: SafeFrameRatio): Pixel
149152
return {
150153
x: 0,
151154
y: 0,
152-
width: Math.min(width, crop.width),
153-
height: Math.min(height, crop.height),
155+
width: floorToEven(Math.min(width, crop.width)),
156+
height: floorToEven(Math.min(height, crop.height)),
154157
};
155158
}
156159

160+
function floorToEven(value: number): number {
161+
return Math.max(2, Math.floor(value / 2) * 2);
162+
}
163+
157164
/**
158165
* Parse `data-safe-frames` JSON. When `dimensions` are given, pixel sizes
159166
* must match the named ratio after snap.

0 commit comments

Comments
 (0)