在node.js中生成子进程时出错

发布于 2024-10-20 16:40:48 字数 2250 浏览 1 评论 0原文

我正在尝试启动并运行一些 ffmpeg 转换器服务,到目前为止取得了相当好的进展。但当涉及到生成实际的 ffmpeg 转换过程时,我遇到了困难。

// options.ffmpegopts is an array containing format-specific parameters
var args = [ '-y', '"' + options.targetfile + '"' ];
args = options.ffmpegopts.concat(args);

var ffmpegProc = spawn('ffmpeg ', args);
ffmpegProc.stderr.on('data', function(data) {
  console.log('stderr: ' + data);
});

执行此代码时,我得到以下控制台输出:

stderr: execvp(): No such file or directory

我已经检查了不同的节点版本(0.4.0、0.4.2 和 0.5.0-pre),但没有任何效果。

另一个非常奇怪的行为是我必须调用包含空格的spawn('ffmpeg'而不仅仅是'ffmpeg')。如果我省略这个空格,我会得到一个不同的错误(stderr:“/path/to/my/movie.mpeg”:没有这样的文件或目录)。当直接从 shell 调用 ffmpeg 时,发送到 child_process.spawn() 的命令执行没有任何问题。

对此有任何提示吗?我已经检查了实现相同目标的其他项目(例如 node-imagemagickffmpeg-node,但启示并没有击中我......

更新:strace() 输出

使用 < 运行我的应用程序时code>strace -fF -o strace.log node server.js,我可以 grep 以下进程生成调用:

execve("/usr/local/sbin/ffmpeg", ["ffmpeg", "-i", "\"/data/media_dev/test/ORG_mymovi"..., "-sameq", "-ab", "128k", "-ar", "44100", "-b", "512k", "-r", "25", "-s", "320x240", "-f", "flv", ...], [/* 20 vars */]) = -1 ENOENT (No such file or directory)
execve("/usr/local/bin/ffmpeg", ["ffmpeg", "-i", "\"/data/media_dev/test/ORG_mymovi"..., "-sameq", "-ab", "128k", "-ar", "44100", "-b", "512k", "-r", "25", "-s", "320x240", "-f", "flv", ...], [/* 20 vars */]) = -1 ENOENT (No such file or directory)
execve("/usr/sbin/ffmpeg", ["ffmpeg", "-i", "\"/data/media_dev/test/ORG_mymovi"..., "-sameq", "-ab", "128k", "-ar", "44100", "-b", "512k", "-r", "25", "-s", "320x240", "-f", "flv", ...], [/* 20 vars */]) = -1 ENOENT (No such file or directory)
execve("/usr/bin/ffmpeg", ["ffmpeg", "-i", "\"/data/media_dev/test/ORG_mymovi"..., "-sameq", "-ab", "128k", "-ar", "44100", "-b", "512k", "-r", "25", "-s", "320x240", "-f", "flv", ...], [/* 20 vars */]) = 0

在看到路径上奇怪的转义双引号后,我尝试在不带引号的情况下调用 ffmpeg...但问题仍然存在,我需要能够使用路径中的空格。

更新

:解决方案

可以使用空格,一个简单的 inputfile.replace(' ', '\ ' ) 就足够了。

I'm trying to get a little ffmpeg converter-service up and running, made pretty good progress so far. But when it comes to spawning the actual ffmpeg process for conversion, i'm hitting a brick wall.

// options.ffmpegopts is an array containing format-specific parameters
var args = [ '-y', '"' + options.targetfile + '"' ];
args = options.ffmpegopts.concat(args);

var ffmpegProc = spawn('ffmpeg ', args);
ffmpegProc.stderr.on('data', function(data) {
  console.log('stderr: ' + data);
});

When executing this code, i get the following console output:

stderr: execvp(): No such file or directory

I already checked different node versions (0.4.0, 0.4.2 and 0.5.0-pre) without any effect.

Another really strange behavior is the fact that i have to call spawn including a space ('ffmpeg ' instead of just 'ffmpeg'). If i omit this space, i get a different error (stderr: "/path/to/my/movie.mpeg": no such file or directory). When calling ffmpeg directly from the shell, the command sent to child_process.spawn() executes without any problems.

Any hints on that one? I already checked other projects who achieve the same (like node-imagemagick or ffmpeg-node, but the enlightment didn't hit me...

Update: strace() output

When running my application using strace -fF -o strace.log node server.js, i can grep the following process spawning calls:

execve("/usr/local/sbin/ffmpeg", ["ffmpeg", "-i", "\"/data/media_dev/test/ORG_mymovi"..., "-sameq", "-ab", "128k", "-ar", "44100", "-b", "512k", "-r", "25", "-s", "320x240", "-f", "flv", ...], [/* 20 vars */]) = -1 ENOENT (No such file or directory)
execve("/usr/local/bin/ffmpeg", ["ffmpeg", "-i", "\"/data/media_dev/test/ORG_mymovi"..., "-sameq", "-ab", "128k", "-ar", "44100", "-b", "512k", "-r", "25", "-s", "320x240", "-f", "flv", ...], [/* 20 vars */]) = -1 ENOENT (No such file or directory)
execve("/usr/sbin/ffmpeg", ["ffmpeg", "-i", "\"/data/media_dev/test/ORG_mymovi"..., "-sameq", "-ab", "128k", "-ar", "44100", "-b", "512k", "-r", "25", "-s", "320x240", "-f", "flv", ...], [/* 20 vars */]) = -1 ENOENT (No such file or directory)
execve("/usr/bin/ffmpeg", ["ffmpeg", "-i", "\"/data/media_dev/test/ORG_mymovi"..., "-sameq", "-ab", "128k", "-ar", "44100", "-b", "512k", "-r", "25", "-s", "320x240", "-f", "flv", ...], [/* 20 vars */]) = 0

After seeing that strangely escaped double quotes on the path, i tried to call ffmpeg without the quotes...worked like a charm. But the problem remains, i need to be able to work with spaces in my paths.

Any suggestions?

Update: Solution

Got it working with spaces, a simple inputfile.replace(' ', '\ ') was enough.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春夜浅 2024-10-27 16:40:48

我敢打赌,“ffmpeg”末尾的空格是当前问题的原因。一个快速的小 C 程序将显示:

   #include <unistd.h>

   int main(int argc, char *argv[]) {
       execvp(argv[1], &argv[1]);
       perror(argv[0]);
   }

给出以下输出:

$ ./execvp "ffmpeg"
FFmpeg version 0.6-4:0.6-2ubuntu6, Copyright (c) 2000-2010 the FFmpeg developers
...
$ ./execvp "ffmpeg "
./execvp: No such file or directory
$

我建议再次删除空格,然后在 strace(1) -fF 下重新运行。查找实际执行的命令,查看/path/to/my/movie.mpeg的错误消息是来自ffmpeg还是来自node.js

I'd wager money that the space at the end of "ffmpeg " is the cause of the current problem. A quick little C program will show that:

   #include <unistd.h>

   int main(int argc, char *argv[]) {
       execvp(argv[1], &argv[1]);
       perror(argv[0]);
   }

gives the following output:

$ ./execvp "ffmpeg"
FFmpeg version 0.6-4:0.6-2ubuntu6, Copyright (c) 2000-2010 the FFmpeg developers
...
$ ./execvp "ffmpeg "
./execvp: No such file or directory
$

I suggest removing the space again, and re-run under strace(1) -fF. Look for the command that is actually executed, and look to see if the error message about /path/to/my/movie.mpeg is coming from ffmpeg or from node.js.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文