Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 39 additions & 16 deletions pytimecode.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


class PyTimeCode(object):
def __init__(self, framerate, start_timecode = None, start_seconds=None, frames = None, drop_frame = False, iter_return="tc"):
def __init__(self, framerate, start_timecode = None, frames = None, drop_frame = False, iter_return="tc"):
"""frame rate can be string '60', '59.94', '50', '30', '29.97', '25', '24', '23.98', or 'ms'"""
self.framerate = framerate
self.int_framerate = self.set_int_framerate()
Expand All @@ -30,28 +30,26 @@ def __init__(self, framerate, start_timecode = None, start_seconds=None, frames
elif not frames==None:#because 0==False, and frames can be 0
self.frames = int(frames)
self.frames_to_tc(frame_only=True)
elif not start_seconds==None:#because 0==False, and frames can be 0
start_timecode = self.float_to_tc(start_seconds)
#self.set_timecode(start_timecode)
self.tc_to_frames()
self.__check_drop_frame__()

def set_timecode(self, timecode):
"""sets timecode to argument 'timecode'"""
self.hrs, self.mins, self.secs, self.frs = self.parse_timecode(timecode)

def float_to_tc(self, seconds):
self.frames = int(seconds * self.int_framerate)
return self.frames_to_tc()

def tc_to_frames(self):
"""converts corrent timecode to frames"""
frames = int((((self.hrs * 3600) + (self.mins * 60) + self.secs) * float(self.framerate)) + self.frs)
frames = (((self.hrs * 3600) + (self.mins * 60) + self.secs) * self.int_framerate) + self.frs
if self.drop_frame:
del_frames = self.calc_drop_frames()
frames = frames - del_frames
self.frames = frames

return self.frames

def tc_to_milliseconds(self):
"""converts timecode to milliseconds"""
ms = self.hrs*3600 + self.mins*60 + self.secs + self.frs/float(self.int_framerate)
return int(round(ms * 1000))

def frames_to_tc(self, frame_only=False):
"""converts frames back to timecode, if frame_only==True, it needs to
calculate the drop frames without looking at self.hrs, etc."""
Expand Down Expand Up @@ -121,15 +119,15 @@ def parse_timecode(self, timecode):
hrs = int(timecode[0:2])
mins = int(timecode[3:5])
secs = int(timecode[6:8])
# print(hrs, mins, secs, frs)
return hrs, mins, secs, frs

def make_timecode(self):
self.frames_to_tc()
hr_str = self.__set_time_str(self.hrs)
min_str = self.__set_time_str(self.mins)
sec_str = self.__set_time_str(self.secs)
frame_str = self.__set_time_str(self.frs)
timecode_str = "%s:%s:%s:%s" % (hr_str, min_str, sec_str, frame_str)
timecode_str = "{0:02d}:{1:02d}:{2:02d}:{3:02d}".format(int(self.hrs),
int(self.mins),
int(self.secs),
int(self.frs))
return timecode_str

def __set_time_str(self, time):
Expand Down Expand Up @@ -228,6 +226,31 @@ def __div__(self, other):

def __repr__(self):
return self.make_timecode()

def __eq__(self, other):
if not isinstance(other, type(self)): return NotImplemented
return self.tc_to_frames() == other.tc_to_frames()

def __ne__(self, other):
if not isinstance(other, type(self)): return NotImplemented
return self.tc_to_frames() != other.tc_to_frames()

def __le__(self, other):
if not isinstance(other, type(self)): return NotImplemented
return self.tc_to_frames() <= other.tc_to_frames()

def __lt__(self, other):
if not isinstance(other, type(self)): return NotImplemented
return self.tc_to_frames() < other.tc_to_frames()

def __ge__(self, other):
if not isinstance(other, type(self)): return NotImplemented
return self.tc_to_frames() >= other.tc_to_frames()

def __gt__(self, other):
if not isinstance(other, type(self)): return NotImplemented
return self.tc_to_frames() > other.tc_to_frames()


class PyTimeCodeError(Exception):
pass