node.js - 如何组合视频上传 block Node.js

标签 node.js express busboy

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

我一直在研究可读和可写的流,但我没有得到整个视频。我不断覆盖其中的一部分,导致大约 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`);
    }); 
});

最佳答案

经过 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);
        }
    });
}

关于node.js - 如何组合视频上传 block Node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54541579/

相关文章:

node.js - 当 xml 架构验证失败时,Nodejs libxmljs 使 docker 容器崩溃

node.js - MongoDB 连接在 connection.close() 之后并不总是关闭

node.js - 如何处理 Passport 身份验证响应并将其显示给用户

javascript - Nodejs,连接巴士男孩。上传几个文件

javascript - busboy-connect 在保存文件结束前触发(node.js,express)

node.js - Google App Engine - 间歇性 502/连接由同行重置

node.js - gruntjs 路径过滤器 : exclude files start with _

javascript - 语法错误: Unexpected token o in JSON at position 1 over ajax request

javascript - Route.post() 需要一个回调函数,但得到一个 [object String]

node.js - 如何接收数据并将其保存为二进制?