FFMPEG挂起整个nodejs进程 [英] FFMPEG hangs the whole nodejs process

查看:599
本文介绍了FFMPEG挂起整个nodejs进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What I am trying to do is to make a thumbnail of a video using ffmpeg. The video data is received in a HTTP request and then piped to ffmpeg. The problem is that once the ffmpeg child process exits I simply can't send the response back.

这是代码:

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

什么也不做,客户端从未收到回应。

does nothing, the client never receives the response.

推荐答案

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

After two days of debugging and googling It seems like I have found the problem. There are two related open bugs in node.js responsible:

  • https://github.com/joyent/node/issues/777
  • https://github.com/joyent/node/issues/782

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

I will try to describe what I think the problem is with the 'pipe' method:

请求流无法在ffmpeg.stdin(可能是错误#777)上调用结尾,这会导致管道错误,但是node.js不会因为错误#782而处理错误,同时请求流保持暂停 - 这将阻止任何响应发送。

The request stream fails to invoke end on ffmpeg.stdin (probably bug #777), this causes a broken pipe error, but node.js doesn't handle the error because of bug #782, meanwhile the request stream remains paused - this blocks any response from being sent.

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

The hack/workaround is to resume the request stream once ffmpeg exits.

这是固定代码示例:

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

这篇关于FFMPEG挂起整个nodejs进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆