Skip to content

Instantly share code, notes, and snippets.

@snehesht
Forked from Elements-/getMP4Length.js
Created May 17, 2018 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snehesht/390ae2b905d49aa3f05b781aa352a29d to your computer and use it in GitHub Desktop.
Save snehesht/390ae2b905d49aa3f05b781aa352a29d to your computer and use it in GitHub Desktop.
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');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment