Skip to content

Commit 6d5961b

Browse files
committed
feat(cli): expose agent-native color grading
1 parent e446de6 commit 6d5961b

11 files changed

Lines changed: 821 additions & 179 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {
2+
analyzeMediaGrade,
3+
type MediaTreatmentAnalysis,
4+
} from "../../../../skills/media-use/scripts/lib/grade-analyzer.mjs";
5+
import { findFFmpeg, findFFprobe, getFFmpegInstallHint } from "../browser/ffmpeg.js";
6+
7+
interface CliMediaTreatmentAnalysis extends Omit<MediaTreatmentAnalysis, "adjust"> {
8+
suggestedPatch: { adjust: Record<string, number> };
9+
}
10+
11+
export function analyzeMediaTreatment(mediaPath: string): CliMediaTreatmentAnalysis {
12+
const ffmpegPath = findFFmpeg();
13+
const ffprobePath = findFFprobe();
14+
if (!ffmpegPath || !ffprobePath) {
15+
throw new Error(`FFmpeg and ffprobe are required. ${getFFmpegInstallHint()}`);
16+
}
17+
const analysis = analyzeMediaGrade(mediaPath, { ffmpegPath, ffprobePath });
18+
const { adjust, ...evidence } = analysis;
19+
return { ...evidence, suggestedPatch: { adjust } };
20+
}

‎packages/cli/src/commands/media-treatment.test.ts‎

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
getMediaTreatmentCapabilityDetail,
1313
getMediaTreatmentCapabilityOverview,
1414
mediaTreatmentCommand,
15+
resolveMediaTreatmentSource,
1516
} from "./media-treatment.js";
1617
import { CliRuntimeError } from "../utils/commandResult.js";
1718

@@ -22,6 +23,9 @@ describe("applyMediaTreatmentToHtml", () => {
2223
const overview = getMediaTreatmentCapabilityOverview();
2324

2425
expect(overview.families.find(({ id }) => id === "correction")).not.toHaveProperty("items");
26+
expect(overview.families.find(({ id }) => id === "grading")).toMatchObject({
27+
label: "Color Grading",
28+
});
2529
expect(overview.families.find(({ id }) => id === "art")).not.toHaveProperty("items");
2630
expect(overview.families.find(({ id }) => id === "overlays")).toMatchObject({
2731
owner: "registry",
@@ -76,6 +80,40 @@ describe("applyMediaTreatmentToHtml", () => {
7680
family: "finishing",
7781
control: expect.objectContaining({ key: "vignette" }),
7882
});
83+
expect(getMediaTreatmentCapabilityDetail("wheels")).toMatchObject({
84+
contract: expect.objectContaining({ zones: ["shadows", "midtones", "highlights"] }),
85+
});
86+
expect(getMediaTreatmentCapabilityDetail("curves")).toMatchObject({
87+
contract: expect.objectContaining({ channels: ["master", "red", "green", "blue"] }),
88+
});
89+
const hueCurves = getMediaTreatmentCapabilityDetail("hue-curves");
90+
const serializedHueCurves = JSON.stringify(hueCurves);
91+
expect(serializedHueCurves).toContain('"maxPoints":16');
92+
expect(serializedHueCurves).toContain('"key":"hueVsHue"');
93+
expect(serializedHueCurves).toContain('"key":"hueVsSaturation"');
94+
expect(serializedHueCurves).toContain('"key":"hueVsLuma"');
95+
expect(getMediaTreatmentCapabilityDetail("secondary")).toMatchObject({
96+
contract: expect.objectContaining({
97+
max: 4,
98+
saturation: expect.objectContaining({ relation: "min < max" }),
99+
luma: expect.objectContaining({ relation: "min < max" }),
100+
}),
101+
});
102+
expect(getMediaTreatmentCapabilityDetail("grading")).toMatchObject({
103+
order: [
104+
"adjust",
105+
"wheels",
106+
"curves",
107+
"hueCurves",
108+
"secondaries",
109+
"lut",
110+
"details",
111+
"effects",
112+
],
113+
});
114+
expect(getMediaTreatmentCapabilityDetail("scopes")).toMatchObject({
115+
command: expect.stringContaining("--analyze"),
116+
});
79117
});
80118

81119
it("rejects unknown capability lookups", () => {
@@ -113,6 +151,82 @@ describe("applyMediaTreatmentToHtml", () => {
113151
expect(result.html).toContain("data-color-grading=");
114152
});
115153

154+
it("normalizes and persists advanced grading on real media", () => {
155+
const result = applyMediaTreatmentToHtml(VIDEO, {
156+
selector: "#hero",
157+
grading: {
158+
wheels: { shadows: { hue: 205, amount: 0.08, level: 0.02 } },
159+
curves: {
160+
master: [
161+
[0, 0],
162+
[0.5, 0.55],
163+
[1, 1],
164+
],
165+
},
166+
hueCurves: {
167+
hueVsSaturation: [
168+
[180, 0],
169+
[210, 0.15],
170+
[240, 0],
171+
],
172+
},
173+
secondaries: [
174+
{
175+
key: {
176+
hue: { center: 215, range: 25, softness: 10 },
177+
saturation: { min: 0.2, max: 1, softness: 0.08 },
178+
luma: { min: 0.1, max: 0.9, softness: 0.08 },
179+
},
180+
correction: { saturation: 0.15, luma: 0.03 },
181+
},
182+
],
183+
},
184+
});
185+
186+
expect(result.changed).toBe(true);
187+
expect(result.value).toContain('"wheels"');
188+
expect(result.value).toContain('"curves"');
189+
expect(result.value).toContain('"hueCurves"');
190+
expect(result.value).toContain('"secondaries"');
191+
});
192+
193+
it("persists a disabled secondary without treating it as an active grade", () => {
194+
const result = applyMediaTreatmentToHtml(VIDEO, {
195+
selector: "#hero",
196+
grading: {
197+
secondaries: [
198+
{
199+
enabled: false,
200+
key: { hue: { center: 215, range: 25 } },
201+
correction: { saturation: 0.15 },
202+
},
203+
],
204+
},
205+
});
206+
207+
expect(result.changed).toBe(true);
208+
expect(result.value).toContain('"enabled":false');
209+
expect(result.value).toContain('"secondaries"');
210+
});
211+
212+
it("resolves nested composition media through the shared project-root contract", () => {
213+
const project = "/tmp/hf-project";
214+
215+
expect(
216+
resolveMediaTreatmentSource(
217+
project,
218+
"compositions/scene.html",
219+
"../capture/talking-head.mp4?v=1#frame",
220+
),
221+
).toBe(join(project, "capture/talking-head.mp4"));
222+
expect(
223+
resolveMediaTreatmentSource(project, "compositions/scene.html", "assets/photo.webp"),
224+
).toBe(join(project, "assets/photo.webp"));
225+
expect(() =>
226+
resolveMediaTreatmentSource(project, "compositions/scene.html", "https://example.com/a.mp4"),
227+
).toThrow(/local project asset/);
228+
});
229+
116230
it("merges a validated patch and reports the stored before and after payloads", () => {
117231
const initial = applyMediaTreatmentToHtml(VIDEO, {
118232
selector: "#hero",

0 commit comments

Comments
 (0)