NodeJS + FFMPEG -sending response when FFMPEG is c

2019-06-10 09:25发布

问题:

I have a working NodeJS+FFMPEG application. I am able to upload video files and have them converted on the server. I am using this NodeJS FFMPEG library
https://github.com/fluent-ffmpeg/node-fluent-ffmpeg

I get a message on the server when the job is complete, but how do I notify the client?? In this case a simple AIR application. Right now I can only 'hear' the initial response after a successful upload.

The initial video file was uploaded via a http POST request.

My primary node application without the dependencies is as follows

var ffmpeg =  require('./lib/fluent-ffmpeg');

var express = require('express'),
    multer  = require('multer');
var app = express();

//auto save file to uploads folder
app.use(multer({ dest: './uploads/'}))

var temp;

app.post('/', function (req, res) {
    console.log(req.body); //contains the variables
    console.log("req.files ="+ req.files); //contains the file references

console.log("req.files.Filedata.path ="+ req.files.Filedata.path ); 

temp=req.files.Filedata.path;
// make sure you set the correct path to your video file
var proc = ffmpeg('./'+temp)
  // set video bitrate
  .videoBitrate(1024)
  // set audio codec
  .audioCodec('libmp3lame')    
  // set output format to force
  .format('avi')


  // setup event handlers
  .on('end', function() {
   console.log('file has been converted succesfully');    
  })
  .on('error', function(err) {
    console.log('an error happened: ' + err.message);
  })


  // save to file
  .save('./converted/converted.avi');
    res.send('Thank you for uploading!');

});

app.listen(8080);

回答1:

There are two approaches you can use. The first is to poll the server from the client with AJAX GET requests every x seconds. The second approach is to use WebSockets and notify the client by sending a message to them directly.