Search code examples
node.jsffmpegfluent-ffmpeg

fluent-ffmpeg get codec data without specifying output


I am using fluent-ffmpeg node module for getting codec data from a file. It works if I give an output but I was wondering if there is any option to run fluent-ffmpeg without giving to it an output. This is what I am doing:

readStream.end(new Buffer(file.buffer));
var process = new ffmpeg(readStream);

process.on('start', function() {
  console.log('Spawned ffmpeg');
}).on('codecData', function(data) {
  //get recording duration
  const duration = data.duration;
  console.log(duration)
}).save('temp.flac');

As you can see I am saving the file to temp.flac so I can get the seconds duration of that file.


Solution

  • If you don't want to save the ffmpeg process result to a file, one thing that comes to mind is to redirect the command output to /dev/null.

    In fact, as the owner of the fluent-ffmpeg repository said in one comment, there is no need to specify a real file name for the destination when using null format.

    So, for example, something like that will work:

    let process = new ffmpeg(readStream);
    
    process
      .addOption('-f', 'null')  // set format to null 
      .on('start', function() {
        console.log('Spawned ffmpeg');
      })
      .on('codecData', function(data) {
        //get recording duration
        let duration = data.duration;
        console.log(duration)
      })
      .output('nowhere')  // or '/dev/null' or something else
      .run()
    

    It remains a bit hacky, but we must set an output to avoid the "No output specified" error.