Skip to content

Instantly share code, notes, and snippets.

@numToStr
Created April 5, 2020 07:52
Show Gist options
  • Save numToStr/8469c8ac17deeecd3f7d0a077b6af432 to your computer and use it in GitHub Desktop.
Save numToStr/8469c8ac17deeecd3f7d0a077b6af432 to your computer and use it in GitHub Desktop.
const fs = require("fs").promises;
const buff = Buffer.alloc(100);
const header = Buffer.from("mvhd");
async function length() {
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);
}
length();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment