Search code examples
node.jsffmpegtranscode

FFmpeg with node.js. Transcode a file to another format


Have a bit of a problem with this, I have an .avi file in which I would like to transcode into a .flv file using FFmpeg, here is what I have so far:

var ffmpeg = require('fluent-ffmpeg');

//make sure you set the correct path to your video file
var proc = new ffmpeg({ source: 'C:/Users/Jay/Documents/movie/drop.avi', nolog: true })

//Set the path to where FFmpeg is installed
.setFfmpegPath("C:\Users\Jay\Documents\FFMPEG")

//set the size
.withSize('50%')

// set fps
.withFps(24)

// set output format to force
.toFormat('flv')

// setup event handlers
.on('end', function() {
    console.log('file has been converted successfully');
})
.on('error', function(err) {
    console.log('an error happened: ' + err.message);
})
// save to file <-- the new file I want -->
.saveToFile('C:/Users/Jay/Documents/movie/drop.flv');

It seems straightforward enough and I can do it through the FFmpeg command line, but I am trying to get it working within a node.js app, here is the error it is returning:

C:\Users\Jay\workspace\FFMPEGtest\test.js:17
.withSize('50%')
 ^
TypeError: Cannot call method 'withSize' of undefined
    at Object.<anonymous> (C:\Users\Jay\workspace\FFMPEGtest\test.js:17:2)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:906:3

It throws the same error for each built in FFmpeg function (.toFormat, .withFPS etc)

If anyone has a solution to this, I'd greatly appreciate it


Solution

  • setFfmpegPath() doesn't return an instance of this, as seen in the source here. Meaning you can't chain the method.

    Change it to

    var ffmpeg = require('fluent-ffmpeg');
    
    //make sure you set the correct path to your video file
    var proc = new ffmpeg({ source: 'C:/Users/Jay/Documents/movie/drop.avi', nolog: true })
    
    //Set the path to where FFmpeg is installed
    proc.setFfmpegPath("C:\\Users\\Jay\\Documents\\FFMPEG")
    
    proc
    //set the size
    .withSize('50%')
    
    // set fps
    .withFps(24)
    
    // set output format to force
    .toFormat('flv')
    
    // setup event handlers
    .on('end', function() {
        console.log('file has been converted successfully');
    })
    .on('error', function(err) {
        console.log('an error happened: ' + err.message);
    })
    // save to file <-- the new file I want -->
    .saveToFile('C:/Users/Jay/Documents/movie/drop.flv');