[英]Find Video Duration From Content-Length in NodeJS
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注:视频格式为 MP4, res为 express 对象, request为 NodeJS 中的 httpclient 库
You can use this npm module It will help you to get the video length even from an url你可以使用这个npm 模块它会帮助你从一个 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).
(x1000)。
You can use one of these three npm modules.您可以使用这三个 npm 模块之一。
https://www.npmjs.com/package/get-video-duration https://www.npmjs.com/package/node-video-duration (deprecated ?) https://www.npmjs.com/package/ffprobe (deprecated) https://www.npmjs.com/package/get-video-duration https://www.npmjs.com/package/node-video-duration (已弃用?) https://www.npmjs.com/package/ ffprobe (已弃用)
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)
})
Not sure regarding 'Content-Length', But this gist will give you the video duration (and other useful data) in Node with fs:不确定关于“内容长度”,但是这个要点将使用 fs 为您提供 Node 中的视频持续时间(和其他有用的数据):
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.