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.

1.7 KB
Raw Blame
import type { Timeline } from "@hypit/timeline";

import type { TemporalDuration } from "./types.js";

function integer(value: number, label: string): bigint {
  if (!Number.isSafeInteger(value) || value < 0) throw new Error(`${label} must be a non-negative safe integer.`);
  return BigInt(value);
}

function roundedRatio(numerator: bigint, denominator: bigint, label: string): number {
  if (denominator <= 0n) throw new Error(`${label} denominator must be positive.`);
  const value = (numerator * 2n + denominator) / (denominator * 2n);
  if (value < 0n || value > BigInt(Number.MAX_SAFE_INTEGER)) {
    throw new Error(`${label} exceeds safe sample arithmetic.`);
  }
  return Number(value);
}

/** Quantize one non-negative authored duration into the canonical sample domain exactly once. */
export function temporalDurationInSamples(
  duration: TemporalDuration,
  timeline: Timeline,
  sampleRate = 48_000,
): number {
  const rate = integer(sampleRate, "Sample rate");
  if (rate === 0n) throw new Error("Sample rate must be positive.");
  switch (duration.unit) {
    case "frames":
      return roundedRatio(
        integer(duration.value, "Frame duration") * rate * integer(timeline.frameRate.denominator, "Frame-rate denominator"),
        integer(timeline.frameRate.numerator, "Frame-rate numerator"),
        "Frame duration",
      );
    case "milliseconds":
      return roundedRatio(integer(duration.value, "Millisecond duration") * rate, 1_000n, "Millisecond duration");
    case "seconds":
      return roundedRatio(
        integer(duration.numerator, "Second duration numerator") * rate,
        integer(duration.denominator, "Second duration denominator"),
        "Second duration",
      );
  }
}