Skip to content

Commit 2d46566

Browse files
committed
fix(engine): keep near-even crop-pack siblings 1:1 and size docker listing from the master
When even-rounding trims only one pixel, crop straight to the output size and drop the scale filter so siblings keep the master's pixels 1:1. The docker render path now builds its crop-pack listing with planCropPackMembersForMaster, so `--resolution` renders report the siblings' real size. A failed probe falls back to the composition size.
1 parent 4717506 commit 2d46566

3 files changed

Lines changed: 68 additions & 33 deletions

File tree

‎packages/cli/src/commands/render.ts‎

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,11 @@ import {
9797
runPostRenderStepAsync,
9898
} from "../utils/render-success-state.js";
9999
import type { ProducerLogger, RenderJob } from "@hyperframes/producer";
100-
import { EXTRACT_CACHE_DIR_DISABLED_ALIASES, type VideoFrameFormat } from "@hyperframes/engine";
100+
import {
101+
EXTRACT_CACHE_DIR_DISABLED_ALIASES,
102+
planCropPackMembersForMaster,
103+
type VideoFrameFormat,
104+
} from "@hyperframes/engine";
101105
import {
102106
checkOutputResolutionCompatibility,
103107
suggestMatchingPreset,
@@ -106,7 +110,6 @@ import {
106110
type OutputResolutionIssueKind,
107111
type Fps,
108112
} from "@hyperframes/core";
109-
import { cropPackOutputPath, outputSizeForCrop, pixelRect } from "@hyperframes/parsers/safe-frames";
110113

111114
export default defineCommand({
112115
meta: {
@@ -781,6 +784,10 @@ async function renderDocker(
781784
// the exit code.
782785
markRenderSucceeded();
783786

787+
// Sized from the master's probed dimensions, as the producer plans them, so
788+
// `--resolution` renders report the siblings' real size.
789+
const cropPack = await cropPackListing(outputPath, options.cropPack);
790+
784791
// Track metrics (no job object available from Docker — use a minimal stub)
785792
runPostRenderStep("trackRenderComplete", () =>
786793
trackRenderComplete({
@@ -792,7 +799,7 @@ async function renderDocker(
792799
gpu: options.gpu,
793800
authoringSkill: options.authoringSkill,
794801
catalogUsage: options.catalogUsage,
795-
cropPack: cropPackListing(outputPath, options.cropPack),
802+
cropPack,
796803
...getMemorySnapshot(),
797804
}),
798805
);
@@ -801,22 +808,15 @@ async function renderDocker(
801808
// threaded back here; the summary shows render time only (never a wrong video
802809
// length). Probe the output with ffprobe if a duration figure is wanted here.
803810
runPostRenderStep("printRenderComplete", () =>
804-
printRenderComplete(
805-
outputPath,
806-
elapsed,
807-
options.quiet,
808-
undefined,
809-
undefined,
810-
cropPackListing(outputPath, options.cropPack),
811-
),
811+
printRenderComplete(outputPath, elapsed, options.quiet, undefined, undefined, cropPack),
812812
);
813813
runPostRenderStep("warnIfWebmAlphaDropped", () =>
814814
warnIfWebmAlphaDropped(outputPath, options.format, options.quiet),
815815
);
816816
if (options.exitAfterComplete) scheduleRenderProcessExit();
817817
return {
818818
renderTimeMs: elapsed,
819-
cropPack: cropPackListing(outputPath, options.cropPack),
819+
cropPack,
820820
};
821821
}
822822

@@ -1620,21 +1620,23 @@ function trackRenderMetrics(
16201620
});
16211621
}
16221622

1623-
function cropPackListing(
1623+
async function cropPackListing(
16241624
masterPath: string,
16251625
spec: RenderOptions["cropPack"] | undefined,
1626-
): SingleRenderResult["cropPack"] {
1626+
): Promise<SingleRenderResult["cropPack"]> {
16271627
if (!spec || spec.members.length === 0) return undefined;
1628-
return spec.members.map((frame) => {
1629-
const crop = pixelRect(frame, spec.compositionWidth, spec.compositionHeight);
1630-
const size = outputSizeForCrop(crop, frame.ratio);
1631-
return {
1632-
id: frame.id,
1633-
outputPath: cropPackOutputPath(masterPath, frame.id),
1634-
width: size.width,
1635-
height: size.height,
1636-
};
1637-
});
1628+
const plans = await planCropPackMembersForMaster(
1629+
spec.members,
1630+
spec.compositionWidth,
1631+
spec.compositionHeight,
1632+
masterPath,
1633+
);
1634+
return plans.map((plan) => ({
1635+
id: plan.id,
1636+
outputPath: plan.outputPath,
1637+
width: plan.outputWidth,
1638+
height: plan.outputHeight,
1639+
}));
16381640
}
16391641

16401642
function printRenderComplete(

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ describe("buildCropPackArgs", () => {
5353

5454
it("crops in -filter:v without the master's range conversion", () => {
5555
const args = buildCropPackArgs({ ...base, encode: mp4Encode });
56-
expect(valueOf(args, "-filter:v")).toBe("crop=608:1080:656:0,scale=608:1080");
56+
expect(valueOf(args, "-filter:v")).toBe("crop=608:1080:656:0");
5757
expect(args).not.toContain("-vf");
5858
expect(valueOf(args, "-c:a")).toBe("copy");
5959
expect(args).toContain("-map_metadata");
@@ -174,14 +174,14 @@ 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", () => {
177+
it("crops an odd rect straight to the even output size, without a resample", () => {
178178
const [member] = planCropPackMembers(
179179
[{ id: "square", ratio: "1:1", x: 0, y: 0, width: (0.123 * 1080) / 1920, height: 0.123 }],
180180
1920,
181181
1080,
182182
"renders/launch.mp4",
183183
);
184-
expect(member?.crop).toEqual({ x: 0, y: 0, width: 133, height: 133 });
184+
expect(member?.crop).toEqual({ x: 0, y: 0, width: 132, height: 132 });
185185
expect(member?.outputWidth).toBe(132);
186186
expect(member?.outputHeight).toBe(132);
187187
const args = buildCropPackArgs({
@@ -193,7 +193,31 @@ describe("planCropPackMembersForMaster", () => {
193193
copyAudio: true,
194194
encode: mp4Encode,
195195
});
196-
expect(args[args.indexOf("-filter:v") + 1]).toBe("crop=133:133:0:0,scale=132:132");
196+
expect(args[args.indexOf("-filter:v") + 1]).toBe("crop=132:132:0:0");
197+
});
198+
199+
it("still scales when the output is smaller than the crop by 2px or more", () => {
200+
const args = buildCropPackArgs({
201+
masterPath: "renders/launch.mp4",
202+
outputPath: "renders/launch.vertical.mp4",
203+
crop: { x: 656, y: 0, width: 608, height: 1080 },
204+
outputWidth: 304,
205+
outputHeight: 540,
206+
copyAudio: true,
207+
encode: mp4Encode,
208+
});
209+
expect(args[args.indexOf("-filter:v") + 1]).toBe("crop=608:1080:656:0,scale=304:540");
210+
});
211+
212+
it("falls back to the composition size when the probe fails", async () => {
213+
extractVideoMetadataMock.mockRejectedValue(new Error("ffprobe failed"));
214+
const members = await planCropPackMembersForMaster(
215+
[vertical],
216+
1920,
217+
1080,
218+
"renders/launch.mp4",
219+
);
220+
expect(members[0]?.crop).toEqual({ x: 656, y: 0, width: 608, height: 1080 });
197221
});
198222

199223
it("falls back to the composition size when the probe has no dimensions", async () => {

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,12 @@ export function planCropPackMembers(
5353
masterPath: string,
5454
): CropPackMemberPlan[] {
5555
return frames.map((frame) => {
56-
const crop = pixelRect(frame, compositionWidth, compositionHeight);
57-
const output = outputSizeForCrop(crop, frame.ratio);
56+
const pixel = pixelRect(frame, compositionWidth, compositionHeight);
57+
const output = outputSizeForCrop(pixel, frame.ratio);
58+
// When even-rounding only trimmed a pixel, crop to the output size so the
59+
// sibling keeps the master's pixels 1:1 instead of being resampled.
60+
const nearlyEqual = pixel.width - output.width < 2 && pixel.height - output.height < 2;
61+
const crop = nearlyEqual ? { ...pixel, width: output.width, height: output.height } : pixel;
5862
return {
5963
id: frame.id,
6064
crop,
@@ -76,8 +80,8 @@ export async function planCropPackMembersForMaster(
7680
compositionHeight: number,
7781
masterPath: string,
7882
): Promise<CropPackMemberPlan[]> {
79-
const metadata = await extractVideoMetadata(masterPath);
80-
const sized = metadata.width > 0 && metadata.height > 0;
83+
const metadata = await extractVideoMetadata(masterPath).catch(() => undefined);
84+
const sized = metadata !== undefined && metadata.width > 0 && metadata.height > 0;
8185
return planCropPackMembers(
8286
frames,
8387
sized ? metadata.width : compositionWidth,
@@ -103,7 +107,12 @@ function alphaDecoderArgs(encode: CropPackEncodeSettings): string[] {
103107
* Audio is copied when requested; chapter metadata via `-map_metadata 0`.
104108
*/
105109
export function buildCropPackArgs(input: BuildCropPackArgsInput): string[] {
106-
const filter = `crop=${input.crop.width}:${input.crop.height}:${input.crop.x}:${input.crop.y},scale=${input.outputWidth}:${input.outputHeight}`;
110+
const cropFilter = `crop=${input.crop.width}:${input.crop.height}:${input.crop.x}:${input.crop.y}`;
111+
const unscaled =
112+
input.crop.width === input.outputWidth && input.crop.height === input.outputHeight;
113+
const filter = unscaled
114+
? cropFilter
115+
: `${cropFilter},scale=${input.outputWidth}:${input.outputHeight}`;
107116
let args = buildEncoderArgs(
108117
{ ...input.encode, width: input.outputWidth, height: input.outputHeight, useGpu: false },
109118
[...alphaDecoderArgs(input.encode), "-i", input.masterPath, "-filter:v", filter],

0 commit comments

Comments
 (0)