You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Node.js通过RTSP链接捕获视频片段报错:Cannot find ffmpeg

解决Error: Cannot find ffmpeg问题及RTSP视频录制优化

错误原因

这个报错的核心是系统未安装FFmpeg,或者fluent-ffmpeg模块无法定位到FFmpeg的可执行文件。

解决步骤

  1. 安装FFmpeg

    • Windows:下载FFmpeg官方编译包,解压后将包含ffmpeg.exe的bin目录添加到系统环境变量PATH,重启终端或编辑器后生效。
    • macOS:通过Homebrew执行命令安装:brew install ffmpeg
    • Linux:使用系统包管理器安装,例如Ubuntu/Debian:sudo apt update && sudo apt install ffmpeg;CentOS/RHEL:sudo yum install ffmpeg
  2. 手动指定FFmpeg路径(可选,安装后仍报错时用)
    如果安装后模块还是找不到FFmpeg,在代码开头手动指定可执行文件路径:

    const ffmpeg = require('fluent-ffmpeg');
    // 根据系统替换路径
    ffmpeg.setFfmpegPath('C:/ffmpeg/bin/ffmpeg.exe'); // Windows示例
    // ffmpeg.setFfmpegPath('/usr/local/bin/ffmpeg'); // macOS/Linux示例
    

原代码优化建议

原代码用setTimeout+kill终止录制容易导致MP4文件损坏(MP4需要正确写入文件头),建议直接用FFmpeg自带的时长参数控制录制时间,更可靠:

const ffmpeg = require('fluent-ffmpeg');

// 手动指定FFmpeg路径(如果需要)
// ffmpeg.setFfmpegPath('你的FFmpeg路径');

const streamUrl = 'mycamlink';
const outputPath = 'output.mp4';

const command = ffmpeg(streamUrl)
  .inputOptions('-rtsp_transport', 'tcp')
  .inputOptions('-t', '10') // 指定录制时长10秒
  .outputOptions('-c', 'copy')
  .outputOptions('-flags', '+global_header')
  .output(outputPath)
  .on('end', () => console.log('录制完成'))
  .on('error', err => console.error('录制出错:', err));

command.run();

内容的提问来源于stack exchange,提问作者Matheus Leles

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 02:17:04