Ricky
Ricky

Reputation: 777

ffmpeg how do I get the duration onto the node.js?

Ok I know how to get the duration of a video in ffmpeg using this command

ffmpeg -i ./output/sample.mp4 2>&1 | grep Duration | cut -d ' ' -f 4 | sed s/,//

I run this command through a function in node but this outputs the duration onto the console, how do I get it onto node.js where I need it?

Upvotes: 7

Views: 15746

Answers (2)

Shashwat Gupta
Shashwat Gupta

Reputation: 5272

var ffmpeg = require('ffmpeg');

app.get('/getVideoDuration', (req, res) => {
  fs.unlink('public/output.mp4')
  try {
    new ffmpeg('public/input.mov', function (err, video) {
      if (!err) {
        console.log(video.metadata.duration.seconds, "video")
            res.send({
              'msg': "Success",
              'duration': video.metadata.duration.seconds
            })
          }

    });
  } catch (e) {
    console.log(e.code);
    console.log(e.msg);
  }

})

Upvotes: 1

Business Tomcat
Business Tomcat

Reputation: 1061

There is a way to read console output stream, but I would prefer to use fluent-ffmpeg: https://github.com/fluent-ffmpeg/node-fluent-ffmpeg

It is a npm module for nodejs.

From here you can call ffprobe (a tool shipped with ffmpeg and better than ffmpeg to get informations about a video), like this:

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

ffmpeg.ffprobe('./input.mp4', function(err, metadata) {
    //console.dir(metadata); // all metadata
    console.log(metadata.format.duration);
});

Upvotes: 21

Related Questions