相关问题:Add specific image files to ffmpeg video with fluent-ffmpeg
具有驻留在网络共享上的文件名数组。想将它们变成带有 nodejs 服务器上的进程的延时视频。我打算使用 ffmpeg 来做到这一点。 FFMPEG 不允许文件列表作为输入(据我所知)。但是,它们确实允许“可读文件流”。
是否有从文件名数组创建可读文件流的机制?
他们在 fluent-ffmpeg 示例中使用的命令是:var command = ffmpeg(fs.createReadStream('/path/to/file.avi'));
...但显然,这只是一个文件。
最佳答案
我认为这是一个有趣的问题,所以我创建了 multiSourceStream
接受一组文件的类,它将一个接一个地流式传输它们。我已经在简单的文本文件上对此进行了测试,包括一些非常大的文件,它似乎可以正确地进行流控制,但可能需要更详细的测试。
const {Readable} = require('stream');
const fs = require('fs');
class multiSourceStream extends Readable {
constructor(files, options) {
super(options);
this.files = files.slice();
this.readyToPush = false;
this.nextFile();
}
nextFile() {
if (this.files && this.files.length) {
const rs = this.rs = fs.createReadStream(this.files.shift());
// don't start flowing data until _read() is called
if (!this.readyToPush) {
rs.pause();
}
rs.on('data', chunk => {
if (!this.push(chunk)) {
rs.pause();
this.readyToPush = false;
}
}).on('end', () => {
this.nextFile();
}).on('error', err => {
this.destroy(err);
});
} else {
this.rs = null;
// all done
this.push(null);
}
}
_read() {
this.readyToPush = true;
if (this.rs) {
this.rs.resume();
} else {
// all done
this.push(null);
}
}
}
// example use
const ms = new multiSourceStream(['temp.txt', 'file1.txt', 'file2.txt']);
ms.pipe(fs.createWriteStream('myoutput.txt'));
关于node.js - 如何从多个文件创建可读文件流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63801606/