parseToFrames static method

int parseToFrames(
  1. String timecodeString, {
  2. TimecodeFramerate? framerate,
})

The default format for a timecodeString is HH:MM:SS:FF where FF stands for the number of frames after the current second and may be 0 <= FF < fps.

When a drop frame encoding has been employed, the format will be HH:MM:SS;FF (final semicolon) or HH;MM;SS;FF (all semicolons).

An alternate form looks like this HH:MM:SS.SSS where the seconds are specified in floating point form. This usually indicates the fps is 1000 (millisecond) but can be used with any framerate.

Implementation

static int parseToFrames(String timecodeString, {TimecodeFramerate? framerate}) {
  framerate ??= TimecodeFramerate(defaultFPS);
  var isDropFrame = timecodeString.contains(';');
  if (isDropFrame != framerate.isDropFrame) {
    throw TimecodeException(
        'timecode string and framerate mismatch. Framerates that employ drop frame encoding (29.97 and 59.94) use a timecode format like this 00:00:00;00');
  }
  var parts = timecodeString.replaceAll(';', ':').split(':').map<double>((e) => double.tryParse(e) ?? 0).toList();

  // if the timecode were fractional, there will be only three elements
  if (parts.length < 3) return 0;

  var hh = parts[0].toInt();
  var mm = parts[1].toInt();
  var ss = parts[2];
  int ff = (parts.length == 4) ? parts[3].toInt() : 0;

  var totalMinutes = mm + hh * 60;
  var totalSeconds = ss + totalMinutes * 60;
  var timecodeFrames = ff + framerate.timecodeSecondsToFrames(totalSeconds);
  return framerate.realFrames(timecodeFrames);
}