简体   繁体   中英

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.

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.

When no stream argument is present, the pipe() method returns a PassThrough stream, which you can pipe to somewhere else (or just listen to events on).

var command = ffmpeg('/path/to/file.avi')
  .videoCodec('libx264')
  .audioCodec('libmp3lame')
  .size('320x240')
  .on('error', function(err) {
    console.log('An error occurred: ' + err.message);
  })
  .on('end', function() {
    console.log('Processing finished !');
  });

var ffstream = command.pipe();
ffstream.on('data', function(chunk) {
  console.log('ffmpeg just wrote ' + chunk.length + ' bytes');
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM