Skip to content

Instantly share code, notes, and snippets.

@jessetan
Last active April 6, 2023 21:11
Show Gist options
  • Save jessetan/2a2fece59d9afa9461cc to your computer and use it in GitHub Desktop.
Save jessetan/2a2fece59d9afa9461cc to your computer and use it in GitHub Desktop.
Convert seconds to SMPTE timecode JSON object and string
/** Convert seconds to SMPTE timecode JSON object, example input is html video.currentTime */
function secondsToSMPTE(seconds, framerate) {
var f = Math.floor((seconds % 1) * framerate);
var s = Math.floor(seconds);
var m = Math.floor(s / 60);
var h = Math.floor(m / 60);
m = m % 60;
s = s % 60;
return {h: h, m: m, s: s, f: f};
}
/** Pretty print SMPTE timecode JSON object */
function SMPTEToString(timecode) {
if (timecode.h < 10) { timecode.h = "0" + timecode.h; }
if (timecode.m < 10) { timecode.m = "0" + timecode.m; }
if (timecode.s < 10) { timecode.s = "0" + timecode.s; }
if (timecode.f < 10) { timecode.f = "0" + timecode.f; }
return timecode.h + ":" + timecode.m + ":" + timecode.s + ":" + timecode.f;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment