skip to Main Content

Can someone help with a quick script that will convert Milliseconds to Timecode (HH:MM:SS:Frame) with this NPM library?

https://www.npmjs.com/package/ms-to-timecode

I just need to pass a miliseconds number (ie 7036.112) to it and output the converted timecode. I’ve installed npm and the ms-to-timecode library on my debian server. I know perl/bash, and never coded with these library modules.

Any help is greatly appreciated.
-Akak

2

Answers


  1. You need to write a javascript code to use this npm module.

    const msToTimecode = require('ms-to-timecode');
    const ms = 6000;
    const fps = 30
    
    const result = msToTimecode(ms, fps)
    console.log(result) // Print 00:00:06:00
    
    Login or Signup to reply.
  2. If you are using the npm module ms-to-timecode then:

    const msToTimecode = require('ms-to-timecode');
    
    const timeCode = msToTimecode(7036, 112);
    
    console.log(timeCode); // displays 00:00:07:04
    

    If you don’t want to use the npm module then:

    function msToTimeCode(ms, fps) {
        let frames = parseInt((ms/1000 * fps) % fps)
        let seconds = parseInt((ms/1000)%60)
        let minutes = parseInt((ms/(1000*60))%60)
        let hours = parseInt((ms/(1000*60*60))%24);
      
        hours = (hours < 10) ? "0" + hours : hours;
        minutes = (minutes < 10) ? "0" + minutes : minutes;
        seconds = (seconds < 10) ? "0" + seconds : seconds;
        frames = (frames < 10) ? "0" + frames : frames;
      
        return hours + ":" + minutes + ":" + seconds + ":" + frames;
    };
    
    console.log(msToTimeCode(7036, 112));  // displays 00:00:07:04
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search