Skip to content

Latest commit

 

History

History
234 lines (166 loc) · 5.08 KB

File metadata and controls

234 lines (166 loc) · 5.08 KB

修复 FFmpeg 路径问题 ✅

创建时间: 2025-02-03 错误: 'ffmpeg.exe' 不是内部或外部命令


问题原因

错误位置

文件: apps/desktop/src/main/pipeline-realtime-v2.cjs 行号: 375

错误代码:

const process = spawn('ffmpeg', args);

问题分析

  1. 直接使用 'ffmpeg'

    • 代码直接使用 spawn('ffmpeg', ...)
    • 依赖系统 PATH 环境变量
    • Windows 下可能找不到 ffmpeg.exe
  2. 其他地方正确使用

    • generator.cjs 中的 runFFmpeg 函数正确使用了完整路径
    • 其他模块都使用 ffmpeg-static 提供的完整路径
  3. 不一致导致错误

    • 流水线模块使用相对路径
    • 系统找不到 FFmpeg 可执行文件

解决方案

修改 1: 导入依赖

文件: apps/desktop/src/main/pipeline-realtime-v2.cjs 位置: 行 1-15

添加:

const { spawn } = require('child_process');

// FFmpeg 路径
const ffmpegStatic = require('ffmpeg-static');
const { getBinaryPath } = require('../utils/binaryPath');
const ffmpegPath = getBinaryPath(ffmpegStatic);

修改 2: 使用完整路径

文件: apps/desktop/src/main/pipeline-realtime-v2.cjs 位置: 行 378-384

修改前:

logFn('info', `🔗 拼接 ${blockFiles.length} 个块...`);

await new Promise((resolve, reject) => {
  const process = spawn('ffmpeg', args);

修改后:

logFn('info', `🔗 拼接 ${blockFiles.length} 个块...`);

// 获取 FFmpeg 可执行文件路径
const ffmpegExecutable = typeof ffmpegPath === 'string' ? ffmpegPath : ffmpegPath.path;

await new Promise((resolve, reject) => {
  const process = spawn(ffmpegExecutable, args, { windowsHide: true });

为什么需要这个修复

FFmpeg 路径获取

项目使用 ffmpeg-static 包来管理 FFmpeg:

const ffmpegStatic = require('ffmpeg-static');
// 返回: "F:\\...\\node_modules\\ffmpeg-static\\bin\\win32\\x64\\ffmpeg.exe"

getBinaryPath 工具函数

// apps/desktop/src/main/utils/binaryPath.js
function getBinaryPath(binary) {
  // 处理不同格式的 binary 对象
  if (typeof binary === 'string') {
    return binary;
  }
  if (binary && binary.path) {
    return binary.path;
  }
  throw new Error('Invalid binary');
}

为什么不能直接用 'ffmpeg'

  1. Windows 系统限制

    • FFmpeg 不在系统 PATH 中
    • 需要完整路径才能执行
  2. 依赖管理

    • 项目通过 npm 管理依赖
    • FFmpeg 在 node_modules
    • 必须使用完整路径
  3. 跨平台兼容性

    • 不同操作系统路径不同
    • ffmpeg-static 自动处理平台差异

验证修复

检查点

  1. 导入正确

    const ffmpegStatic = require('ffmpeg-static');
    const { getBinaryPath } = require('../utils/binaryPath');
    const ffmpegPath = getBinaryPath(ffmpegStatic);
  2. 路径解析正确

    const ffmpegExecutable = typeof ffmpegPath === 'string'
      ? ffmpegPath
      : ffmpegPath.path;
  3. Spawn 使用正确

    const process = spawn(ffmpegExecutable, args, { windowsHide: true });

测试命令

# 启动应用
npm run dev:desktop

# 生成视频(应该不再报错)
# 选择视频片段并点击生成

相关文件

修改的文件

  1. apps/desktop/src/main/pipeline-realtime-v2.cjs
    • 行 10: 添加 spawn 导入
    • 行 13-15: 添加 FFmpeg 路径配置
    • 行 380-381: 解析可执行文件路径
    • 行 384: 使用完整路径

正确的实现参考

apps/desktop/src/main/generator.cjs:712-760

async function runFFmpeg(args, inputFile = null, errorLogPath = null) {
  return new Promise((resolve, reject) => {
    const ffmpegExecutable = typeof ffmpegPath === 'string'
      ? ffmpegPath
      : ffmpegPath.path;

    // ...

    const child = spawn(ffmpegExecutable, args, options);
    // ...
  });
}

其他需要检查的地方

检查清单

  • pipeline-realtime-v2.cjs - ✅ 已修复
  • generator.cjs - ✅ 已正确
  • pipeline-realtime.cjs - 需要检查
  • 其他自定义 spawn 调用 - 需要检查

检查命令

# 搜索所有直接使用 'ffmpeg' 的地方
grep -r "spawn('ffmpeg'" apps/desktop/src/main/
grep -r "execSync.*ffmpeg" apps/desktop/src/main/

总结

修复内容

  1. 导入必要的模块

    • child_processspawn
    • ffmpeg-static
    • getBinaryPath 工具函数
  2. 使用完整路径

    • 解析 FFmpeg 可执行文件的完整路径
    • 传递给 spawn 而不是使用 'ffmpeg'
  3. 添加 windowsHide 选项

    • 避免显示 Windows 控制台窗口

影响

  • 🎯 修复: 'ffmpeg.exe' 不是内部或外部命令错误
  • 🎯 一致性: 所有模块使用相同的 FFmpeg 路径获取方式
  • 🎯 可靠性: 不依赖系统 PATH,使用包管理的 FFmpeg

修复完成! 🎉

现在流水线模块可以正确找到并执行 FFmpeg 了。请重新测试生成功能。