Monday, 29 May 2017

NodeJs: Parsing POST request file upload as stream to ffmpeg

I'm trying to upload a file by sending a POST request to my NodeJs-server where the server will upon receiving the request (but not necessarily whole file) create a readStream on the incoming file and pass it to fluent-ffmpeg for video-compression and then saving it on the server. The idea is to pass the stream to ffmpeg without waiting for the whole file to be uploaded instead of waiting for the upload to complete and then pass the file since it would take more time and require to store the file temporarily on the server before the compression.

With the current code ffmpeg gives the error pipe:0: Invalid data found when processing input which I translate to some error with the stream creation. The code is as follows

module.exports = function(req, res) {
  let formidable = require('formidable');
  let form = new formidable.IncomingForm();
  form.parse(req, (err, fields, files) => {
    let readStream = fs.createReadStream(files.uploadedfile.path);
    let ffmpeg = require('fluent-ffmpeg')
    let ffmpegCmd = ffmpeg(readStream)
    ...
  });
});

Am I making some wrong assumption here?



via Christian Abdelmassih

No comments:

Post a Comment