Time Code to Seconds

Convert HH:MM:SS:FF timecode to seconds for use in Adobe Premiere Pro SDK

HTML

<div data-el="timeCode">a</div>
<div data-el="fps">b</div>
<div data-el="dropFrame">c</div>
<div data-el="output">d</div>

JavaScript

// Convert HH:MM:SS:FF timecode to seconds for use in Adobe Premiere Pro SDK
// Tested 23.976, 29.97, and 60 in Premiere.
// 29.97 Drop Frame has some issues with minutes 20, 21, and 22 being short one frame.
// https://forums.adobe.com/thread/1961613

// Change the default values if you want
var defaultTimeCode = '00:03:00:00';
var defaultFPS = '29.97';

// time is HH:MM:SS:FF (not detecting ';' vs ':')
// fps is '23.976', '29.97', '60', etc
// dropFrame is true/false
function convertTimeCode(time, fps, dropFrame) {
  time = time.split(':');
  if (time.length != 4) {
    // we could make assumptions and fill in FF or whatever, but for now just return false
    return false;
  } else {
    // combine hours and minutes into minutes
    var minutes = (time[0] * 60) + Number(time[1]);
    // now that we have the total number of minutes, subtract how many minutes are divisible by ten
    var minutesCounted = minutes - Math.floor(minutes / 10);
    // combine mintues and seconds to seconds
    var seconds = (minutes * 60) + Number(time[2]);
    var frames = Number(time[3]);
    // fractions of frames? no. round up though.
    frames += Math.ceil(seconds * fps);
    // only run if we're dealing with dropFrame timecode
    if (dropFrame) {
      // we don't subtract 2 if we're on frame 0 or 1 of the first minute so let's add one or two frames back in
      if (time[3] < 2 && minutesCounted > 0 && time[2] == '00') {
        // 1 frame if we're on '01' and 2 if we're on '00'
        frames += (2 - Number(time[3]));
      }
      // from the minutes counted above, we'll subtract 2 frames per minute
      frames = frames - (minutesCounted * 2);
    }
    // convert frames back into seconds and multiply by (1001 / 1000)
    return parseFloat(parseFloat(frames / fps) * 1.001);
  }
}

// This basically just calls the convertTime function, but keeps the four calls to it cleaner
function updateCalc() {
  var timeCode = $('[data-el="timeCodeVal"]').val();
  var fps =...