node.js - 如何从多个文件创建可读文件流

标签 node.js ffmpeg fs

相关问题: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/

相关文章:

c++ - 什么是损坏的 MPEG 流?

nginx - 流式传输实时视频和仅将音频中继到 icecast2 服务器

javascript - 如何用js将数据写入xls表

javascript - 发出请求时 GCF 超时

javascript - 不理解使用函数和参数解析查询字符串

javascript - 使用数据库查找在 Node 中导出大文件 - 避免多次数据库调用?

node.js - 在 Heroku 上运行时找不到模块 './mergeConfig'

video - 如何使用 ffmpeg 获取 mp4 容器的任何流的持续时间?

node.js - 如何从内存中的字节创建readStream?

node.js - 删除nodejs脏db文件