Metor method using npm module


#1

Hi,

I want to use a NPM module to encode file with fluent-ffmpeg

I am able to run the encoding process on the server sid and get console.log information on the server

I want to send theses informations to the client but my callback doesn’t seem to go to the client as i have an “undefined” error

on the client side, my .js file initiate the call like this :
Template.inputFile.events({
‘click .btn’: function (evt) {
evt.preventDefault();
var input = $(’.input_file’).val(); // récupération avec Jquery de la valeur de l’input
var inputAr = $(’.aspect_ratio’).val(); // récupération avec Jquery de la valeur de l’input
var inputCodec = $(’.codec’).val(); // récupération avec Jquery de la valeur de l’input
inputok = input.replace(“C:\fakepath\”, “C:\bin\”);
files.insert({name: inputok});
console.log(inputok);
console.log(inputAr);
console.log(inputCodec);

      Meteor.call('sendEncode',inputok, inputAr, inputCodec, function(error,response){
        if(error){
          console.log('ERROR :', error);
        }else{
          console.log('Response :', response);
        }
      })

   }

});

and on my server side, my .js file is :

Meteor.methods({
‘sendEncode’: function(input1, input2, input3){ // récupération du paramètre passé par le meteor.call
var FileToEncode = ffmpeg(input1)
// set video bitrate
.videoBitrate(1024)
// set target codec
.videoCodec(input3)
// set aspect ratio
.aspect(input2)
// set size in percent
.size(‘50%’)
// set fps
.fps(24)
// set audio bitrate
.audioBitrate(‘128k’)
// set audio codec
.audioCodec(‘libmp3lame’)
// set number of audio channels
.audioChannels(2)
// set custom option
.addOption(’-vtag’, ‘DIVX’)
// 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);
})
.on(‘progress’, function(progress) {
console.log('Processing: ’ + progress.percent + ‘% done’);
})
// save to file
.save(‘C:\apps\walking3.avi’);
}

how can i retrieve the on ‘end’, on, ‘error’ and on ‘pogress’ informations on the client side ?

thanks

best regards

Fabien


#2

Please wrap your code in triple backticks, like this:

```
paste
your
code
here
```

#3

#4

Yes sorry, as i can’t edit my original post, i post it again

Hi,

I want to use a NPM module to encode file with fluent-ffmpeg

I am able to run the encoding process on the server sid and get console.log information on the server

I want to send theses informations to the client but my callback doesn’t seem to go to the client as i have an “undefined” error

on the client side, my .js file initiate the call like this :

Template.inputFile.events({
'click .btn': function (evt) {
evt.preventDefault();
var input = $('.input_file').val(); // récupération avec Jquery de la valeur de l'input
var inputAr = $('.aspect_ratio').val(); // récupération avec Jquery de la valeur de l'input
var inputCodec = $('.codec').val(); // récupération avec Jquery de la valeur de l'input
inputok = input.replace("C:\fakepath\", "C:\bin\");
files.insert({name: inputok});
console.log(inputok);
console.log(inputAr);
console.log(inputCodec);

      Meteor.call('sendEncode',inputok, inputAr, inputCodec, function(error,response){
        if(error){
          console.log('ERROR :', error);
        }else{
          console.log('Response :', response);
        }
      })

   }
});

and on my server side, my .js file is :

Meteor.methods({
'sendEncode': function(input1, input2, input3){ // récupération du paramètre passé par le meteor.call
var FileToEncode = ffmpeg(input1)
// set video bitrate
.videoBitrate(1024)
// set target codec
.videoCodec(input3)
// set aspect ratio
.aspect(input2)
// set size in percent
.size('50%')
// set fps
.fps(24)
// set audio bitrate
.audioBitrate('128k')
// set audio codec
.audioCodec('libmp3lame')
// set number of audio channels
.audioChannels(2)
// set custom option
.addOption('-vtag', 'DIVX')
// 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);
})
.on('progress', function(progress) {
console.log('Processing: ' + progress.percent + '% done');
})
// save to file
.save('C:\apps\walking3.avi');
}

how can i retrieve the on ‘end’, on, ‘error’ and on ‘pogress’ informations on the client side ?

thanks

best regards

Fabien


#5

You need to return an EJSONable object of some sort. However, you will need to do this using Futures or promises, since otherwise the method will have completed (returning undefined) before the callback runs.

You need to do a throw new Meteor.Error, again within the context of a Future or a promise: http://docs.meteor.com/#/full/meteor_error

This is trickier (I think). What you really need here is a publication returning progress “as it happens”. However, I’m not sure if this can be done within the execution context of a method. Outside of the method, you will not have access to the ffmpeg object.

An interesting problem.


#6

After giving this some more though I think by far the easiest solution is to not use a method, but a “roll-your-own” publication, which returns “collection documents” appropriate to the ffmpeg status. That means you can use standard Meteor functionality, including the ability to follow the conversion progress live. My tests indicate that multiple async callbacks seem to be handled correctly without recourse to Futures or promises. :slight_smile: