hmnayem
7/31/2019 - 9:01 AM

Read the duration of a mp4 file nodejs

Read the duration of a mp4 file nodejs

var buff = new Buffer(100);
fs.open(file, 'r', function(err, fd) {
  fs.read(fd, buff, 0, 100, 0, function(err, bytesRead, buffer) {
    var start = buffer.indexOf(new Buffer('mvhd')) + 17;
    var timeScale = buffer.readUInt32BE(start, 4);
    var duration = buffer.readUInt32BE(start + 4, 4);
    var movieLength = Math.floor(duration/timeScale);
    
    console.log('time scale: ' + timeScale);
    console.log('duration: ' + duration);
    console.log('movie length: ' + movieLength + ' seconds');
  });
});