Repository: Mintplex-Labs/anything-llm
File: collector/utils/WhisperProviders/ffmpeg/index.js (JavaScript)

Code before:
   * Locates ffmpeg binary.
   * Uses fix-path on non-Windows platforms to ensure we can find ffmpeg.
   *
   * @async
   * @returns {Promise<string>} Path to ffmpeg binary
   * @throws {Error}
   */
  async getFFMPEGPath() {
    if (this.ffmpegPath) return this.ffmpegPath;

    if (
      process.env.FFMPEG_PATH &&
      this.isValidFFMPEG(process.env.FFMPEG_PATH)
    ) {
      this.ffmpegPath = process.env.FFMPEG_PATH;
      return this.ffmpegPath;
    }

    if (process.platform !== "win32" && !process.env.JEST_WORKER_ID) {
      try {
        const fixPath = await import("fix-path").then((m) => m.default);
        fixPath();
      } catch (error) {
        this.log("Could not load fix-path, using system PATH");
      }
    }

    const possibleCommands =
      process.platform === "win32" ? ["ffmpeg.exe", "ffmpeg"] : ["ffmpeg"];

    for (const cmd of possibleCommands) {
      try {
        const which = process.platform === "win32" ? "where" : "which";
        const result = execSync(`${which} ${cmd}`, { encoding: "utf8" }).trim();
        const candidatePath = result.split("\n")[0];

        if (this.isValidFFMPEG(candidatePath)) {
          this.ffmpegPath = candidatePath;
          return this.ffmpegPath;
        }
      } catch (error) {
        continue;
      }
    }

    throw new Error(
      "FFMPEG not found. Please install FFMPEG or set FFMPEG_PATH environment variable."
    );
  }

  /**

Review comment (suggestion):
Can `fix-path` not run in Jest or does it cause issues?

If so, instead of a code-level test patch, try to stub the import as a noop in the test that will call it. That way it does not resolve to anything that has an impact

Code after:
    console.log(`\x1b[35m[FFMPEG]\x1b[0m ${text}`, ...args);
  }

  /**
   * Locates ffmpeg binary.
   * Uses fix-path on non-Windows platforms to ensure we can find ffmpeg.
   *
   * @returns {string} Path to ffmpeg binary
   * @throws {Error}
   */
  get ffmpegPath() {
    if (this._ffmpegPath) return this._ffmpegPath;

    if (process.platform !== "win32") {
      try {
        const fixPath = require("fix-path");
        fixPath();
      } catch (error) {
        this.log("Could not load fix-path, using system PATH");
      }
    }

    try {
      const which = process.platform === "win32" ? "where" : "which";
      const result = execSync(`${which} ffmpeg`, { encoding: "utf8" }).trim();
      const candidatePath = result?.split("\n")?.[0]?.trim();
      if (!candidatePath) throw new Error("FFMPEG candidate path not found.");
      if (!this.isValidFFMPEG(candidatePath))
        throw new Error("FFMPEG candidate path is not valid ffmpeg binary.");

      this.log(`Found FFMPEG binary at ${candidatePath}`);
      this._ffmpegPath = candidatePath;
      return this._ffmpegPath;
    } catch (error) {
      this.log(error.message);
    }

    throw new Error("FFMPEG binary not found.");
  }

  /**
   * Validates that path points to a valid ffmpeg binary.
   * Runs ffmpeg -version command.
   *
   * @param {string} pathToTest - Path of ffmpeg binary
   * @returns {boolean}
   */
  isValidFFMPEG(pathToTest) {
    try {
      if (!pathToTest || !fs.existsSync(pathToTest)) return false;
      execSync(`"${pathToTest}" -version`, { encoding: "utf8", stdio: "pipe" });