0

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

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
akak
  • 3
  • 1
  • Thanks for the help! These both worked, While I liked the short 3 liner, I do have multiple fps values to pass. I'd like to find out how to pass to STDIN, but can probably google that. Thanks again! – akak Nov 11 '20 at 07:07
  • Check it out: http://kiria.duckdns.org/mstc – akak Nov 11 '20 at 07:20

2 Answers2

0

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
Anh Nhat Tran
  • 561
  • 1
  • 6
  • 18
0

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
Suresh Mangs
  • 705
  • 8
  • 19