【问题标题】:Nodejs stream chainingNodejs流链接
【发布时间】:2019-01-01 18:25:51
【问题描述】:
你好我想链接一些fileReadStreams,我目前的使用是这样的;
- 产生一个子进程
- 创建文件ReadStream
- 通过管道(child.stdin)发送文件内容
- 循环回到第 2 步
我正在尝试使用管道将一些 mp3 文件发送到 ffmpeg,以便它可以输出无限的基于 hls&mpeg-dash 的流。
PS:我尝试了 writableStream 的 finished 事件,但如果完成会触发 child.stdin 被 writableStream 关闭。如果我将标志传递给不关闭,我永远不会完成事件。
【问题讨论】:
标签:
node.js
stream
nodejs-stream
【解决方案1】:
我使用过 PassThrough 流,结果并不完美,但在我的情况下还可以。如果有人需要这样的东西,我就是这样处理的;
const {PassThrough} = require('stream');
const pt = new PassThrough();
/* spawn child process */
/* PS: I do not know why direct pipe to ffmpeg does not work in my case throws pipe: Permission denied */
const child = spawn(
'cat',
['|', 'ffmpeg', ...],
{
stdio: ['pipe', 'inherit', 'inherit'],
shell: true
});
pt.pipe(child.stdin);
/* pass streams to pt stream with end false parameter */
stream1.pipe(pt, { end: false }); /* This also prevents stream1 finished event, but otherwise it would close the pt stream. */
stream2.pipe(pt, { end: false }); /* Or you can handle this part programmatically(loops, generators etc) */