我使用过 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) */