Skip to content

armgit / hypit

Public Imported from GitHub
0

Clone any viral video with AI agents. Not just a script, the whole workflow: swap the face, the words, the B-roll, ship 100 variants in one command, and get your 100M views.

fix: encode still-video clips with BT.709 and tag the output (#363)

HEOJUNFO31c358d4f006
diff --git a/packages/media-execution/src/execute.ts b/packages/media-execution/src/execute.ts
index 049b4d70..4846fb68 100644
--- a/packages/media-execution/src/execute.ts
+++ b/packages/media-execution/src/execute.ts
@@ -774,6 +774,11 @@ export async function executeNormalizeMedia(
}
}
+// Authored pictures are sRGB: convert with the BT.709 matrix and tag the stream so players decode
+// it the same way. Same encode tail as the HyperFrames local renderer.
+const stillBt709Yuv420p = "scale=out_color_matrix=bt709:out_range=tv,format=yuv420p,"
+ + "setparams=color_primaries=bt709:color_trc=bt709:colorspace=bt709:range=tv";
+
/**
* Encode authored images into an exact video-only CFR clip. One picture is held for the whole frame
* count; several are each held for their planned segment, fitted into the first picture's frame
@@ -810,7 +815,7 @@ export async function executeRenderStillVideo(
].join(",");
let argv: string[];
if (need.request.segments.length === 1) {
- const filter = `${hold(need.request.frameCount)},pad=ceil(iw/2)*2:ceil(ih/2)*2:(ow-iw)/2:(oh-ih)/2:color=black,setsar=1`;
+ const filter = `${hold(need.request.frameCount)},pad=ceil(iw/2)*2:ceil(ih/2)*2:(ow-iw)/2:(oh-ih)/2:color=black,setsar=1,${stillBt709Yuv420p}`;
argv = ["-y", "-i", inputs[0]!, "-map", "0:v:0", "-an", "-vf", filter];
} else {
const first = await outputInspection({
@@ -827,7 +832,7 @@ export async function executeRenderStillVideo(
const height = Math.ceil(picture.height / 2) * 2;
const chains = need.request.segments.map((segment, index) =>
`[${index}:v]${hold(segment.endFrameExclusive - segment.startFrame)},`
- + `scale=${width}:${height}:force_original_aspect_ratio=decrease,pad=${width}:${height}:(ow-iw)/2:(oh-ih)/2:color=black,setsar=1,format=yuv420p[s${index}]`);
+ + `scale=${width}:${height}:force_original_aspect_ratio=decrease,pad=${width}:${height}:(ow-iw)/2:(oh-ih)/2:color=black,setsar=1,${stillBt709Yuv420p}[s${index}]`);
const concat = `${need.request.segments.map((_, index) => `[s${index}]`).join("")}concat=n=${need.request.segments.length}:v=1:a=0[v]`;
argv = ["-y", ...inputs.flatMap((path) => ["-i", path]), "-filter_complex", `${chains.join(";")};${concat}`, "-map", "[v]", "-an"];
}
diff --git a/packages/provider-media-local/test/provider.test.ts b/packages/provider-media-local/test/provider.test.ts
index 21c31343..cef5a62f 100644
--- a/packages/provider-media-local/test/provider.test.ts
+++ b/packages/provider-media-local/test/provider.test.ts
@@ -711,6 +711,66 @@ test("local media Provider transforms A/V and extracts ordinary audio and frame
}
});
+test("still video converts sRGB pictures with BT.709 and tags the stream", {
+ skip: !hasMediaBinaries,
+}, async () => {
+ const root = await mkdtemp(join(tmpdir(), "hypit-provider-media-still-color-"));
+ try {
+ // HD, so players (and Chromium) decode an untagged stream as BT.709.
+ const picturePath = join(root, "green.png");
+ await run("ffmpeg", [
+ "-v", "error", "-y", "-f", "lavfi", "-i", "color=c=0x20C040:s=1280x720,format=rgb24",
+ "-frames:v", "1", picturePath,
+ ]);
+ const resources = new MemoryResourceStore();
+ const picture = await resources.put(await readFile(picturePath), "image/png");
+ const render = async (id: string, segments: readonly CanonicalValue[]) => {
+ const request = need(id, mediaPipelineCapabilities.renderStill, artifactTypes.blob, canonicalize({
+ request: {
+ frameRate: { numerator: 30, denominator: 1 },
+ frameCount: 6,
+ output: { container: "mp4", codec: "h264", pixelFormat: "yuv420p" },
+ segments,
+ },
+ }));
+ const result = await (await handlerFor(request)).handler({
+ command: { kind: "fulfill-need", id: `command:${id}`, need: request },
+ need: request,
+ resources,
+ credentials: {},
+ });
+ assert.ok(result.value.kind === "blob");
+ const path = join(root, `${id.replace(/\W/g, "-")}.mp4`);
+ await writeFile(path, (await resources.get(result.value.resource))!);
+ return path;
+ };
+ const outputs = [
+ await render("need:still-color-single", [{ startFrame: 0, endFrameExclusive: 6, source: picture }]),
+ await render("need:still-color-spread", [
+ { startFrame: 0, endFrameExclusive: 3, source: picture },
+ { startFrame: 3, endFrameExclusive: 6, source: picture },
+ ]),
+ ];
+ for (const output of outputs) {
+ const tags = spawnSync("ffprobe", [
+ "-v", "error", "-select_streams", "v:0",
+ "-show_entries", "stream=color_range,color_space,color_primaries,color_transfer", "-of", "csv=p=0", output,
+ ], { encoding: "utf8", windowsHide: true }).stdout.trim();
+ assert.equal(tags, "tv,bt709,bt709,bt709", output);
+ const pixel = spawnSync("ffmpeg", [
+ "-v", "error", "-i", output, "-frames:v", "1",
+ "-vf", "crop=2:2:640:360,scale=in_color_matrix=bt709:in_range=tv,format=rgb24",
+ "-f", "rawvideo", "pipe:1",
+ ], { windowsHide: true }).stdout.subarray(0, 3);
+ [0x20, 0xc0, 0x40].forEach((expected, channel) => {
+ assert.ok(Math.abs(pixel[channel]! - expected) <= 3, `${output} channel ${channel}: ${pixel[channel]} vs ${expected}`);
+ });
+ }
+ } finally {
+ await rm(root, { recursive: true, force: true }).catch(() => {});
+ }
+});
+
test("local media Provider renders one frame-domain audio plan and muxes exactly one video-only visual with it", {
skip: !hasMediaBinaries,
}, async () => {