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.

6.7 KB
Raw Blame
import { mkdir, stat, writeFile } from "node:fs/promises";
import { dirname, resolve } from "node:path";

import {
  cliProviderLine,
  cliProviderView,
  createCliScratchResources,
  openCliRuntimeHost,
  selectCliProvider,
} from "@hypit/hypit/cli";
import type { CliApplicationContext, CliCommandModule, CliIo, CliRuntimeHost } from "@hypit/hypit/cli";
import { speechEvidenceFile } from "@hypit/media-local";
import type { Need } from "@hypit/hypit/protocol";
import { sealSpeechEvidenceAudio, speechEvidenceTypes, transcriptDocumentFromEvidence } from "@hypit/speech-evidence";
import type { AlignedTranscriptEvidence } from "@hypit/speech-evidence";

import { whisperXRequestForEvidenceAudio } from "./evidence.js";
import { whisperXCapabilities } from "./manifest.js";
import { parseWhisperXLanguage } from "./types.js";

function assert(condition: unknown, message: string): asserts condition {
  if (!condition) throw new Error(message);
}

type Parsed = {
  readonly positionals: readonly string[];
  readonly options: ReadonlyMap<string, string>;
  readonly json: boolean;
};

function parse(argv: readonly string[]): Parsed {
  const allowed = new Set(["--language", "--to", "--runtime", "--project"]);
  const positionals: string[] = [];
  const options = new Map<string, string>();
  let json = false;
  for (let index = 1; index < argv.length; index++) {
    const item = argv[index]!;
    if (item === "--json") { json = true; continue; }
    if (["--debug", "--verbose", "--no-color"].includes(item)) continue;
    if (item === "--color") { index += 1; continue; }
    if (!item.startsWith("--")) { positionals.push(item); continue; }
    assert(allowed.has(item), `unknown option ${item}`);
    assert(!options.has(item), `${item} cannot be repeated`);
    const value = argv[++index];
    assert(value !== undefined && !value.startsWith("--"), `${item} requires a value`);
    options.set(item, value);
  }
  return { positionals, options, json };
}

async function destination(parsed: Parsed, cwd: string): Promise<string> {
  const named = parsed.options.get("--to");
  assert(named !== undefined, "--to is required: the transcript file to write");
  const path = resolve(cwd, named);
  assert(!await stat(path).then(() => true, () => false), `Destination ${path} already exists`);
  return path;
}

async function writeNew(path: string, content: string): Promise<void> {
  await mkdir(dirname(path), { recursive: true });
  try { await writeFile(path, content, { flag: "wx" }); }
  catch (error) {
    if (error instanceof Error && "code" in error && error.code === "EEXIST") {
      throw new Error(`Destination ${path} already exists`);
    }
    throw error;
  }
}

export type TranscribeEnvironment = {
  readonly cwd: string;
  openHost(runtime: string | undefined, project: string | undefined): Promise<{
    readonly profile: string;
    readonly host: Pick<CliRuntimeHost, "providers" | "invoke">;
  }>;
};

export function transcribeEnvironment(application: CliApplicationContext): TranscribeEnvironment {
  return {
    cwd: application.cwd,
    openHost: async (runtime, project) => await openCliRuntimeHost(application, runtime, project),
  };
}

export function writeTranscribeHelp(io: CliIo): void {
  io.write("hypit transcribe\nEstablish word times with the WhisperX alignment Endpoint selected by the Runtime Profile.\n\n"
    + "  hypit transcribe <audio|video> --to <transcript.json> --language <code> [--runtime <profile>] [--project <directory>]\n\n"
    + "The local media capability projects 16 kHz mono evidence; WhisperX owns alignment and the transcript view.\n"
    + "This is one immediate request: no Build or Result state is created.\n");
}

export async function runTranscribeCli(
  argv: readonly string[],
  io: CliIo,
  environment: TranscribeEnvironment,
): Promise<void> {
  if (argv.includes("--help")) { writeTranscribeHelp(io); return; }
  const parsed = parse(argv);
  assert(parsed.positionals.length === 1, "transcribe takes exactly one audio or video file");
  const source = resolve(environment.cwd, parsed.positionals[0]!);
  const language = parseWhisperXLanguage(parsed.options.get("--language"), "transcribe --language");
  const to = await destination(parsed, environment.cwd);
  const { profile, host } = await environment.openHost(parsed.options.get("--runtime"), parsed.options.get("--project"));
  const evidence = await speechEvidenceFile(source);
  const resources = createCliScratchResources();
  const artifact = await resources.put(evidence.bytes, "audio/wav");
  const audio = sealSpeechEvidenceAudio({ domainId: "cli-transcribe", artifact, sampleFrames: evidence.sampleFrames });
  const need: Need = {
    id: "need:hypit-transcribe",
    capability: whisperXCapabilities.alignment,
    returns: speechEvidenceTypes.alignedTranscript,
    constraints: whisperXRequestForEvidenceAudio(audio, { language }),
    result: "record:hypit-transcribe",
  };
  const provider = await selectCliProvider(host, need, profile);
  if (!parsed.json) io.write(`Transcribing through ${cliProviderLine(provider)}\n`);
  const report = parsed.json ? io.writeProgress : io.writeProgress ?? io.write;
  let previousPhase: string | undefined;
  const fulfillment = await host.invoke(need, resources, {
    reportProgress: async (progress) => {
      if (progress.phase === previousPhase) return;
      previousPhase = progress.phase;
      report?.(`  · ${progress.phase}\n`);
    },
    reportDiagnostic: async (diagnostic) => { report?.(`  · ${diagnostic.message}\n`); },
  });
  assert(fulfillment.value.kind === "inline", "the transcript came back by reference");
  const aligned = fulfillment.value.value as unknown as AlignedTranscriptEvidence;
  const file = transcriptDocumentFromEvidence(aligned, { source, language });
  const words = file.passages.reduce((total, passage) => total + passage.words.length, 0);
  await writeNew(to, `${JSON.stringify(file, null, 2)}\n`);
  const view = { format: "hypit.whisperx-transcribe@1", source, language,
    audio_seconds: file.audio_seconds, extracted: evidence.extracted, ...cliProviderView(provider),
    passages: file.passages.length, words, path: to };
  if (parsed.json) io.write(`${JSON.stringify(view, null, 2)}\n`);
  else io.write(`✓ Transcript written\n\n  ${words} words in ${file.passages.length} passage${
    file.passages.length === 1 ? "" : "s"} over ${file.audio_seconds}s\n  ${to}\n`);
}

export const cliCommandModules = [{
  format: "hypit.cli-command@1",
  id: "@hypit/whisperx",
  commands: ["transcribe"],
  writeRootHelp(io) { io.write("\nSpeech alignment\n  transcribe\n  hypit transcribe --help for inputs and Runtime selection\n"); },
  writeHelp(_argv, io) { writeTranscribeHelp(io); },
  async run(argv, io, context) { await runTranscribeCli(argv, io, transcribeEnvironment(context)); },
}] as const satisfies readonly CliCommandModule[];