Is there any way to calculate the video duration in a millisecond from content-length?
request
.get("http://myvideourl.com/filename.mp4")
.on("response", response => {
const content_length = response.headers.content-length;// "content-length": "1986943971"
res.json({
stream_duration: "",
thumb: thumb,
size: content_length,
});
});
Note : Video Format is MP4 , res is express object, request is a httpclient library in NodeJS
5 Answers 5
You can use this npm module It will help you to get the video length even from an url
const { getVideoDurationInSeconds } = require('get-video-duration');
getVideoDurationInSeconds('http://myvideourl.com/filename.mp4').then((duration) => {
console.log(duration)
})
Of course you can then convert it into milliseconds. (x1000).
4 Comments
Not sure regarding 'Content-Length', But this gist will give you the video duration (and other useful data) in Node with fs:
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();
Comments
You can use one of these three npm modules.
https://www.npmjs.com/package/get-video-duration https://www.npmjs.com/package/node-video-duration (deprecated ?) https://www.npmjs.com/package/ffprobe (deprecated)
The first one seems up to date. There is an example from the doc
const { getVideoDurationInSeconds } = require('get-video-duration')
// From a local path...
getVideoDurationInSeconds('video.mov').then((duration) => {
console.log(duration)
})
// From a URL...
getVideoDurationInSeconds('http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4').then((duration) => {
console.log(duration)
})
// From a readable stream...
const fs = require('fs')
const stream = fs.createReadStream('video.mov')
getVideoDurationInSeconds(stream).then((duration) => {
console.log(duration)
})
Comments
The content length of a media file has no simple relationship to the media length. However, you can use a media library like ffmpeg to get accurate information about your media.
//First save the buffer to a temporary file
//...
new ffmpeg('temp/temp-file-name.mp4', function (err, video) {
if (!err)
console.log(video.metadata.duration.seconds)
}
Comments
This can be done using ffmpeg.probe.
import ffmpegStatic from "ffmpeg-static";
import ffmpegfluent, { ffprobe } from "fluent-ffmpeg";
import {path as ffprobePath} from "ffprobe-static";
ffmpegfluent.setFfmpegPath(ffmpegStatic as string);
ffmpegfluent.setFfprobePath(ffprobePath);
ffmpegfluent.ffprobe(videoPath,(err,metadata)=>{
if(err){
console.log(err);
reject();
}else{
console.log("Processing finished");
if(typeof metadata.format.duration==undefined)
reject();
resolve(metadata.format.duration as number );
}
}