diff --git a/docs/packages/cli.mdx b/docs/packages/cli.mdx index 6d22fc2d04..f770228aad 100644 --- a/docs/packages/cli.mdx +++ b/docs/packages/cli.mdx @@ -1563,7 +1563,9 @@ with a `reverse` field: whether you are signed in, the plan (`free`, `pro`, or purchase to this run (both `null` when HeyGen says no plan can be bought right now). It stops the run only when HeyGen blocks it for a Free account: it then exits non-zero with that command, or, without one, says a plan can't be purchased right now. Signed out, offline, or on a server without -the route, it skips the session and still probes. +the route, it skips the session and still probes; `--dir` is still created with a +`hyperframes.json` naming the skill. `init` accepts that folder with the video already in it when +it is the `--video` it was given. ```bash npx hyperframes reverse start ref.mp4 --dir my-rebuild diff --git a/packages/cli/src/commands/init.sourceInFolder.test.ts b/packages/cli/src/commands/init.sourceInFolder.test.ts new file mode 100644 index 0000000000..8d9ecb937b --- /dev/null +++ b/packages/cli/src/commands/init.sourceInFolder.test.ts @@ -0,0 +1,193 @@ +import { afterEach, beforeEach, expect, it, vi } from "vitest"; +import { execFileSync, spawnSync } from "node:child_process"; +import { + copyFileSync, + existsSync, + linkSync, + mkdirSync, + mkdtempSync, + readFileSync, + renameSync, + rmSync, + symlinkSync, + writeFileSync, +} from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { runCommand } from "citty"; +import { consumeCommandResult } from "../utils/commandResult.js"; +import init from "./init.js"; + +vi.mock("../telemetry/events.js", () => ({ trackInitTemplate: vi.fn() })); + +const ffmpeg = spawnSync("ffmpeg", ["-version"]).status === 0; +const hevc = + ffmpeg && spawnSync("ffmpeg", ["-hide_banner", "-encoders"]).stdout.includes("libx265"); +const caseBlind = (() => { + const probe = mkdtempSync(join(tmpdir(), "hf-case-")); + writeFileSync(join(probe, "a"), ""); + const blind = existsSync(join(probe, "A")); + rmSync(probe, { recursive: true, force: true }); + return blind; +})(); +let dir: string; +let project: string; + +const encode = (out: string, codec: string[]) => + execFileSync("ffmpeg", [ + ...["-v", "error", "-y", "-f", "lavfi", "-i", "testsrc2=size=160x90:rate=24:duration=1"], + ...codec, + ...["-pix_fmt", "yuv420p", out], + ]); +const codecOf = (file: string) => + execFileSync("ffprobe", [ + ...["-v", "error", "-select_streams", "v:0", "-show_entries", "stream=codec_name"], + ...["-of", "csv=p=0", "--", file], + ]) + .toString() + .trim(); + +beforeEach(() => { + dir = mkdtempSync(join(tmpdir(), "hf-init-source-")); + project = join(dir, "rebuild"); + mkdirSync(project); + // What `reverse start --dir` leaves, then the video the agent downloaded into it. + writeFileSync( + join(project, "hyperframes.json"), + '{\n "authoringSkill": "reverse-any-video"\n}\n', + ); + if (ffmpeg) encode(join(project, "original.mp4"), ["-c:v", "libx264"]); + vi.stubEnv("HYPERFRAMES_SKIP_SKILLS", "1"); + vi.spyOn(console, "log").mockImplementation(() => {}); + vi.spyOn(console, "error").mockImplementation(() => {}); +}); + +afterEach(() => { + rmSync(dir, { recursive: true, force: true }); + vi.restoreAllMocks(); + vi.unstubAllEnvs(); + consumeCommandResult(); +}); + +const initWithVideo = (video = join(project, "original.mp4"), keepOriginal = true) => + runCommand(init, { + rawArgs: [ + ...[project, "--video", video, "--example", "blank"], + ...(keepOriginal ? ["--keep-original"] : []), + ...["--skip-transcribe", "--non-interactive"], + ], + }); + +it.skipIf(!ffmpeg)( + "scaffolds into the folder that already holds the video it was given", + async () => { + const video = readFileSync(join(project, "original.mp4")); + await initWithVideo(); + expect(existsSync(join(project, "index.html"))).toBe(true); + expect(readFileSync(join(project, "original.mp4")).equals(video)).toBe(true); + expect(readFileSync(join(project, "hyperframes.json"), "utf-8")).toContain("reverse-any-video"); + }, +); + +it.skipIf(!ffmpeg || !caseBlind)( + "scaffolds when the video in the folder is named in another case on a case-blind disk", + async () => { + renameSync(join(project, "original.mp4"), join(project, "Original.MP4")); + const video = readFileSync(join(project, "Original.MP4")); + await initWithVideo(join(project, "original.mp4")); + expect(existsSync(join(project, "index.html"))).toBe(true); + expect(readFileSync(join(project, "Original.MP4")).equals(video)).toBe(true); + }, +); + +it.skipIf(!ffmpeg)("still refuses a folder holding anything else", async () => { + writeFileSync(join(project, "notes.txt"), "mine"); + await initWithVideo().catch(() => undefined); + expect(existsSync(join(project, "index.html"))).toBe(false); +}); + +it.skipIf(!ffmpeg).each(["index.html", "meta.json", "AGENTS.md"])( + "refuses a video named %s, a file the scaffold writes, and leaves it as it was", + async (name) => { + const source = join(project, name); + copyFileSync(join(project, "original.mp4"), source); + rmSync(join(project, "original.mp4")); + const video = readFileSync(source); + await initWithVideo(source).catch(() => undefined); + expect(readFileSync(source).equals(video)).toBe(true); + expect(existsSync(join(project, name === "index.html" ? "meta.json" : "index.html"))).toBe( + false, + ); + }, +); + +it.skipIf(!hevc).each(["original.mp4", "original.MP4"])( + "transcodes an HEVC %s already in the folder beside it, keeping the original", + async (name) => { + rmSync(join(project, "original.mp4")); + const source = join(project, name); + encode(source, ["-c:v", "libx265", "-tag:v", "hvc1"]); + const video = readFileSync(source); + await initWithVideo(source, false); + // On a case-sensitive disk original.mp4 is a free name beside original.MP4. + const out = name === "original.mp4" || caseBlind ? "original.h264.mp4" : "original.mp4"; + expect(readFileSync(source).equals(video)).toBe(true); + expect(codecOf(join(project, out))).toBe("h264"); + expect(readFileSync(join(project, "index.html"), "utf-8")).toContain(out); + }, + 60_000, +); + +const linkKinds = [ + ["hard link", linkSync], + ["symlink", symlinkSync], +] as const; + +it.skipIf(!hevc).each(linkKinds)( + "never transcodes onto the HEVC source through a %s where its .mp4 would go", + async (_, link) => { + rmSync(join(project, "original.mp4")); + const source = join(project, "original.mov"); + encode(source, ["-c:v", "libx265", "-tag:v", "hvc1"]); + link(source, join(project, "original.mp4")); + const video = readFileSync(source); + await initWithVideo(source, false); + expect(readFileSync(source).equals(video)).toBe(true); + expect(codecOf(source)).toBe("hevc"); + expect(codecOf(join(project, "original.h264.mp4"))).toBe("h264"); + }, + 60_000, +); + +it.skipIf(!hevc).each(linkKinds)( + "never transcodes onto the HEVC source through a %s where its .h264.mp4 would go", + async (_, link) => { + rmSync(join(project, "original.mp4")); + const source = join(project, "original.mp4"); + encode(source, ["-c:v", "libx265", "-tag:v", "hvc1"]); + link(source, join(project, "original.h264.mp4")); + const video = readFileSync(source); + await initWithVideo(source, false); + expect(readFileSync(source).equals(video)).toBe(true); + expect(codecOf(source)).toBe("hevc"); + expect(codecOf(join(project, "original.h264-2.mp4"))).toBe("h264"); + }, + 60_000, +); + +it.skipIf(!hevc)( + "never transcodes onto the HEVC source when it is named through a linked folder", + async () => { + rmSync(join(project, "original.mp4")); + encode(join(project, "original.mp4"), ["-c:v", "libx265", "-tag:v", "hvc1"]); + const alias = join(dir, "alias"); + symlinkSync(project, alias); + const source = join(alias, "original.mp4"); + const video = readFileSync(source); + await initWithVideo(source, false); + expect(readFileSync(source).equals(video)).toBe(true); + expect(codecOf(source)).toBe("hevc"); + expect(codecOf(join(project, "original.h264.mp4"))).toBe("h264"); + }, + 60_000, +); diff --git a/packages/cli/src/commands/init.ts b/packages/cli/src/commands/init.ts index eb901b7a4e..c4cc09e752 100644 --- a/packages/cli/src/commands/init.ts +++ b/packages/cli/src/commands/init.ts @@ -23,8 +23,10 @@ export const examples: Example[] = [ ]; import { existsSync, + lstatSync, mkdirSync, copyFileSync, + statSync, writeFileSync, readFileSync, readdirSync, @@ -278,8 +280,58 @@ function getSharedTemplateDir(): string { return resolveAssetDir(["..", "templates", "_shared"], ["templates", "_shared"]); } -function holdsFilesBesideProjectConfig(dir: string): boolean { - return existsSync(dir) && readdirSync(dir).some((name) => name !== "hyperframes.json"); +const MEDIA_THE_SCAFFOLD_NEVER_WRITES = + /\.(mp4|m4v|mov|webm|mkv|avi|mpe?g|wav|mp3|m4a|aac|ogg|oga|opus|flac)$/i; + +function fileOnDisk(path: string): string | undefined { + try { + const { dev, ino } = statSync(path); + return `${dev}:${ino}`; + } catch { + return undefined; + } +} + +function holdsFilesBesideProjectConfigOrSources( + dir: string, + sources: (string | undefined)[] = [], +): boolean { + const kept = new Set( + sources.flatMap((s) => (s && MEDIA_THE_SCAFFOLD_NEVER_WRITES.test(s) ? [fileOnDisk(s)] : [])), + ); + kept.delete(undefined); + const isKeptSource = (name: string) => + MEDIA_THE_SCAFFOLD_NEVER_WRITES.test(name) && kept.has(fileOnDisk(resolve(dir, name))); + return ( + existsSync(dir) && + readdirSync(dir).some((name) => name !== "hyperframes.json" && !isKeptSource(name)) + ); +} + +function nothingAt(path: string): boolean { + try { + lstatSync(path); + return false; + } catch { + return true; + } +} + +/** + * The first of `name.mp4`, `name.h264.mp4`, `name.h264-2.mp4`, ... with nothing at its path. FFmpeg + * overwrites its output, and a link or a case-only spelling there can be the source itself. + */ +function freeTranscodeName(destDir: string, localVideoName: string): string { + const stem = localVideoName.replace(/\.[^.]+$/, ""); + for (let n = 0; ; n++) { + const name = n === 0 ? `${stem}.mp4` : `${stem}.h264${n === 1 ? "" : `-${n}`}.mp4`; + if (nothingAt(resolve(destDir, name))) return name; + } +} + +function copyIntoProjectUnlessAlreadyThere(src: string, dest: string): void { + const same = resolve(src) === resolve(dest) || fileOnDisk(src) === (fileOnDisk(dest) ?? ""); + if (!same) copyFileSync(src, dest); } /** The longest folder name the common disks hold: 255 UTF-8 bytes on Linux, 255 UTF-16 units on macOS and Windows. */ @@ -492,7 +544,7 @@ async function handleVideoFile( } if (shouldTranscode) { - const mp4Name = localVideoName.replace(/\.[^.]+$/, ".mp4"); + const mp4Name = freeTranscodeName(destDir, localVideoName); const mp4Path = resolve(destDir, mp4Name); const spin = clack.spinner(); spin.start("Transcoding to H.264 MP4..."); @@ -502,10 +554,10 @@ async function handleVideoFile( localVideoName = mp4Name; } else { spin.stop(c.warn("Transcode failed — copying original file")); - copyFileSync(videoPath, resolve(destDir, localVideoName)); + copyIntoProjectUnlessAlreadyThere(videoPath, resolve(destDir, localVideoName)); } } else { - copyFileSync(videoPath, resolve(destDir, localVideoName)); + copyIntoProjectUnlessAlreadyThere(videoPath, resolve(destDir, localVideoName)); } } else { if (interactive) { @@ -515,10 +567,10 @@ async function handleVideoFile( console.log(c.warn("ffmpeg not installed — cannot transcode. Copying original.")); console.log(c.dim("Install: ") + c.accent(getFFmpegInstallHint())); } - copyFileSync(videoPath, resolve(destDir, localVideoName)); + copyIntoProjectUnlessAlreadyThere(videoPath, resolve(destDir, localVideoName)); } } else { - copyFileSync(videoPath, resolve(destDir, localVideoName)); + copyIntoProjectUnlessAlreadyThere(videoPath, resolve(destDir, localVideoName)); } return { meta, localVideoName }; @@ -864,7 +916,7 @@ export default defineCommand({ failCommand(); } - if (holdsFilesBesideProjectConfig(destDir)) { + if (holdsFilesBesideProjectConfigOrSources(destDir, [videoFlag, audioFlag])) { console.error(c.error(`Directory already exists and is not empty: ${name}`)); failCommand(); } @@ -912,7 +964,7 @@ export default defineCommand({ // Handle audio if (audioPath) { sourceFilePath = audioPath; - copyFileSync(audioPath, resolve(destDir, basename(audioPath))); + copyIntoProjectUnlessAlreadyThere(audioPath, resolve(destDir, basename(audioPath))); const { getMediaDurationSeconds } = await import("../whisper/transcribe.js"); videoDuration = getMediaDurationSeconds(audioPath) ?? undefined; console.log(`Audio: ${basename(audioPath)}`); @@ -1048,7 +1100,7 @@ export default defineCommand({ failCommand(); } - if (holdsFilesBesideProjectConfig(destDir)) { + if (holdsFilesBesideProjectConfigOrSources(destDir, [videoFlag, audioFlag])) { const overwrite = await clack.confirm({ message: `Directory ${c.accent(name)} already exists and is not empty. Overwrite?`, initialValue: false, @@ -1089,7 +1141,7 @@ export default defineCommand({ } mkdirSync(destDir, { recursive: true }); sourceFilePath = audioPath; - copyFileSync(audioPath, resolve(destDir, basename(audioPath))); + copyIntoProjectUnlessAlreadyThere(audioPath, resolve(destDir, basename(audioPath))); const { getMediaDurationSeconds } = await import("../whisper/transcribe.js"); videoDuration = getMediaDurationSeconds(audioPath) ?? undefined; clack.log.info(`Audio copied to ${c.accent(basename(audioPath))}`); diff --git a/packages/cli/src/commands/reverse/start.test.ts b/packages/cli/src/commands/reverse/start.test.ts index 54b89ce20a..4f562a9c6d 100644 --- a/packages/cli/src/commands/reverse/start.test.ts +++ b/packages/cli/src/commands/reverse/start.test.ts @@ -1,5 +1,5 @@ import { runCommand } from "citty"; -import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; @@ -169,12 +169,29 @@ describe("reverse start", () => { { recorded: false, reason: "unavailable", plan: "free" }, { signed_in: true, plan: "free", recorded: false }, ], - ])("never stops the run when %s", async (_label, answer, reverse) => { - server.answer = answer; + ])( + "never stops the run when %s, and still makes the --dir project", + async (_label, answer, reverse) => { + server.answer = answer; + const dir = join(root, "rebuild"); + await run("--dir", dir); + expect(output()).toMatchObject({ + ok: true, + width: 1080, + reverse: { ...reverse, saved_to: join(dir, "hyperframes.json") }, + }); + expect(config(dir)).toMatchObject({ authoringSkill: "reverse-any-video" }); + expect(config(dir)).not.toHaveProperty("reverseSessionId"); + }, + ); + + it("names no saved_to when the --dir project's config is broken, and leaves it as it was", async () => { const dir = join(root, "rebuild"); + mkdirSync(dir); + writeFileSync(join(dir, "hyperframes.json"), "{ not json"); await run("--dir", dir); - expect(output()).toMatchObject({ ok: true, width: 1080, reverse }); - expect(() => readFileSync(join(dir, "hyperframes.json"))).toThrow(); + expect(output()).toMatchObject({ ok: true, reverse: { recorded: false, saved_to: null } }); + expect(readFileSync(join(dir, "hyperframes.json"), "utf-8")).toBe("{ not json"); }); it("stops a Free account the server blocks, with the subscribe command as the fix, after saving the session", async () => { diff --git a/packages/cli/src/commands/reverse/start.ts b/packages/cli/src/commands/reverse/start.ts index 183d4f2018..08a626addb 100644 --- a/packages/cli/src/commands/reverse/start.ts +++ b/packages/cli/src/commands/reverse/start.ts @@ -1,4 +1,4 @@ -import { existsSync } from "node:fs"; +import { existsSync, mkdirSync } from "node:fs"; import { defineCommand } from "../../utils/defineCommand.js"; import type { Example } from "../_examples.js"; import { jsonArg, runMeasure, videoArg } from "../../measure/command.js"; @@ -13,6 +13,7 @@ import { projectConfigPath, readProjectConfig, recordProjectReverseSession, + seedProjectAuthoringSkill, } from "../../utils/projectConfig.js"; import { probeReport } from "../measure/probe.js"; @@ -44,6 +45,7 @@ async function summary(started: ReverseStart, savedTo: string | undefined) { signed_in: started.reason !== "not_signed_in", plan: started.plan ?? null, recorded: false, + saved_to: savedTo ?? null, ...(started.plan === "free" ? { upgrade_url: await reverseUpgradeUrl(), subscribe_command: subscribeCommand() } : {}), @@ -61,6 +63,22 @@ async function summary(started: ReverseStart, savedTo: string | undefined) { }; } +function makeProjectStampedWithSkill(dir: string): string | undefined { + try { + mkdirSync(dir, { recursive: true }); + } catch { + return undefined; + } + seedProjectAuthoringSkill(dir, SKILL); + return readProjectConfig(dir)?.authoringSkill === SKILL ? projectConfigPath(dir) : undefined; +} + +function saveRun(started: ReverseStart, dir: string | undefined): string | undefined { + if (started.recorded && (dir !== undefined || existsSync(projectConfigPath(".")))) + return recordProjectReverseSession(dir ?? ".", started.session.sessionId, SKILL); + return dir === undefined ? undefined : makeProjectStampedWithSkill(dir); +} + async function refusalWhenServerBlocksFree(session: ReverseSession): Promise { if (session.gate !== "block" || session.plan !== "free") return undefined; if ((await reverseUpgradeUrl(session)) === null) { @@ -99,10 +117,7 @@ export default defineCommand({ const started = await startReverseSession({ resumeSessionId: readProjectConfig(projectDir)?.reverseSessionId, }); - const save = started.recorded && (dir !== undefined || existsSync(projectConfigPath("."))); - const savedTo = save - ? recordProjectReverseSession(projectDir, started.session.sessionId, SKILL) - : undefined; + const savedTo = saveRun(started, dir); const refused = started.recorded ? await refusalWhenServerBlocksFree(started.session) : undefined;