如何在Node.js中从执行的子进程获取响应。

huangapple go评论64阅读模式
英文:

How can I get a response from exec child process in node js

问题

以下是代码部分的翻译:

exec("ffmpeg -i ./file.txt ./file2.txt");
var exec = require('child_process').exec;
const command = exec("ffmpeg -i ./file.txt ./file2.txt");

command.stdout.on('data', (data)=>{
  console.log(data); 
  return res.send({"Success:": data});
});
command.stderr.on('data', (data)=>{
  console.error(data);
  return res.send({"ERROR:": data});
});
"ERROR:": "ffmpeg version 2023-01-01-git-62da0b4a74-full_build-www.gyan.dev Copyright (c) 2000-2023 the FFmpeg developers\r\n  built with gcc 12.1.0 (Rev2, Built by MSYS2 project)\r\n  configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-bzlib --enable-lzma --enable-libsnappy --enable-zlib --enable-librist --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-libbluray --enable-libcaca --enable-sdl2 --enable-libaribb24 --enable-libdav1d --enable-libdavs2 --enable-libuavs3d --enable-libzvbi --enable-librav1e --enable-libsvtav1 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs2 --enable-libxvid --enable-libaom --enable-libjxl --enable-libopenjpeg --enable-libvpx --enable-mediafoundation --enable-libass --enable-frei0r --enable-libfreetype --enable-libfribidi --enable-liblensfun --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 --enable-libvpl --enable-libshaderc --enable-vulkan --enable-libplacebo --enable-opencl --enable-libcdio --enable-libgme --enable-libmodplug --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libshine --enable-libtheora --enable-libtwolame --enable-libvo-amrwbenc --enable-libilbc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-ladspa --enable-libbs2b --enable-libflite --enable-libmysofa --enable-librubberband --enable-libsoxr --enable-chromaprint\r\n"

如果您需要更多信息或有其他问题,请随时提问。

英文:

How can I get a response after executing this command below,

exec("ffmpeg -i ./file.txt ./file2.txt");

I want to get a response if the command execute successfully or not

So I tried this below

 var exec = require('child_process').exec;
const command = exec("ffmpeg -i ./file.txt ./file2.txt");

    command.stdout.on('data', (data)=>{
      console.log(data); 
      return res.send({"Success:": data});
  });
  command.stderr.on('data', (data)=>{
      console.error(data);
      return res.send({"ERROR:": data});
  });

But It returned the error below, even when the command worked fine

"ERROR:": "ffmpeg version 2023-01-01-git-62da0b4a74-full_build-www.gyan.dev Copyright (c) 2000-2023 the FFmpeg developers\r\n  built with gcc 12.1.0 (Rev2, Built by MSYS2 project)\r\n  configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-bzlib --enable-lzma --enable-libsnappy --enable-zlib --enable-librist --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-libbluray --enable-libcaca --enable-sdl2 --enable-libaribb24 --enable-libdav1d --enable-libdavs2 --enable-libuavs3d --enable-libzvbi --enable-librav1e --enable-libsvtav1 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs2 --enable-libxvid --enable-libaom --enable-libjxl --enable-libopenjpeg --enable-libvpx --enable-mediafoundation --enable-libass --enable-frei0r --enable-libfreetype --enable-libfribidi --enable-liblensfun --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 --enable-libvpl --enable-libshaderc --enable-vulkan --enable-libplacebo --enable-opencl --enable-libcdio --enable-libgme --enable-libmodplug --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libshine --enable-libtheora --enable-libtwolame --enable-libvo-amrwbenc --enable-libilbc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-ladspa --enable-libbs2b --enable-libflite --enable-libmysofa --enable-librubberband --enable-libsoxr --enable-chromaprint\r\n"

答案1

得分: 1

返回的“错误”实际上并不是错误,而是ffmpeg日志的一部分。手册中说:

默认情况下,程序将日志记录到stderr

在执行ffmpeg的过程中,可以有多个数据块发送到stdoutstderr,但是您的代码在第一个块到达时就发送了响应(使用res.send)。接下来的块会导致另一个res.send,从而产生ERR_HTTP_HEADERS_SENT错误。

英文:

The "error" that is returned is not actually an error, but is part of what ffmpeg logs. The manpage says:
> By default the program logs to stderr

During one execution of ffmpeg, there can be multiple chunks of data sent either to stdout or stderr, but your code sends a response (with res.send) already when the first chunk arrives. The next chunk then produces another res.send, will leads to an ERR_HTTP_HEADERS_SENT error.

huangapple
  • 本文由 发表于 2023年1月10日 23:36:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75072303.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定