Download src/lib/passthrough.ts from Zero124/VR-MEDIA: direct link, hf CLI and curl.
- Browser
- Download file 11.5 kB
-
https://huggingface.co/spaces/Zero124/VR-MEDIA/resolve/main/src/lib/passthrough.ts
- Command line
-
hf download hf://spaces/Zero124/VR-MEDIA/src/lib/passthrough.ts
-
curl -L -o passthrough.ts https://huggingface.co/spaces/Zero124/VR-MEDIA/resolve/main/src/lib/passthrough.ts
11.5 kB
| // Passthrough helper — decides HOW each video should be keyed for DeoVR | |
| // passthrough (AR) playback, and detects the chroma-key COLOR from the | |
| // filename (all colors + hex) with an optional auto-detected fallback from a | |
| // sampled frame (written by the thumbnail generator). | |
| // | |
| // IMPORTANT — how DeoVR passthrough actually works on a SELF-HOSTED feed | |
| // (verified against https://deovr.com/app/doc and the passthrough docs): | |
| // | |
| // * ALPHA passthrough — DeoVR AUTO-enables it when the video's URL filename | |
| // contains the token `_ALPHA` AND the video is | |
| // fisheye. This is the only passthrough type DeoVR | |
| // auto-detects from a self-hosted deeplink. The video | |
| // must be authored with a packed alpha channel. | |
| // * CHROMA-KEY (color) — DeoVR keys out a solid color background using its | |
| // Hue / Saturation / Brightness / Color Range / | |
| // Falloff controls. The feed CAN carry these AND an | |
| // auto-enable flag: DeoVR reads a lowercase | |
| // `"chromakey"` block ({enabled, hasAlpha, h, s, v, | |
| // opacity, threshold}) from the video JSON. Setting | |
| // `enabled:"true"` turns passthrough on automatically | |
| // at launch and applies the color — this is how SLR | |
| // videos "just work." (Verified against the xbvr | |
| // reference feed: DeoSceneChromaKey in | |
| // xbapps/xbvr pkg/api/deovr.go.) | |
| // | |
| // So: forcing `_ALPHA` on a green/blue/black-screen (non-alpha) video BREAKS | |
| // passthrough, because DeoVR then looks for a packed alpha channel that isn't | |
| // there. We therefore detect the correct METHOD per video, only inject `_ALPHA` | |
| // for genuine alpha videos, and emit the `chromakey` block for every | |
| // passthrough video (hasAlpha:"true" for alpha; the detected color for chroma) | |
| // with enabled:"true" so passthrough auto-enables on launch. | |
| import { existsSync, readFileSync } from "node:fs"; | |
| import { join } from "node:path"; | |
| export type PassthroughMethod = "alpha" | "chroma" | "none"; | |
| export interface ChromaColor { | |
| /** Canonical color name, e.g. "green". */ | |
| name: string; | |
| /** Hex string like "#00ff00". */ | |
| hex: string; | |
| /** Hue in degrees 0-360. */ | |
| hue: number; | |
| /** Saturation 0-100. */ | |
| saturation: number; | |
| /** Brightness/Value 0-100. */ | |
| brightness: number; | |
| /** How the color was determined. */ | |
| source: "filename" | "auto-frame" | "default"; | |
| } | |
| /** | |
| * DeoVR passthrough block, serialized into the feed as the lowercase | |
| * `"chromakey"` field. This is the field DeoVR reads to AUTO-enable passthrough | |
| * on launch (verified against the xbvr reference feed implementation: | |
| * DeoSceneChromaKey in xbapps/xbvr pkg/api/deovr.go). | |
| * | |
| * IMPORTANT: DeoVR expects `enabled` and `hasAlpha` as STRING booleans | |
| * ("true" / "false"), not JSON booleans. | |
| * - enabled : "true" turns passthrough ON automatically when the video opens. | |
| * - hasAlpha : "true" for alpha-packed fisheye videos (uses the embedded | |
| * alpha channel); "false" for color chroma-key videos. | |
| * - h/s/v : key color in HSV (hue 0-360, saturation 0-100, value 0-100). | |
| * - threshold: color range / tolerance (how wide a band of color to remove). | |
| * - opacity : falloff / edge softness of the transparency. | |
| */ | |
| export interface ChromaKey { | |
| enabled: string; // "true" | "false" | |
| hasAlpha: string; // "true" | "false" | |
| h: number; // hue 0-360 | |
| s: number; // saturation 0-100 | |
| v: number; // brightness/value 0-100 | |
| opacity: number; // falloff / edge softness | |
| threshold: number; // color range / tolerance | |
| } | |
| const THUMB_DIR = process.env.THUMBNAIL_DIR || "/data/thumbnails"; | |
| /** | |
| * Named color table. Hue is standard HSV degrees (0-360). "black" and "white" | |
| * are achromatic so hue is 0 and we rely on brightness + a wide range. | |
| * "All types of colors" are covered by these names plus any `#rrggbb` / | |
| * `rrggbb` hex token found in the filename. | |
| */ | |
| export const NAMED_COLORS: Record<string, { hex: string; hue: number; saturation: number; brightness: number; aliases: string[] }> = { | |
| green: { hex: "#00ff00", hue: 120, saturation: 100, brightness: 100, aliases: ["green", "greenscreen", "green-screen", "chromagreen", "lime"] }, | |
| blue: { hex: "#0000ff", hue: 240, saturation: 100, brightness: 100, aliases: ["blue", "bluescreen", "blue-screen", "chromablue"] }, | |
| cyan: { hex: "#00ffff", hue: 180, saturation: 100, brightness: 100, aliases: ["cyan", "aqua", "teal"] }, | |
| magenta: { hex: "#ff00ff", hue: 300, saturation: 100, brightness: 100, aliases: ["magenta", "pink", "fuchsia", "chromapink"] }, | |
| red: { hex: "#ff0000", hue: 0, saturation: 100, brightness: 100, aliases: ["red", "redscreen"] }, | |
| yellow: { hex: "#ffff00", hue: 60, saturation: 100, brightness: 100, aliases: ["yellow"] }, | |
| orange: { hex: "#ff8000", hue: 30, saturation: 100, brightness: 100, aliases: ["orange"] }, | |
| purple: { hex: "#8000ff", hue: 270, saturation: 100, brightness: 100, aliases: ["purple", "violet"] }, | |
| black: { hex: "#000000", hue: 0, saturation: 0, brightness: 0, aliases: ["black", "blackscreen", "black-screen"] }, | |
| white: { hex: "#ffffff", hue: 0, saturation: 0, brightness: 100, aliases: ["white", "whitescreen", "white-screen"] }, | |
| gray: { hex: "#808080", hue: 0, saturation: 0, brightness: 50, aliases: ["gray", "grey"] }, | |
| }; | |
| function hexToHsv(hex: string): { hue: number; saturation: number; brightness: number } { | |
| const m = hex.replace(/^#/, ""); | |
| const r = parseInt(m.slice(0, 2), 16) / 255; | |
| const g = parseInt(m.slice(2, 4), 16) / 255; | |
| const b = parseInt(m.slice(4, 6), 16) / 255; | |
| const max = Math.max(r, g, b); | |
| const min = Math.min(r, g, b); | |
| const d = max - min; | |
| let h = 0; | |
| if (d !== 0) { | |
| if (max === r) h = ((g - b) / d) % 6; | |
| else if (max === g) h = (b - r) / d + 2; | |
| else h = (r - g) / d + 4; | |
| h *= 60; | |
| if (h < 0) h += 360; | |
| } | |
| const s = max === 0 ? 0 : d / max; | |
| return { hue: Math.round(h), saturation: Math.round(s * 100), brightness: Math.round(max * 100) }; | |
| } | |
| /** Word-boundary token match to avoid false hits (e.g. "red" in "hundred"). */ | |
| function tokenMatch(haystack: string, token: string): boolean { | |
| const esc = token.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); | |
| return new RegExp(`(^|[^a-z0-9])${esc}([^a-z0-9]|$)`, "i").test(haystack); | |
| } | |
| /** | |
| * Detect a chroma-key color from a filename. Recognizes: | |
| * - named colors + "*screen" aliases (green, blue, black, white, magenta, …) | |
| * - a bare/prefixed hex token: `#00ff00`, `_00ff00_`, `-ff00ff-` | |
| * Returns null when no explicit color hint is present. | |
| */ | |
| export function detectColorFromFilename(name: string): ChromaColor | null { | |
| const lower = name.toLowerCase(); | |
| // Hex first (most explicit). | |
| const hexMatch = lower.match(/(?:^|[^a-f0-9])#?([0-9a-f]{6})(?:[^a-f0-9]|$)/i); | |
| if (hexMatch) { | |
| const hex = `#${hexMatch[1]}`; | |
| const hsv = hexToHsv(hex); | |
| return { name: hex, hex, ...hsv, source: "filename" }; | |
| } | |
| for (const [name, def] of Object.entries(NAMED_COLORS)) { | |
| for (const alias of def.aliases) { | |
| if (tokenMatch(lower, alias)) { | |
| return { | |
| name, | |
| hex: def.hex, | |
| hue: def.hue, | |
| saturation: def.saturation, | |
| brightness: def.brightness, | |
| source: "filename", | |
| }; | |
| } | |
| } | |
| } | |
| return null; | |
| } | |
| /** | |
| * Read an auto-detected background color for a video, if the thumbnail | |
| * generator wrote a sidecar `<id>.color.json`. Best-effort; returns null when | |
| * absent or unreadable. | |
| */ | |
| export function readAutoColor(videoId: string): ChromaColor | null { | |
| try { | |
| const p = join(THUMB_DIR, `${videoId}.color.json`); | |
| if (!existsSync(p)) return null; | |
| const raw = JSON.parse(readFileSync(p, "utf8")); | |
| if (!raw || typeof raw.hex !== "string") return null; | |
| const hsv = hexToHsv(raw.hex); | |
| return { | |
| name: raw.name || raw.hex, | |
| hex: raw.hex, | |
| hue: typeof raw.hue === "number" ? raw.hue : hsv.hue, | |
| saturation: typeof raw.saturation === "number" ? raw.saturation : hsv.saturation, | |
| brightness: typeof raw.brightness === "number" ? raw.brightness : hsv.brightness, | |
| source: "auto-frame", | |
| }; | |
| } catch { | |
| return null; | |
| } | |
| } | |
| /** Resolve the default chroma color from env (PASSTHROUGH_COLOR), fallback green. */ | |
| export function defaultChromaColor(): ChromaColor { | |
| const envColor = (process.env.PASSTHROUGH_COLOR || "green").toLowerCase().trim(); | |
| if (/^#?[0-9a-f]{6}$/i.test(envColor)) { | |
| const hex = envColor.startsWith("#") ? envColor : `#${envColor}`; | |
| return { name: hex, hex, ...hexToHsv(hex), source: "default" }; | |
| } | |
| const def = NAMED_COLORS[envColor] || NAMED_COLORS.green; | |
| return { name: envColor in NAMED_COLORS ? envColor : "green", hex: def.hex, hue: def.hue, saturation: def.saturation, brightness: def.brightness, source: "default" }; | |
| } | |
| /** | |
| * Resolve the chroma color for a video: explicit filename hint wins, then the | |
| * auto-detected frame color, then the configured default. | |
| */ | |
| export function resolveChromaColor(name: string, videoId: string): ChromaColor { | |
| return ( | |
| detectColorFromFilename(name) || | |
| readAutoColor(videoId) || | |
| defaultChromaColor() | |
| ); | |
| } | |
| /** | |
| * Build the DeoVR passthrough block for a CHROMA-KEY (color background) video. | |
| * `enabled: "true"` makes DeoVR turn passthrough on automatically at launch and | |
| * apply the detected key color, so the viewer doesn't have to pick it manually | |
| * (SLR-style behavior). Range/falloff default to sensible values and can be | |
| * overridden with PASSTHROUGH_RANGE / PASSTHROUGH_FALLOFF. | |
| */ | |
| export function buildChromaKey(color: ChromaColor): ChromaKey { | |
| // Achromatic keys (black/white/gray) need a wider range and rely on | |
| // brightness; colored keys use a moderate range with a soft falloff. | |
| const achromatic = color.saturation <= 10; | |
| const range = Number(process.env.PASSTHROUGH_RANGE || (achromatic ? 400 : 320)); | |
| const falloff = Number(process.env.PASSTHROUGH_FALLOFF || 1000); | |
| return { | |
| enabled: "true", | |
| hasAlpha: "false", | |
| h: color.hue, | |
| s: color.saturation, | |
| v: color.brightness, | |
| threshold: Number.isFinite(range) ? range : achromatic ? 400 : 320, | |
| opacity: Number.isFinite(falloff) ? falloff : 1000, | |
| }; | |
| } | |
| /** | |
| * Build the DeoVR passthrough block for an ALPHA-PACKED fisheye video. DeoVR | |
| * uses the embedded alpha channel, so no color params are needed; `enabled` and | |
| * `hasAlpha` are both "true" so passthrough auto-enables on launch. | |
| */ | |
| export function buildAlphaChromaKey(): ChromaKey { | |
| return { enabled: "true", hasAlpha: "true", h: 0, s: 0, v: 0, opacity: 0, threshold: 0 }; | |
| } | |
| const ALPHA_TOKENS = ["_alpha", "-alpha", " alpha", "alpha_", "alpha-", "deoalpha", "alphapack", "alpha_packed", "alphapacked"]; | |
| /** True when the filename explicitly indicates a packed alpha-channel video. */ | |
| export function filenameIndicatesAlpha(name: string): boolean { | |
| const lower = name.toLowerCase(); | |
| if (ALPHA_TOKENS.some((t) => lower.includes(t))) return true; | |
| // standalone "alpha" word | |
| return tokenMatch(lower, "alpha"); | |
| } | |