简体   繁体   English

在Node.js中的两个子进程之间进行管道传输?

[英]Pipe between two child processes in Node.js?

I'm trying to capture video using FFmpeg with Node.js, and send it to a browser via websockets for playing using the MediaSource API. 我正在尝试使用FFmpeg和Node.js捕获视频,并通过websocket将其发送到浏览器以使用MediaSource API进行播放。 What I have so far works in Firefox but doesn't decode properly in Chrome. 到目前为止,我在Firefox中可以正常使用,但在Chrome中无法正确解码。 Apparently, from reading this question I need to use the sample_muxer program to ensure each 'cluster' starts with a keyframe. 显然,从阅读此问题开始,我需要使用sample_muxer程序来确保每个“集群”都以关键帧开头。

Here's the code I'm using: 这是我正在使用的代码:

var ffmpeg = child_process.spawn("ffmpeg",[
    "-y",
    "-r", "30",

    "-f","dshow",           
    "-i","video=FFsource:audio=Stereo Mix (Realtek High Definition Audio)",

    "-vcodec", "libvpx",
    "-acodec", "libvorbis",

    "-threads", "0",

    "-b:v", "3300k",
    "-keyint_min", "150",
    "-g", "150",

    "-f", "webm",

    "-" // Output to STDOUT
]);

ffmpeg.stdout.on('data', function(data) {
    //socket.send(data); // Just sending the FFmpeg clusters works with Firefox's 
                         // implementation of the MediaSource API. No joy with Chrome.

    // - - - This is the part that doesn't work - - -
    var muxer = child_process.spawn("sample_muxer",[
        "-i", data, // This isn't correct...

        "-o", "-" // Output to STDOUT
    ]);

    muxer.stdout.on('data', function(muxdata) {
        socket.send(muxdata); // Send the cluster
    });
});

ffmpeg.stderr.on('data', function (data) {
    console.log("" + data); // Output to console
});

Obviously I'm not piping it correctly and I'm unsure how I would while also including the arguments. 显然,我没有正确地传递它,也不确定要如何包含这些参数。 Appreciate any help getting this working. 感谢任何帮助使此工作正常进行。 Thanks! 谢谢!

The sample_muxer program takes -i argument as the name of file. sample_muxer程序将-i参数用作文件名。 It cannot read video data as standard input. 它不能读取视频数据作为标准输入。 To view error, you should send error stream from sample_muxer to an error log file. 要查看错误,您应该将错误流从sample_muxer发送到错误日志文件。

var muxer = child_process.spawn("sample_muxer",[
    "-i", data, // This isn't correct...
    "-o", "-" // Output to STDOUT
]);

This code will result in error at https://code.google.com/p/webm/source/browse/sample_muxer.cpp?repo=libwebm#240 此代码将导致错误, 网址https://code.google.com/p/webm/source/browse/sample_muxer.cpp?repo=libwebm#240

You can try writing to a file from ffmpeg and then reading that file from sample_muxer. 您可以尝试从ffmpeg写入文件,然后从sample_muxer读取该文件。 Once that works, try with a FIFO file to pipe data from ffmpeg to sample_muxer. 一旦可行,请尝试使用FIFO文件将数据从ffmpeg传递到sample_muxer。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM