FFMPEG挂起整个nodejs进程

rco*_*ode 4 ffmpeg response pipe node.js

我想要做的是使用ffmpeg制作视频的缩略图.视频数据在HTTP请求中接收,然后通过管道传输到ffmpeg.问题是,一旦ffmpeg子进程退出,我就无法发回响应.

这是代码:

var http = require('http'),
sys = require('sys'),
child = require('child_process')
http.createServer(function (req, res) {
    im = child.spawn('ffmpeg',['-i','-','-vcodec','mjpeg','-ss','00:00:03','-vframes','1','-s','100x80','./thumb/thumbnail.jpg']);
    im.on('exit', function (code, signal) {
        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.end('{"success":true}\n');
     });
    req.connection.pipe(im.stdin);
}).listen(5678, "127.0.0.1");
Run Code Online (Sandbox Code Playgroud)

问题是调用:

res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('{"success":true}\n');
Run Code Online (Sandbox Code Playgroud)

什么都不做,客户端永远不会收到回复.

rco*_*ode 5

经过两天的调试和谷歌搜索似乎我发现了问题.node.js中有两个相关的开放错误:

我将尝试用'pipe'方法描述我认为的问题:

请求流无法在ffmpeg.stdin上调用end(可能是bug#777),这会导致管道错误,但是由于bug#782,node.js没有处理错误,同时请求流仍然暂停 - 这个块发送的任何回复.

黑客/解决方法是在ffmpeg退出后恢复请求流.

这是固定代码示例:

var http = require('http'),
sys = require('sys'),
child = require('child_process')
http.createServer(function (req, res) {
im = child.spawn('ffmpeg',['-i','-','-vcodec','mjpeg','-ss','00:00:03','-vframes','1','-s','100x80','./thumb/thumbnail.jpg']);
    im.on('exit', function (code, signal) {
        req.resume();
        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.end('{"success":true}\n');
     });
  req.connection.pipe(im.stdin);
}).listen(5678, "127.0.0.1");
Run Code Online (Sandbox Code Playgroud)

请记住,这是一个黑客/解决方法,一旦他们对这些错误做了些什么,可能会导致未来node.js版本出现问题