← Files AI Producer by OpusClipARCHIVED FILE

skills/aip/scripts/speaker_effects.py

21.8 KB · Nov 25, 2026 · 18:13 UTC

↓ Download file

See the change to this file →

"""Offline checks for speaker zoom, filter and motion effect hosts in index.html.

A speaker zoom, filter or motion is an empty visual host the editor lists and edits and
the service's engine replays; the aip-composition skill owns the contract. These checks
mirror what the service accepts, so a host that passes here is one the editor shows. A
motion host also names a definition, an inline <script data-aip-motion> whose frame
function the engine calls every frame; when Node is available, `sample` runs each one
the way the service's commit check does and reports what the engine would not draw.
"""

import json
import math
import re
import shutil
import subprocess

ENGINE = "public/vendor/speaker-effects.js"
TRACKS = {"zoom": "20", "filter": "21"}
MAX_SCALE = 3.0
MAX_RAMP = 10.0
ANCHOR_TOLERANCE = 0.02

MOTION_NAME = re.compile(r"^[a-z0-9-]{1,40}$")
PARAM_NAME = re.compile(r"^[a-z][a-zA-Z0-9]{0,15}$")
MOTION_TRACK = re.compile(r"^\s*\d{1,4}\s*$")
MIN_MOTION_TRACK, MAX_MOTION_TRACK = 900, 907
MAX_PARAMS, MAX_PARAMS_CHARS, MAX_PARAM_MAGNITUDE = 8, 400, 1_000_000
# The sources a definition could read once and keep; read on code with strings and
# comments blanked, as the service reads it.
MOTION_NONDETERMINISTIC = re.compile(
    r"\bMath\s*\.\s*random\b|\bDate\b|\bperformance\b|\bcrypto\b|\bIntl\b|\bWeakRef\b|\bFinalizationRegistry\b|"
    r"\bsetTimeout\b|\bsetInterval\b|\brequestAnimationFrame\b"
    r"|\brequestAnimationFrame\b")
_JS_STRING_OR_COMMENT = re.compile(
    r"\"(?:\\.|[^\"\\])*\"|'(?:\\.|[^'\\])*'|`(?:\\.|[^`\\])*`|/\*.*?\*/|//[^\n]*", re.S)

_FUNCTION = (
    r"(?:(?:grayscale|sepia|saturate|contrast|brightness|invert)\(\d{1,3}(?:\.\d{1,4})?%?\)"
    r"|hue-rotate\(-?\d{1,3}(?:\.\d{1,4})?deg\)"
    r"|blur\(\d{1,2}(?:\.\d{1,4})?px\))"
)
FILTER = re.compile(rf"^{_FUNCTION}(?:\s+{_FUNCTION}){{0,7}}$")
_COORD = r"-?\d{1,4}(?:\.\d{1,4})?(?:%|px)"
ORIGIN = re.compile(rf"^{_COORD}\s+{_COORD}$")


def _number(value, default=None):
    if value is None or str(value).strip() == "":
        return default
    try:
        number = float(value)
    except (TypeError, ValueError):
        return None
    return number if math.isfinite(number) else None


def _lane_file(attrs):
    """A speaker clip's file as a whole path, without a query or a leading ``./`` or ``/``."""
    path = re.split(r"[?#]", str(attrs.get("src") or "").strip(), maxsplit=1)[0]
    while path.startswith("./"):
        path = path[2:]
    return path.lstrip("/")


def _source_at(clips, start):
    """The source second playing at output second ``start``, or None off the cut. Also None
    on a lane that plays more than one file: a source second there belongs to one of them,
    and an anchor names its file in ``data-source-id``, which only the service can match
    to a path. The service's commit check is the authority for those anchors."""
    if len({_lane_file(attrs) for attrs in clips}) > 1:
        return None
    for attrs in clips:
        clip_start = _number(attrs.get("data-start"))
        clip_duration = _number(attrs.get("data-duration"))
        media_start = _number(attrs.get("data-media-start"), 0.0)
        if None in (clip_start, clip_duration, media_start):
            continue
        if clip_start <= start < clip_start + clip_duration:
            return media_start + (start - clip_start)
    return None


def motion_params(raw):
    """A motion host's parameter values, or None when the service would reject them."""
    if raw is None or str(raw).strip() == "":
        return {}
    if len(raw) > MAX_PARAMS_CHARS:
        return None
    try:
        value = json.loads(raw)
    except ValueError:
        return None
    if not isinstance(value, dict) or len(value) > MAX_PARAMS:
        return None
    for name, number in value.items():
        is_number = isinstance(number, (int, float)) and not isinstance(number, bool)
        if not PARAM_NAME.fullmatch(name) or not is_number or not math.isfinite(number) or abs(number) > MAX_PARAM_MAGNITUDE:
            return None
    return value


def _motion_host_ok(attrs):
    track = attrs.get("data-track-index")
    return (MOTION_NAME.fullmatch((attrs.get("data-effect-motion") or "").strip()) is not None
            and motion_params(attrs.get("data-effect-params")) is not None
            and track is not None and MOTION_TRACK.match(track) is not None
            and MIN_MOTION_TRACK <= int(track) <= MAX_MOTION_TRACK)


def _value_codes(kind, attrs, duration):
    if kind == "motion":
        return [] if _motion_host_ok(attrs) else ["motion_host_invalid"]
    codes = []
    ramp_in = _number(attrs.get("data-effect-in"), 0.0)
    ramp_out = _number(attrs.get("data-effect-out"), 0.0)
    if ramp_in is None or ramp_out is None or not (0 <= ramp_in <= MAX_RAMP and 0 <= ramp_out <= MAX_RAMP):
        codes.append("invalid_speaker_effect_ramp")
    elif ramp_in + ramp_out > duration:
        codes.append("speaker_effect_ramps_exceed_window")
    if kind == "filter":
        if not FILTER.match((attrs.get("data-effect-filter") or "").strip()):
            codes.append("invalid_speaker_effect_filter")
        return codes
    scale = _number(attrs.get("data-effect-scale"))
    if scale is None or not 1 < scale <= MAX_SCALE:
        codes.append("invalid_speaker_effect_scale")
    origin = attrs.get("data-effect-origin")
    if origin is not None and not ORIGIN.match(origin.strip()):
        codes.append("invalid_speaker_effect_origin")
    return codes


def _lane(kind, attrs):
    """The lane two effects may not overlap in: a zoom or filter's kind, a motion's track."""
    return f"motion:{(attrs.get('data-track-index') or '').strip()}" if kind == "motion" else kind


def _host_codes(attrs, clips):
    kind = attrs.get("data-aip-effect")
    if kind not in (*TRACKS, "motion") or "visual-host" not in (attrs.get("class") or "").split() or not attrs.get(
        "data-composition-id"
    ):
        return ["invalid_speaker_effect"], None
    codes = []
    if "data-composition-src" in attrs:
        codes.append("speaker_effect_has_composition")
    if attrs.get("data-hide-captions") != "false":
        codes.append("speaker_effect_hides_captions")
    if "data-no-timeline" not in attrs:
        codes.append("speaker_effect_waits_for_timeline")
    if kind in TRACKS and attrs.get("data-track-index") != TRACKS[kind]:
        codes.append("speaker_effect_track")
    start = _number(attrs.get("data-start"))
    duration = _number(attrs.get("data-duration"))
    if start is None or duration is None or duration <= 0:
        return codes, None  # the timing check reports the attribute itself
    codes.extend(_value_codes(kind, attrs, duration))
    anchor = _number(attrs.get("data-src-anchor"))
    if anchor is None:
        codes.append("speaker_effect_anchor_missing")
    elif _anchor_mismatch(attrs, clips) is not None:
        codes.append("speaker_effect_anchor_mismatch")
    return codes, (_lane(kind, attrs), start, start + duration)


def _anchor_mismatch(attrs, clips):
    """``(expected, found)`` source seconds when the host's anchor is not the source second at its start."""
    start = _number(attrs.get("data-start"))
    anchor = _number(attrs.get("data-src-anchor"))
    if start is None or anchor is None:
        return None
    expected = _source_at(clips, start)
    if expected is None or abs(anchor - expected) <= ANCHOR_TOLERANCE:
        return None
    return expected, anchor


def anchor_mismatches(elements):
    """One ``{"id", "expected", "found"}`` per effect host whose ``data-src-anchor`` is not the
    source second at its ``data-start``, in document order."""
    top = [(tag, attrs) for tag, attrs, depth in elements if depth == 0]
    clips = [attrs for tag, attrs in top if tag == "video" and attrs.get("data-track-index") == "0"]
    found = []
    for tag, attrs in top:
        if attrs.get("data-aip-effect") is None or "speaker_effect_anchor_mismatch" not in _host_codes(attrs, clips)[0]:
            continue
        expected, anchor = _anchor_mismatch(attrs, clips)
        found.append({"id": attrs.get("data-composition-id"), "expected": round(expected, 3),
                      "found": round(anchor, 3)})
    return found


def _definition_codes(scripts):
    """Codes for the index's motion definitions, and the names they define."""
    codes, names, seen = [], set(), set()
    for name, text in scripts:
        name = (name or "").strip()
        if not MOTION_NAME.match(name) or name in seen:
            codes.append("motion_definition_invalid")
            continue
        seen.add(name)
        if MOTION_NONDETERMINISTIC.search(_JS_STRING_OR_COMMENT.sub(" ", text)):
            codes.append("motion_nondeterministic")
            continue
        names.add(name)
    return codes, names, seen


def check(elements, motion_scripts=()):
    """Error codes for the effect hosts among ``index.html``'s ``(tag, attrs, depth)``
    elements and its ``(name, text)`` motion definitions, in the order found; empty when
    there are none or all are valid."""
    top = [(tag, attrs) for tag, attrs, depth in elements if depth == 0]
    clips = [attrs for tag, attrs in top if tag == "video" and attrs.get("data-track-index") == "0"]
    codes, windows = [], {}
    hosts = [attrs for _, attrs in top if attrs.get("data-aip-effect") is not None]
    for attrs in hosts:
        host_codes, window = _host_codes(attrs, clips)
        codes.extend(host_codes)
        if window is not None:
            windows.setdefault(window[0], []).append(window[1:])
    for lane, spans in windows.items():
        spans.sort()
        if any(later[0] < earlier[1] for earlier, later in zip(spans, spans[1:])):
            codes.append("motion_track_overlap" if lane.startswith("motion:") else "speaker_effect_overlap")
    definition_codes, defined, named = _definition_codes(motion_scripts)
    codes.extend(definition_codes)
    for attrs in hosts:
        name = (attrs.get("data-effect-motion") or "").strip()
        if attrs.get("data-aip-effect") == "motion" and _motion_host_ok(attrs) and name not in named:
            codes.append("motion_definition_missing")
    engine_loaded = any(tag == "script" and (attrs.get("src") or "").strip() == ENGINE for tag, attrs in top)
    if hosts and not engine_loaded:
        codes.append("speaker_effect_engine_missing")
    return list(dict.fromkeys(codes))


# Runs the index's definition scripts in document order in a Node vm context where
# randomness and time throw, samples each motion host at 34 instants across its window in
# that context twice and in a second one once, and reports what the service's commit check
# would refuse. The service repeats this in its own contained preflight step.
_SAMPLER = r"""
const vm = require("vm");
const input = JSON.parse(require("fs").readFileSync(0, "utf8"));
const LIMITS = { x: 160, y: 160, rotate: 8 };
const COLOR = /^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/;
const MAG = 1000000;
const STUB = "randomness or time is not available to a motion definition";
// One realm holding the definition scripts in document order, as the page runs them:
// built-ins only, randomness, time, Intl and the collector's observers throwing, Math and
// JSON frozen, no code from strings. The harness is a frozen, non-writable entry built
// from built-ins captured before any definition runs, and it reads each frame the way the
// engine does, handing back only a string of primitives.
const SETUP = `
  var thrower = function () { throw new Error(${JSON.stringify(STUB)}); };
  Math.random = thrower;
  globalThis.Date = function () { thrower(); };
  globalThis.Date.now = thrower;
  globalThis.performance = { now: thrower };
  globalThis.crypto = { getRandomValues: thrower, randomUUID: thrower };
  globalThis.setTimeout = thrower; globalThis.setInterval = thrower;
  globalThis.requestAnimationFrame = thrower; globalThis.queueMicrotask = thrower;
  ["Intl", "WeakRef", "FinalizationRegistry"].forEach(function (name) {
    Object.defineProperty(globalThis, name, { get: thrower, configurable: false });
  });
  globalThis.window = globalThis; globalThis.self = globalThis;
  globalThis.__aipMotions = {};
  (function () {
    var keys = Object.keys, create = Object.create, isArray = Array.isArray, stringify = JSON.stringify;
    var parse = JSON.parse, hasOwn = Function.prototype.call.bind(Object.prototype.hasOwnProperty);
    var CHANNELS = create(null);
    CHANNELS.x = CHANNELS.y = CHANNELS.scale = CHANNELS.rotate = CHANNELS.overlay = true;
    var THREW = create(null);
    var marked = create(null);
    var prim = function (v) {
      var type = typeof v;
      return v === undefined || v === null || type === "number" || type === "string" || type === "boolean" ? v : "[" + type + "]";
    };
    var definition = function (name) {
      try {
        var reg = globalThis.__aipMotions;
        if (!reg || !hasOwn(reg, name)) return null;
        var def = reg[name];
        return def && typeof def.frame === "function" ? def : null;
      } catch (e) { return null; }
    };
    var rand = function (id) {
      var h = 2166136261;
      for (var k = 0; k < id.length; k++) h = Math.imul(h ^ id.charCodeAt(k), 16777619) >>> 0;
      return function (i) {
        var x = Math.imul(h ^ (((i | 0) + 0x9e3779b9) >>> 0), 2246822519) >>> 0;
        x = Math.imul(x ^ (x >>> 13), 3266489917) >>> 0;
        return (((x ^ (x >>> 16)) >>> 0) / 4294967296) * 2 * Math.PI;
      };
    };
    var ok = function (spec) {
      return !!spec && typeof spec === "object" && isFinite(spec.min) && isFinite(spec.max) && spec.min < spec.max &&
        Math.abs(spec.min) <= ${MAG} && Math.abs(spec.max) <= ${MAG} && isFinite(spec.default) &&
        spec.default >= spec.min && spec.default <= spec.max;
    };
    var entries = function () {
      var now = create(null), reg = globalThis.__aipMotions;
      if (!reg || (typeof reg !== "object" && typeof reg !== "function")) return now;
      var own = keys(reg);
      for (var i = 0; i < own.length; i++) {
        try {
          var d = reg[own[i]], live = d && (typeof d === "object" || typeof d === "function");
          now[own[i]] = [d, live ? d.frame : undefined, live ? d.params : undefined];
        } catch (e) { now[own[i]] = [THREW, THREW, THREW]; }
      }
      return now;
    };
    var api = {
      mark: function () { marked = entries(); return ""; },
      changed: function () {
        var now = entries(), out = "", seen = create(null), lists = [keys(now), keys(marked)];
        for (var j = 0; j < 2; j++) {
          for (var i = 0; i < lists[j].length; i++) {
            var k = lists[j][i];
            if (k in seen) continue;
            seen[k] = true;
            var a = marked[k], b = now[k];
            if (!a || !b || a[0] !== b[0] || a[1] !== b[1] || a[2] !== b[2]) out += (out ? "," : "") + stringify(k);
          }
        }
        return "[" + out + "]";
      },
      decl: function (name) {
        var def = definition(name);
        if (!def) return "missing";
        var decl = def.params === undefined ? {} : def.params;
        if (!decl || typeof decl !== "object" || isArray(decl) || keys(decl).length > 8) return "invalid";
        var own = keys(decl), p = create(null);
        for (var i = 0; i < own.length; i++) {
          if (!ok(decl[own[i]])) return "invalid";
          var q = create(null);
          q.min = prim(decl[own[i]].min); q.max = prim(decl[own[i]].max); q.default = prim(decl[own[i]].default);
          p[own[i]] = q;
        }
        return stringify(p);
      },
      frame: function (name, paramsJson, id, t, d) {
        var def = definition(name);
        if (!def) return "";
        var values = parse(paramsJson), decl = parse(api.decl(name)), names = keys(decl), p = {};
        for (var i = 0; i < names.length; i++) {
          var key = names[i], v = hasOwn(values, key) ? values[key] : decl[key].default;
          p[key] = Math.min(decl[key].max, Math.max(decl[key].min, v));
        }
        var out = def.frame(t, d, p, rand(id));
        if (!out || typeof out !== "object" || isArray(out)) return "null";
        var r = create(null), names = ["x", "y", "scale", "rotate"];
        for (var n = 0; n < names.length; n++) { var c = out[names[n]]; if (c !== undefined) r[names[n]] = prim(c); }
        var o = out.overlay;
        if (o !== undefined) {
          if (!o || typeof o !== "object") r.overlay = prim(o);
          else { var ov = create(null); ov.opacity = prim(o.opacity); ov.color = prim(o.color); r.overlay = ov; }
        }
        var own = keys(out);
        for (var m = 0; m < own.length; m++) if (!(own[m] in CHANNELS)) r[own[m]] = "[unknown]";
        return stringify(r);
      },
    };
    Object.defineProperty(globalThis, "__aipSampler", { value: Object.freeze(api), writable: false, configurable: false });
  })();
  Object.freeze(Math);
  Object.freeze(JSON);
`;
function entry(ctx, code) {
  const text = vm.runInContext("__aipSampler." + code, ctx, { timeout: 250 });
  if (typeof text !== "string") throw new Error("output");
  return text;
}
function realm() {
  const ctx = vm.createContext({}, { codeGeneration: { strings: false, wasm: false }, microtaskMode: "afterEvaluate" });
  vm.runInContext(SETUP, ctx);
  const errors = new Map();
  const lastSetBy = new Map();
  const strays = new Set();
  for (const script of input.scripts) {
    let changed = [];
    try {
      entry(ctx, "mark()");
      vm.runInContext(script.source, ctx, { timeout: 1000 });
    } catch (e) {
      if (!errors.has(script.name)) errors.set(script.name, e);
    }
    try { changed = JSON.parse(entry(ctx, "changed()")); } catch (e) { if (!errors.has(script.name)) errors.set(script.name, e); }
    for (const name of changed) {
      lastSetBy.set(name, script.name);
      if (name !== script.name) strays.add(script.name);
    }
  }
  for (const [name, by] of lastSetBy) if (by !== name) strays.add(name);
  return { ctx, errors, strays };
}
function bad(out) {
  if (!out || typeof out !== "object") return true;
  for (const key of Object.keys(out)) {
    if (key === "overlay") {
      const o = out.overlay;
      if (!o || !Number.isFinite(o.opacity) || o.opacity < 0 || o.opacity > 1) return true;
      if (o.color !== undefined && !COLOR.test(String(o.color))) return true;
    } else if (key === "scale") {
      if (!Number.isFinite(out.scale) || out.scale < 1 || out.scale > 3) return true;
    } else if (key in LIMITS) {
      if (!Number.isFinite(out[key]) || Math.abs(out[key]) > LIMITS[key]) return true;
    } else return true;
  }
  return false;
}
function pass(ctx, host) {
  const outs = [];
  for (let k = 0; k < 34; k++) {
    const t = Math.min(host.duration * k / 33, host.duration - 1e-6);
    const text = entry(ctx, `frame(${JSON.stringify(host.name)}, ${JSON.stringify(JSON.stringify(host.params))}, ${JSON.stringify(host.id)}, ${t}, ${host.duration})`);
    if (text === "") throw new Error("output");
    const out = JSON.parse(text);
    if (bad(out)) throw new Error("output");
    outs.push(text);
  }
  return outs.join("|");
}
function thrown(e) {
  const text = String(e && e.message);
  if (/timed out/.test(text)) return "motion_too_slow";
  if (text === "output") return "motion_output_invalid";
  return text.indexOf(STUB) >= 0 || (e && e.name === "ReferenceError") ? "motion_nondeterministic" : "motion_definition_invalid";
}
const a = realm();
const b = realm();
const codes = [];
for (const host of input.hosts) {
  if (!input.scripts.some((script) => script.name === host.name)) { codes.push("motion_definition_missing"); continue; }
  if (a.errors.has(host.name)) { codes.push(thrown(a.errors.get(host.name))); continue; }
  if (a.strays.has(host.name)) { codes.push("motion_definition_invalid"); continue; }
  let decl;
  try { decl = entry(a.ctx, `decl(${JSON.stringify(host.name)})`); } catch (e) { codes.push("motion_definition_invalid"); continue; }
  if (decl === "missing") { codes.push("motion_definition_missing"); continue; }
  if (decl === "invalid") { codes.push("motion_definition_invalid"); continue; }
  const declared = JSON.parse(decl);
  if (Object.keys(host.params).some((name) => !Object.prototype.hasOwnProperty.call(declared, name) ||
      host.params[name] < declared[name].min || host.params[name] > declared[name].max)) {
    codes.push("motion_params_invalid");
    continue;
  }
  let first;
  try { first = pass(a.ctx, host); } catch (e) {
    const code = thrown(e);
    codes.push(code === "motion_definition_invalid" ? "motion_output_invalid" : code);
    continue;
  }
  for (const ctx of [a.ctx, b.ctx]) {
    let again;
    try { again = pass(ctx, host); } catch (e) {
      codes.push(thrown(e) === "motion_too_slow" ? "motion_too_slow" : "motion_nondeterministic");
      break;
    }
    if (again !== first) { codes.push("motion_nondeterministic"); break; }
  }
}
process.stdout.write(JSON.stringify(codes));
"""


def sample(elements, motion_scripts=()):
    """Error codes from running each valid motion host's definition, or [] when there are
    none, when Node is absent, or when the static checks already refuse the index."""
    node = shutil.which("node")
    scripts = [{"name": (name or "").strip(), "source": text} for name, text in motion_scripts]
    named = {script["name"] for script in scripts}
    hosts = []
    for tag, attrs, depth in elements:
        if depth or attrs.get("data-aip-effect") != "motion" or not _motion_host_ok(attrs):
            continue
        duration = _number(attrs.get("data-duration"))
        name = attrs["data-effect-motion"].strip()
        if duration and duration > 0 and name in named:
            hosts.append({"id": attrs.get("data-composition-id") or "", "name": name, "duration": duration,
                          "params": motion_params(attrs.get("data-effect-params"))})
    if node is None or not hosts:
        return []
    try:
        done = subprocess.run([node, "-e", _SAMPLER], input=json.dumps({"hosts": hosts, "scripts": scripts}),
                              capture_output=True, text=True, timeout=30, check=False)
        found = json.loads(done.stdout) if done.returncode == 0 else []
    except (OSError, subprocess.TimeoutExpired, ValueError):
        return []
    return list(dict.fromkeys(str(code) for code in found))

SHA-256: 4684a48ddeafc3f2c46f8cdd1d4ec7377b3c05e3a5cb48966f2be819e75247b5