如何合并视频上传块 Node.js

How to combine video upload chunks Node.js

我正在尝试通过使用 busboy 分块将大型 (8.3GB) 视频上传到我的 Node.js (Express) 服务器。我如何接收每个块(服务员正在做这部分)并将其拼凑成一个完整的视频?

我一直在研究可读和可写的流,但我从来没有得到完整的视频。我一直在覆盖它的一部分,导致大约 1 GB。

这是我的代码:

req.busboy.on('file', (fieldname, file, filename) => {
    logger.info(`Upload of '${filename}' started`);

    const video = fs.createReadStream(path.join(`${process.cwd()}/uploads`, filename));
    const fstream = fs.createWriteStream(path.join(`${process.cwd()}/uploads`, filename));

    if (video) {
        video.pipe(fstream);
    }

    file.pipe(fstream);

    fstream.on('close', () => {
        logger.info(`Upload of '${filename}' finished`);
        res.status(200).send(`Upload of '${filename}' finished`);
    }); 
});

我觉得 multer 很好用,你试过 multer 了吗?

使用流

multer 允许您轻松处理文件上传作为快速路由的一部分。这对于不会留下大量内存占用的小文件非常有用。

将大文件加载到内存中的问题是您实际上可能 运行 内存不足并导致应用程序崩溃。

使用multipart/form-data请求。这可以通过在您的请求选项中将 readStream 分配给该字段来处理

流对于优化性能非常有价值。

试试这个代码示例,我认为它对你有用。

busboy.on("file", function(fieldName, file, filename, encoding, mimetype){
    const writeStream = fs.createWriteStream(writePath);
    file.pipe(writeStream);

    file.on("data", data => {
        totalSize += data.length;
        cb(totalSize);
    });

    file.on("end", () => {
        console.log("File "+ fieldName +" finished");
    });
});

你也可以参考这个link来解决这个问题

https://github.com/mscdex/busboy/issues/143

12 个多小时后,我使用 this article that was given to me 中的片段弄明白了。我想出了这个代码:

//busboy is middleware on my index.js
const fs = require('fs-extra');
const streamToBuffer = require('fast-stream-to-buffer');

//API function called first
uploadVideoChunks(req, res) {
    req.pipe(req.busboy);

    req.busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
        const fileNameBase = filename.replace(/\.[^/.]+$/, '');

        //save all the chunks to a temp folder with .tmp extensions
        streamToBuffer(file, function (error, buffer) {
            const chunkDir = `${process.cwd()}/uploads/${fileNameBase}`;
            fs.outputFileSync(path.join(chunkDir, `${Date.now()}-${fileNameBase}.tmp`), buffer);
        });
    });

    req.busboy.on('finish', () => {
        res.status(200).send(`Finshed uploading chunk`);
    });
}

//API function called once all chunks are uploaded
saveToFile(req, res) {
    const { filename, profileId, movieId } = req.body;

    const uploadDir = `${process.cwd()}/uploads`;
    const fileNameBase = filename.replace(/\.[^/.]+$/, '');
    const chunkDir = `${uploadDir}/${fileNameBase}`;
    let outputFile = fs.createWriteStream(path.join(uploadDir, filename));

    fs.readdir(chunkDir, function(error, filenames) {
       if (error) {
           throw new Error('Cannot get upload chunks!');
       }

       //loop through the temp dir and write to the stream to create a new file
       filenames.forEach(function(tempName) {
           const data = fs.readFileSync(`${chunkDir}/${tempName}`);
                outputFile.write(data);
                //delete the chunk we just handled
                fs.removeSync(`${chunkDir}/${tempName}`);
           });

            outputFile.end();
        });

        outputFile.on('finish', async function () {
            //delete the temp folder once the file is written
            fs.removeSync(chunkDir);
        }
    });
}