无法使用ffmpeg将视频与express合并:无法使用“ in”运算符搜索未定义的“可读”

时间:2019-03-23 21:15:04

标签: node.js express fluent-ffmpeg

目标是将视频文件保存到 api / videos / result ,然后将该文件夹中的所有视频合并为一个视频。

npm模块 fluent-ffmpeg 引发错误:

TypeError: Cannot use 'in' operator to search for 'readable' in undefined
    at FfmpegCommand.proto.mergeAdd.proto.addInput.proto.input (C:\Users\vmadmin\Desktop\M152-Frontend\node_modules\fluent-ffmpeg\lib\options\inputs.js:33:24)
    at C:\Users\vmadmin\Desktop\M152-Frontend\server.js:36:35
    at Array.forEach (<anonymous>)
    at C:\Users\vmadmin\Desktop\M152-Frontend\server.js:30:12
    at Layer.handle [as handle_request] (C:\Users\vmadmin\Desktop\M152-Frontend\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\vmadmin\Desktop\M152-Frontend\node_modules\express\lib\router\route.js:137:13)
    at Array.<anonymous> (C:\Users\vmadmin\Desktop\M152-Frontend\node_modules\multer\lib\make-middleware.js:53:37)
    at listener (C:\Users\vmadmin\Desktop\M152-Frontend\node_modules\on-finished\index.js:169:15)
    at onFinish (C:\Users\vmadmin\Desktop\M152-Frontend\node_modules\on-finished\index.js:100:5)
    at callback (C:\Users\vmadmin\Desktop\M152-Frontend\node_modules\ee-first\index.js:55:10)

我不确定该如何处理。我的代码中既没有“内”也没有“可读”的地方,因此没有什么可尝试的。

var mergedVideo = ffmpeg();
var upload = multer({storage: storage});
var single = upload.single('MyVideo');
var fs = require('fs');

});
// POST Request. Video is being uploaded here
app.post('/api/videos', single, function (req, res) {
    // Declares variable, that saves an array from all elements in result folder
    var source = fs.readdirSync('./api/videos/result/');
    // Loop, which iterates through each element in result folder
    source.forEach(function (element) {
        // Merge 'api', 'videos' and 'result' to one path
        var pathJoin = path.join('api', 'videos', 'result');
        // Save complete path with file in one variable
        var finalPath = console.log(__dirname + '\\' + pathJoin + '\\' + element);
        // Adds each file into the input for merging
        mergedVideo = mergedVideo.addInput(finalPath);
    });
    // Merge files and save them as merged.mp4
    mergedVideo.mergeToFile('./api/videos/merged.mp4', '/api/videos/tempDir/')
        .on('error', function (err) {
        console.log('Error ' + err.message);
    })
        .on('end', function () {
        console.log('Finished!');
    });
    res.send(console.log('Hallo!'));
});

期望 api / videos 文件夹中名为 merged.mp4 的文件。

2 个答案:

答案 0 :(得分:1)

我在代码中发现错误。这里的问题是,

var finalPath = console.log(__dirname + '\\' + pathJoin + '\\' + element);

返回无效。 (因为这只是一个控制台输出)。其实应该是

var finalPath = __dirname + '\\' + pathJoin + '\\' + element;

答案 1 :(得分:0)

尝试如下所述

var fs = require('fs');
var source = fs.readdirSync('./api/videos/result/');

我希望这对您有帮助。