FFMPEG挂起整个nodejs进程

时间:2011-06-26 11:12:36

标签: node.js ffmpeg response pipe

我要做的是使用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");

问题是呼叫:

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

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

2 个答案:

答案 0 :(得分: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");

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

答案 1 :(得分:2)

我会尝试这样的事情。

var http = require('http'):
var sys = require('sys'):
var child = require('child_process'):

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    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.end('{"success":true}\n');
    });
    req.connection.pipe(im.stdin);
}).listen(5678, "127.0.0.1");

您正在尝试在发送标头之前将数据传输到套接字。