在node.js中生成子进程时出错
我正在尝试启动并运行一些 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-imagemagick 或 ffmpeg-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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我敢打赌,“ffmpeg”末尾的空格是当前问题的原因。一个快速的小 C 程序将显示:
给出以下输出:
我建议再次删除空格,然后在
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:gives the following output:
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 fromffmpeg
or fromnode.js
.