4

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

asked Mar 18, 2019 at 16:18

5 Answers 5

4

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).

answered Mar 18, 2019 at 16:23
Sign up to request clarification or add additional context in comments.

4 Comments

first of all, it does not provides seconds accurately and it takes too much time to respond.
Oh let me take a look for a better approach
It is not providing accurate data
How accurate/inaccurate is this @MalayM?
3

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();
answered Jun 22, 2021 at 8:25

Comments

2

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)
})
Carcigenicate
46.5k12 gold badges80 silver badges133 bronze badges
answered Mar 18, 2019 at 16:32

Comments

0

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)
 }
answered Mar 7, 2024 at 5:02

Comments

0

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 );
 }
 }
answered Jun 11, 2024 at 10:51

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.