Skip to content

Instantly share code, notes, and snippets.

@Elements-
Created March 26, 2016 18:32
Show Gist options
  • Save Elements-/cf063254730cd754599e to your computer and use it in GitHub Desktop.
Save Elements-/cf063254730cd754599e 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');
});
});
@OllieJones
Copy link

Thanks for this. I'ver run with it a bit. Here: https://gist.github.com/OllieJones/5ffb011fa3a11964154975582360391c

@evandrix
Copy link

(node:) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.

@trulysinclair
Copy link

(node:) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.

I think new Buffer.alloc(100); is the drop-in replacement I could be wrong

@trulysinclair
Copy link

@Elements- would you be able to give an explanation of the code above? Like what mvhd is for?

@davalapar
Copy link

Looks good, just replace new Buffer('mvhd') with Buffer.from('mvhd')

mvhd is the Movie Header containing the duration field, https://stackoverflow.com/a/3233417

@mraxus
Copy link

mraxus commented Mar 9, 2020

Fantastic gist! Love. Thank you. I tried it out on m4a without any problems (with a discrepancy if a few or 10 milliseconds error)

Here is a promise/async version:

const fs = require('fs');
const fsp = fs.promises;

const  buff = new Buffer(100);
const fileHandle = await fsp.open(filepath, 'r');
const { buffer } = await fileHandle.read(buff, 0, 100, 0);
await fileHandle.close();

const start = buffer.indexOf(Buffer.from('mvhd')) + 17;
const timeScale = buffer.readUInt32BE(start, 4);
const duration = buffer.readUInt32BE(start + 4, 4);
const audioLength = Math.floor(duration/timeScale * 1000) / 1000;

@numToStr
Copy link

numToStr commented Apr 5, 2020

Love this gist. Here is my version with all the issue fixed.

const fs = require("fs").promises;

const buff = Buffer.alloc(100);
const header = Buffer.from("mvhd");

async function main() {
    const file = await fs.open("video.mp4", "r");
    const { buffer } = await file.read(buff, 0, 100, 0);

    await file.close();

    const start = buffer.indexOf(header) + 17;
    const timeScale = buffer.readUInt32BE(start);
    const duration = buffer.readUInt32BE(start + 4);

    const audioLength = Math.floor((duration / timeScale) * 1000) / 1000;

    console.log(buffer, header, start, timeScale, duration, audioLength);
}

main();

@juslin03
Copy link

Love this gist. Here is my version with all the issue fixed.

const fs = require("fs").promises;

const buff = Buffer.alloc(100);
const header = Buffer.from("mvhd");

async function main() {
    const file = await fs.open("video.mp4", "r");
    const { buffer } = await file.read(buff, 0, 100, 0);

    await file.close();

    const start = buffer.indexOf(header) + 17;
    const timeScale = buffer.readUInt32BE(start);
    const duration = buffer.readUInt32BE(start + 4);

    const audioLength = Math.floor((duration / timeScale) * 1000) / 1000;

    console.log(buffer, header, start, timeScale, duration, audioLength);
}

main();

thank you very much ! please, how can we extract a part of video using buffer? for example, I have a video of 1 minute and I want to get the first 30s of it.

@juslin03
Copy link

Thank you very much !

@zaid225
Copy link

zaid225 commented Jun 25, 2021

i use this code but it not getting correct result i user this code for getting video duration like i want to know the video is how its long its 30 seconds its 40 seconds anyone can help me

@Ronkiro
Copy link

Ronkiro commented Jul 13, 2021

i use this code but it not getting correct result i user this code for getting video duration like i want to know the video is how its long its 30 seconds its 40 seconds anyone can help me

You probably are using the duration variable, while you should use movieLength to achieve this.

@pundoo
Copy link

pundoo commented Sep 8, 2021

Adding on @numToStr

I ran into some files having mvhd header towards the end (ish). In that case..

const { size } = await fs.stat("video.mp4");
const buff = Buffer.alloc(size);
const { buffer } = await file.read(buff, 0, size, 0);

@redsuperbat
Copy link

redsuperbat commented Apr 25, 2023

The first post has the wrong offset for the start variable. It should be +16 not +17.

This is my updated gist:

  const buf = Buffer.alloc(100);
  const file = await fs.open(resource);
  const { buffer } = await file.read({
    buffer: buf,
    length: 100,
    offset: 0,
    position: 0,
  });
  await file.close();
  
  const start = buffer.indexOf(Buffer.from("mvhd")) + 16;
  const timeScale = buffer.readUInt32BE(start);
  const duration = buffer.readUInt32BE(start + 4);
  const movieLength = Math.floor(duration / timeScale);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment