如何关闭从实时捕获源主动流式传输的Node.js FFMPEG子进程? [英] How do I close a Node.js FFMPEG child process that is actively streaming from a live capture source?

查看:365
本文介绍了如何关闭从实时捕获源主动流式传输的Node.js FFMPEG子进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Node.js的新手,并弄清楚了如何利用child.spawn启动FFMPEG实例,该实例用于捕获实时视频并将其通过rtmp发送到Adobe Media Server.

I'm new to Node.js and have figured out how to utilize child.spawn to launch an instance of FFMPEG that is being used to capture live video and send it over to Adobe Media Server via rtmp.

我所见过的每个示例中,FFMPEG与Node.js结合使用的示例都是有时间限制的,因此一旦FFMPEG到达要转换的文件末尾,子进程便会关闭.

Every example I've seen of FFMPEG being used in conjunction with Node.js has been with a time limited sample, so the child process closes once FFMPEG reaches the end of the file it is converting.

在这种情况下,没有文件结尾".

In this case, there is no "end of file".

如果我实例化:

    var ffmpeg = child.spawn('ffmpeg.exe', [args]);

它将创建实时供稿.

我尝试立即使用以下命令关闭子进程:

I have tried immediately shutting the child process down with a:

    setTimeout(function() {
        ffmpeg.stdin.resume();
        ffmpeg.stdin.write('insert command to echo q to close FFMPEG');
        ffmpeg.stdin.end();
    });

但是,这似乎不起作用.我继续在测试框上看到我的rtmp提要.

However, that does not seem to work. I continue to see my rtmp feed on my test box.

是否可以通过Node.js中的stdin向FFMPEG传递关闭命令?

Is there any way to pass FFMPEG a shut down command via stdin in Node.js?

提前谢谢!

里克

推荐答案

我的主要app.js使用child_process.fork()方法将以下代码作为模块加载:

The following code is loaded by my main app.js as a module using the child_process.fork() method:

    var spawn = require('child_process').spawn;

    var ffmpeg = spawn('C:\\Program Files (x86)\\ffmpeg\\bin\\ffmpeg.exe', ['-y', '-threads', '-0', '-re', '-rtbufsize', '204800000', '-probesize', '4096', '-vsync', '2', '-async', '30', '-f', 'dshow', '-s', '320x240', '-i', 'video=Integrated Webcam:audio=Microphone Array (IDT High Defi', '-c:a', 'libvo_aacenc', '-ab', '48000', '-ar', '22050', '-ac', '2', '-c:v', 'libx264', '-s', '400x300', '-g', '96', '-x264opts', 'bitrate=1200', '-preset', 'ultrafast', '-profile:v', 'baseline', '-pix_fmt', 'yuv420p', '-aspect', '4:3', '-f', 'flv', 'rtmp://server']);

    setTimeout(function() {
        ffmpeg.stderr.on('data', function() {
            ffmpeg.stdin.setEncoding('utf8');
            ffmpeg.stdin.write('q');
            process.exit();
        });
    }, 10000);

这远没有我做的复杂.主要的app.js是一个基本的HTML页面,该页面已投放并使用socket.io接收事件及其相应的数据.在这种情况下,"true"事件将加载module.js文件,该文件将启动FFMPEG的实时捕获会话,将其馈入RTMP服务器,并在10秒超时后正常关闭FFMPEG.

It was far less complicated than I was making it. The main app.js is a basic HTML page that is served up and uses socket.io to receive an event and its corresponding data. In this case, a 'true' event loads the module.js file which launches a live capture session of FFMPEG, feeds it into a RTMP server, and gracefully shuts down FFMPEG on a timeout of 10 seconds.

我的下一个任务是通过Web界面触发的事件将其关闭,这与当前的超时测试方法相反.

My next task is to shut it down via an event triggered from a web interface as opposed to the current testing method of a timeout.

查看Windows中的任务管理器,FFMPEG进程与辅助节点进程一样关闭.

Looking at the task manager in Windows, the FFMPEG process closes as does the secondary node process.

其原因是我发现的所有node-ffmpeg模块都不支持通过捕获输入进行实时流式传输.它们似乎主要用于对现有内容进行代码转换.理想的最终结果是可以启动和停止FFMPEG的基于Web的界面.由于无法保存标准mp4文件,我们的用例将替换Adobe Flash Media Live Encoder作为Adobe Media Server的源.

The reason for this is that none of the node-ffmpeg modules that I found supported live streaming via capture input. They appear to be primarily for transcoding existing content. The final outcome of this will ideally be a web based interface that can start and stop FFMPEG. Our use case will be replacing Adobe Flash Media Live Encoder as the source for our Adobe Media Server due to its inability to save standard mp4 files.

这篇关于如何关闭从实时捕获源主动流式传输的Node.js FFMPEG子进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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