// Localiza Godot, FFmpeg e FFprobe sem varrer o disco: // 1. variável de ambiente (GODOT_BIN, FFMPEG_BIN, FFPROBE_BIN); // 2. PATH; // 3. só para a Godot: executável oficial extraído em /.tools/godot-*/. import fs from "node:fs"; import path from "node:path"; import { LOCAL_TOOLS_DIR } from "./paths.ts"; import { runProcess, succeeded } from "./process.ts"; export type ToolName = "godot" | "ffmpeg" | "ffprobe"; export type ToolSource = "env" | "PATH" | "local-tools"; export type Tool = { name: ToolName; path: string; source: ToolSource }; export class MissingToolError extends Error {} const ENV_VARS: Record = { godot: "GODOT_BIN", ffmpeg: "FFMPEG_BIN", ffprobe: "FFPROBE_BIN" }; const PATH_NAMES: Record = { godot: ["godot", "godot4"], ffmpeg: ["ffmpeg"], ffprobe: ["ffprobe"], }; export function findTool(name: ToolName): Tool { const envVar = ENV_VARS[name]; const fromEnv = process.env[envVar]; if (fromEnv) { // Se o usuário apontou um caminho, não trocamos por outro em silêncio. if (!isFile(fromEnv)) throw new MissingToolError(`${envVar} aponta para "${fromEnv}", que não existe ou não é arquivo`); return { name, path: fromEnv, source: "env" }; } const onPath = findOnPath(PATH_NAMES[name]); if (onPath) return { name, path: onPath, source: "PATH" }; if (name === "godot") { const local = findLocalGodot(); if (local) return { name, path: local, source: "local-tools" }; } throw new MissingToolError( `${name} não encontrado. Instale ou defina ${envVar} com o caminho do executável` + (name === "godot" ? " (ou extraia a Godot oficial em .tools/godot-/)." : "."), ); } export function findOnPath(names: string[]): string | null { const dirs = (process.env.PATH ?? "").split(path.delimiter).filter(Boolean); const exts = process.platform === "win32" ? (process.env.PATHEXT ?? ".EXE;.CMD;.BAT").split(";").map((e) => e.toLowerCase()) : [""]; for (const dir of dirs) { for (const name of names) { for (const ext of exts) { const candidate = path.join(dir, name + ext); if (isFile(candidate)) return candidate; } } } return null; } /** Godot oficial em .tools/godot-/: escolhe o executável principal (não o _console). */ function findLocalGodot(): string | null { if (!fs.existsSync(LOCAL_TOOLS_DIR)) return null; const pattern = process.platform === "win32" ? /^Godot_v[\d.]+-stable_win64\.exe$/ : /^Godot_v[\d.]+-stable_linux\.x86_64$/; const dirs = fs.readdirSync(LOCAL_TOOLS_DIR).filter((d) => d.startsWith("godot-")).sort().reverse(); for (const dir of dirs) { const full = path.join(LOCAL_TOOLS_DIR, dir); const exe = fs.readdirSync(full).find((f) => pattern.test(f)); if (exe) return path.join(full, exe); } return null; } export async function toolVersion(tool: Tool): Promise { const args = tool.name === "godot" ? ["--version"] : ["-hide_banner", "-version"]; const r = await runProcess(tool.path, args, { timeoutMs: 30_000 }); if (!succeeded(r)) return "desconhecida"; const first = r.stdout.trim().split(/\r?\n/)[0] ?? ""; return first.replace(/ Copyright.*$/, ""); } export type EncoderSupport = { libx264: boolean; h264_nvenc: boolean; libopenh264: boolean; aac: boolean }; export async function ffmpegEncoders(ffmpeg: Tool): Promise { const r = await runProcess(ffmpeg.path, ["-hide_banner", "-encoders"], { timeoutMs: 30_000 }); const has = (name: string) => new RegExp(`^\\s*[VA][\\w.]{5}\\s+${name}\\s`, "m").test(r.stdout); return { libx264: has("libx264"), h264_nvenc: has("h264_nvenc"), libopenh264: has("libopenh264"), aac: has("aac") }; } /** Escolhe o encoder H.264: libx264 primeiro; alternativas documentadas no README. */ export function chooseVideoEncoder(enc: EncoderSupport): { name: string; args: string[] } { if (enc.libx264) return { name: "libx264", args: ["-c:v", "libx264", "-preset", "medium", "-crf", "18"] }; if (enc.h264_nvenc) return { name: "h264_nvenc", args: ["-c:v", "h264_nvenc", "-preset", "p5", "-cq", "19"] }; if (enc.libopenh264) return { name: "libopenh264", args: ["-c:v", "libopenh264", "-b:v", "6M"] }; throw new MissingToolError("o FFmpeg instalado não tem encoder H.264 (libx264, h264_nvenc ou libopenh264). Rode `ffmpeg -encoders` e instale um build com libx264."); } function isFile(p: string): boolean { try { return fs.statSync(p).isFile(); } catch { return false; } }