from numpy import uint64 from ..common.Common import * def TimeCodeToMillisec(timecode): if timecode == "": log.error("Empty timecode was given to TimeCodeToMillisec function") tokens = timecode.split(":") if len(tokens) == 0: log.error("Splitting of timecode " + timecode + " produced unexpected output") elif len(tokens) == 1: # No colons - timecode format is in milliseconds return uint64(timecode) hour = 0 min = 0 sec = 0 msec = 0 if len(tokens) >= 2: hour = uint64(tokens[0]) min = uint64(tokens[1]) if len(tokens) >= 3: sec = uint64(tokens[2]) if len(tokens) >= 4: msec = uint64(tokens[3]) min = min + hour*60 sec = sec + min*60 msec = msec + sec*1000 return uint64(msec) def MillisecToTimeCode(milliSec, use_dot_for_millisec = False): Millisecond = 1 Second = 1000 * Millisecond Minute = 60 * Second Hour = 60 * Minute hour = max(0, int(milliSec / Hour)) milliSec -= hour * Hour min = max(0, int(milliSec / Minute)) milliSec -= min * Minute sec = max(0, int(milliSec / Second)) milliSec -= sec * Second msec = max(0, int(milliSec / Millisecond)) milliSec -= sec * Millisecond if hour < 0 or min < 0 or sec < 0 or msec < 0: log.fatal("Could not construct timecode from millisec %v [hour: %v, min: %v, sec: %v, msec: %v]", milliSec, hour, min, sec, msec) if use_dot_for_millisec: timeCode = "%02d:%02d:%02d.%03d" % (hour, min, sec, msec) else: timeCode = "%02d:%02d:%02d:%03d" % (hour, min, sec, msec) return timeCode def FrameIndexToMillisec(frame_index, frame_rate): return int((frame_index * 1000)/frame_rate) def MillisecToFrameIndex(time_in_millisec, frame_rate): return int((time_in_millisec * frame_rate) / 1000) def TimeCodeToFrameIndex(timecode, frame_rate): return MillisecToFrameIndex(TimeCodeToMillisec(timecode), frame_rate) def FrameIndexToTimeCode(frame_index, frame_rate): return MillisecToTimeCode(FrameIndexToMillisec(frame_index, frame_rate))