最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Find Video Duration From Content-Length in NodeJS - Stack Overflow

programmeradmin3浏览0评论

Is there any way to calculate the video duration in a millisecond from content-length?

request
  .get(".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

Is there any way to calculate the video duration in a millisecond from content-length?

request
  .get("http://myvideourl./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

Share Improve this question asked Mar 18, 2019 at 16:18 That's EnamThat's Enam 2862 gold badges3 silver badges16 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 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./filename.mp4').then((duration) => {
    console.log(duration)
}) 

Of course you can then convert it into milliseconds. (x1000).

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();

You can use one of these three npm modules.

https://www.npmjs./package/get-video-duration https://www.npmjs./package/node-video-duration (deprecated ?) https://www.npmjs./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)
})

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

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 );
            }
          }
发布评论

评论列表(0)

  1. 暂无评论