有什么方法可以根据内容长度来计算视频持续时间(以毫秒为单位)?
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,
});
});
注意:视频格式是MP4, res 是Express对象, request 是NodeJS中的httpclient库
答案 0 :(得分:1)
您可以使用此npm module 甚至可以从网址获取视频长度
const { getVideoDurationInSeconds } = require('get-video-duration');
getVideoDurationInSeconds('http://myvideourl.com/filename.mp4').then((duration) => {
console.log(duration)
})
当然,您可以将其转换为毫秒。 (x1000)。
答案 1 :(得分:1)
不确定关于“内容长度”,但是 this gist will give you the video duration(和其他有用的数据)在 Node 中使用 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();
答案 2 :(得分:0)
您可以使用这三个npm模块之一。
https://www.npmjs.com/package/get-video-duration https://www.npmjs.com/package/node-video-duration(已淘汰?) https://www.npmjs.com/package/ffprobe(已弃用)
第一个似乎是最新的。文档中有一个例子
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)
})