Node.js错误:生成进程ffmpeg ChildProcessError

时间:2019-06-10 17:15:35

标签: node.js websocket ffmpeg

我只想制作像OBS这样的程序。

我的理想代码行为是在Node.js中创建一个子进程并执行FFMPEG命令,以将网络摄像头流发送到yourtube实时RTMP服务器。但是,实际行为是由node.js中使用的child-process-promise模块中的错误引起的。

我已经检查了几个问题,但是我没有足够的经验来理解它们,我希望有一个明确的解决方案。

我想那是因为我在Node环境中找不到FFMPEG的命令地址。还是从套接字环境调用是一个问题?

我检查了FFMPEG命令是否可以在Windows提示环境中使用。

※注意:FFMPEG环境变量已注册。

环境:Window10node.jsffmpeg

代码利用了一个简单的WebSocket示例。

当我第一次调查时,我认为唯一的方法就是使用“ fluent-effmpeg”。

我尝试使用“ fluent-ffmpeg”,但无法将笔记本电脑的网络摄像头设置为在Windows环境中运行并作为“ fluent-ffmppeg”命令的参数。

我也考虑过使用WebRTC,但我认为它不是供个人使用的,因为它是P2P连接。 (我还看到了如何将对等连接连接到像Janus这样的WebRTC服务器,但是我没有足够的参考资料来理解它。)

下面是问题的代码。

const SocketIO = require("socket.io");
const ffmpeg = require("fluent-ffmpeg");
const spawn = require("child-process-promise").spawn;

module.exports = server => {
  const io = SocketIO(server, { path: "/socket.io" });

  io.on("connection", socket => {
    const req = socket.request;
    const ip = req.headers["x-forwarded-for"] || req.connection.remoteAddress;
    console.log("새로운 클라이언트 접속!", ip, socket.id, req.ip);
    socket.on("disconnect", () => {
      console.log("클라이언트 접속해제", ip, socket.id);
      clearInterval(socket.interval);
    });
    socket.on("error", error => {
      console.error(error);
    });
    socket.on("reply", data => {
      console.log(data);
      ffmpeg_command();
    });
  });

  function ffmpeg_command() {
    let arg = [
      "-f",
      "lavfi",
      "-i",
      "anullsrc=r=16000:cl=mono",
      "-f",
      "dshow",
      "-ac",
      "2",
      "-i",
      "video='HP Truevision HD'",
      "-s",
      "1280x720",
      "-r",
      "10",
      "-vcodec",
      "libx264",
      "-pix_fmt",
      "yuv420p",
      "-preset",
      "ultrafast",
      "-r",
      "25",
      "-g",
      "20",
      "-b:v",
      "2500k",
      "-codec:a",
      "libmp3lame",
      "-ar",
      "44100",
      "-threads",
      "6",
      "-b:a",
      "11025",
      "-bufsize",
      "512k",
      "-f",
      "flv",
      "rtmp://a.rtmp.youtube.com/live2/8dfu-69k0-dxyw-896q"
    ];
    spawn("ffmpeg", arg).catch(e => {
      console.log(e);
    });
  }
};

这是错误:预期结果是您的网络摄像头正在运行,并且YouTube实时流传输成功。

{ ChildProcessError: `ffmpeg -f lavfi -i anullsrc=r=16000:cl=mono -f dshow -ac 2 -i video='HP Truevision HD' -s 1280x720 -r 10 -vcodec libx264 -pix_fmt yuv420p -preset ultrafast -r 25 -g 20 -b:v 2500k -codec:a libmp3lame -ar 44100 -threads 6 -b:a 11025 -bufsize 512k -f flv rtmp://a.rtmp.youtube.com/live2/8dfu-69k0-dxyw-896q` failed with code 1
    at ChildProcess.<anonymous> (C:\Users\Tricky\Desktop\Work\ESC\ESC_temp\node_modules\child-process-promise\lib\index.js:132:23)
    at ChildProcess.emit (events.js:182:13)
    at ChildProcess.cp.emit (C:\Users\Tricky\Desktop\Work\ESC\ESC_temp\node_modules\child-process-promise\node_modules\cross-spawn\lib\enoent.js:40:29)
    at maybeClose (internal/child_process.js:962:16)
    at Socket.stream.socket.on (internal/child_process.js:381:11)
    at Socket.emit (events.js:182:13)
    at Pipe._handle.close (net.js:606:12)
  name: 'ChildProcessError',
  code: 1,
  childProcess:
   ChildProcess {
     _events: { error: [Function], close: [Function] },
     _eventsCount: 2,
     _maxListeners: undefined,
     _closesNeeded: 3,
     _closesGot: 3,
     connected: false,
     signalCode: null,
     exitCode: 1,
     killed: false,
     spawnfile: 'ffmpeg',
     _handle: null,
     spawnargs:
      [ 'ffmpeg',
        '-f',
        'lavfi',
        '-i',
        'anullsrc=r=16000:cl=mono',
        '-f',
        'dshow',
        '-ac',
        '2',
        '-i',
        'video=\'HP Truevision HD\'',
        '-s',
        '1280x720',
        '-r',
        '10',
        '-vcodec',
        'libx264',
        '-pix_fmt',
        'yuv420p',
        '-preset',
        'ultrafast',
        '-r',
        '25',
        '-g',
        '20',
        '-b:v',
        '2500k',
        '-codec:a',
        'libmp3lame',
        '-ar',
        '44100',
        '-threads',
        '6',
        '-b:a',
        '11025',
        '-bufsize',
        '512k',
        '-f',
        'flv',
        'rtmp://a.rtmp.youtube.com/live2/8dfu-69k0-dxyw-896q' ],
     pid: 18928,
     stdin:
      Socket {
        connecting: false,
        _hadError: false,
        _handle: null,
        _parent: null,
        _host: null,
        _readableState: [ReadableState],
        readable: false,
        _events: [Object],
        _eventsCount: 1,
        _maxListeners: undefined,
        _writableState: [WritableState],
        writable: false,
        allowHalfOpen: false,
        _sockname: null,
        _pendingData: null,
        _pendingEncoding: '',
        server: null,
        _server: null,
        [Symbol(asyncId)]: 132,
        [Symbol(lastWriteQueueSize)]: 0,
        [Symbol(timeout)]: null,
        [Symbol(kBytesRead)]: 0,
        [Symbol(kBytesWritten)]: 0 },
     stdout:
      Socket {
        connecting: false,
        _hadError: false,
        _handle: null,
        _parent: null,
        _host: null,
        _readableState: [ReadableState],
        readable: false,
        _events: [Object],
        _eventsCount: 2,
        _maxListeners: undefined,
        _writableState: [WritableState],
        writable: false,
        allowHalfOpen: false,
        _sockname: null,
        _pendingData: null,
        _pendingEncoding: '',
        server: null,
        _server: null,
        write: [Function: writeAfterFIN],
        [Symbol(asyncId)]: 133,
        [Symbol(lastWriteQueueSize)]: 0,
        [Symbol(timeout)]: null,
        [Symbol(kBytesRead)]: 0,
        [Symbol(kBytesWritten)]: 0 },
     stderr:
      Socket {
        connecting: false,
        _hadError: false,
        _handle: null,
        _parent: null,
        _host: null,
        _readableState: [ReadableState],
        readable: false,
        _events: [Object],
        _eventsCount: 2,
        _maxListeners: undefined,
        _writableState: [WritableState],
        writable: false,
        allowHalfOpen: false,
        _sockname: null,
        _pendingData: null,
        _pendingEncoding: '',
        server: null,
        _server: null,
        write: [Function: writeAfterFIN],
        [Symbol(asyncId)]: 134,
        [Symbol(lastWriteQueueSize)]: 0,
        [Symbol(timeout)]: null,
        [Symbol(kBytesRead)]: 1615,
        [Symbol(kBytesWritten)]: 0 },
     stdio: [ [Socket], [Socket], [Socket] ],
     emit: [Function] },
  stdout: undefined,
  stderr: undefined }

0 个答案:

没有答案