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.

5.4 KB
Raw Blame
import assert from "node:assert/strict";
import test from "node:test";
import { mkdir, mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { sealComposition } from "@hypit/composition";
import { compileHtmlProgram } from "@hypit/html-program";
import { sealTimeline } from "@hypit/timeline";
import { appendVisualClip, appendTimedMediaLayer, createMediaLayerSet, createVisualTrackSet,
  finalizeVisualTrack, projectVisualTrack, sealVisualClipSpec, sealMediaSampleLayerSpec,
  sealVisualTrackHeader } from "@hypit/visual-track";
import { projectProgramWindow } from "../../../test/temporal-fixture.js";
import { MemoryResourceStore, ffmpegBytes, normalizeTestVideo, transparentVideoFixture, writeTestArtifact } from "../../../test/alpha-video-fixture.js";
import sharp from "sharp";
import { rasterizeHtmlProgram, rasterizeHtmlFrames } from "../src/index.js";

test("transparent normalized media composites through an explicit Visual Clip in Chrome", {
  skip: process.env.HYPIT_BROWSER_TESTS !== "1",
}, async () => {
  const directory = process.env.HYPIT_ALPHA_PROOF_DIR ?? await mkdtemp(join(tmpdir(), "hypit-alpha-render-"));
  await mkdir(directory, { recursive: true });
  try {
    const resources = new MemoryResourceStore();
    const fixture = await transparentVideoFixture(directory, "webm");
    const source = await resources.put(fixture.bytes, fixture.mediaType);
    const media = await normalizeTestVideo(resources, source, true);
    const broll = await normalizeTestVideo(resources, source, false);
    const fit = { sizing: "contain" as const, framePoint: { x: 0.5, y: 0.5 }, contentPoint: { x: 0.5, y: 0.5 },
      offsetPx: { x: 0, y: 0 }, constraint: "bounded" as const };
    const frame = { xPx: 0, yPx: 0, widthPx: 96, heightPx: 64 };
    const space = sealTimeline({ id: "film", frameRate: media.frameDomain.frameRate, frameCount: media.frameDomain.frameCount });
    const header = sealVisualTrackHeader({ id: "broll" });
    const layers = appendTimedMediaLayer(createMediaLayerSet(), broll, fit, sealMediaSampleLayerSpec({ id: "cutout",
      appearance: { opacity: 1, filter: { blurPx: 0, brightness: 1, contrast: 1, saturation: 1 } },
    }));
    const spec = sealVisualClipSpec({ id: "broll-cutout", z: 2,
      treatment: { clip: { kind: "none" }, padding: { topPx: 0, rightPx: 0, bottomPx: 0, leftPx: 0 },
        border: { widthPx: 0, style: "solid", color: "#000000" }, shadows: [] } });
    const items = appendVisualClip(createVisualTrackSet(), header, space,
      layers, { ...frame, xPx: 96 }, spec, projectProgramWindow({
        itemId: spec.id, semantic: space, projection: { start: { ref: "timeline.start" }, end: { ref: "timeline.end" } },
      }));
    const speechHeader = sealVisualTrackHeader({ id: "speech" });
    const speechSpec = { ...spec, id: "speech", z: 1 };
    const speechLayers = appendTimedMediaLayer(createMediaLayerSet(), media, fit, sealMediaSampleLayerSpec({
      id: "content",
      appearance: { opacity: 1, filter: { blurPx: 0, brightness: 1, contrast: 1, saturation: 1 } },
    }));
    const speechSet = appendVisualClip(createVisualTrackSet(), speechHeader, space,
      speechLayers, frame, speechSpec,
      projectProgramWindow({ itemId: speechSpec.id, semantic: space,
        projection: { start: { ref: "timeline.start" }, end: { ref: "timeline.end" } } }));
    const speechVisual = projectVisualTrack(space, finalizeVisualTrack(speechSet, speechHeader, space));
    const brollVisual = projectVisualTrack(space, finalizeVisualTrack(items, header, space));
    const document = compileHtmlProgram(sealComposition({ id: "alpha-proof",
      canvas: { width: 192, height: 64, clearColor: "#143cdc" }, tracks: [speechVisual, brollVisual] }), space);
    const rendered = await rasterizeHtmlProgram({ program: document }, { resources, workers: 2, quality: "high", processTimeoutMs: 120_000 });
    const video = await writeTestArtifact(resources, rendered.artifact, join(directory, "speech-and-broll.mp4"));
    const pixels = ffmpegBytes(["-i", video, "-pix_fmt", "rgb24", "-f", "rawvideo", "pipe:1"]);
    assert.equal(pixels.length, 192 * 64 * 3 * 12);
    const pixel = (f: number, x: number, y: number) => [...pixels.subarray(((f * 64 + y) * 192 + x) * 3, ((f * 64 + y) * 192 + x) * 3 + 3)];
    const close = (actual: number[], expected: number[]) => {
      assert.ok(actual.every((value, i) => Math.abs(value - expected[i]!) < 14), `${actual} differs from ${expected}`);
    };
    for (let f = 0; f < 12; f++) for (const left of [0, 96]) {
      close(pixel(f, left + 4, 4), [20, 60, 220]);
      close(pixel(f, left + 32, 32), [240, 20, 20]);
      close(pixel(f, left + 60, 32), [130, 40, 120]);
    }
    const snapshots = await rasterizeHtmlFrames({ program: document, frames: [0, 11] }, { resources, workers: 2 });
    for (const snapshot of snapshots) {
      const pixels = await sharp((await resources.get(snapshot.resource))!).removeAlpha().raw().toBuffer();
      const at = (x: number, y: number) => [...pixels.subarray((y * 192 + x) * 3, (y * 192 + x) * 3 + 3)];
      for (const left of [0, 96]) {
        close(at(left + 4, 4), [20, 60, 220]);
        close(at(left + 32, 32), [240, 20, 20]);
        close(at(left + 60, 32), [130, 40, 120]);
      }
    }
    close(pixel(0, 18, 32), [240, 20, 20]);
    close(pixel(11, 18, 32), [20, 60, 220]);
  } finally {
    if (process.env.HYPIT_ALPHA_PROOF_DIR === undefined) await rm(directory, { recursive: true, force: true });
  }
});