FFMPEG挂起整个nodejs进程

我想要做的是使用ffmpeg制作video的缩略图。 video数据通过HTTP请求接收,然后通过pipe道传输到ffmpeg。 问题是,一旦ffmpegsubprocess退出,我根本无法发送回应。

这里是代码:

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'); 

什么都不做,客户从来没有收到回应。

经过两天的debugging和谷歌search似乎我发现了这个问题。 在node.js中有两个相关的open bugs负责:

我将尝试用“pipe道”方法来描述我认为的问题:

请求stream无法在ffmpeg.stdin(可能是bug#777)上调用结束,这会导致pipe道错误,但由于错误#782,node.js不处理错误,同时请求stream保持暂停状态 – 此块任何被发送的回应。

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

这里是固定的代码示例:

 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发布一旦他们做了一些关于这些错误的问题

我会尝试这样的事情。

 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"); 

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