Skip to content

Commit 752b51d

Browse files
committed
Some fixes, cleanup
1 parent 7e4e573 commit 752b51d

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

pytimecode.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ def set_timecode(self, timecode):
4444
"""sets timecode to argument 'timecode'"""
4545
self.hrs, self.mins, self.secs, self.frs, self.drop_frame = self.parse_timecode(timecode)
4646

47+
def total_seconds(self):
48+
drop_fix = 0
49+
50+
if self.drop_frame:
51+
drop_fix = 2 * (self.mins % 10)
52+
53+
if self.framerate == '59.94':
54+
drop_fix *= 2
55+
56+
return (self.hrs * 3600) + (self.mins * 60) + self.secs + (self.frames - drop_fix) / self.int_framerate
57+
4758
def tc_to_frames(self):
4859
"""converts corrent timecode to frames"""
4960
total_mins = 60 * self.hrs + self.mins
@@ -72,7 +83,8 @@ def frames_to_tc(self):
7283

7384
def calc_drop_frames(self):
7485
# 'formula' taken from http://www.andrewduncan.ws/Timecodes/Timecodes.html
75-
if self.framerate not in ('29.97', '59.94'):
86+
#if self.framerate not in ('29.97', '59.94'):
87+
if not self.drop_frame:
7688
return 0
7789

7890
drop_frames_per_event = 2
@@ -128,17 +140,16 @@ def make_timecode(self):
128140
min_str = self.__set_time_str(self.mins)
129141
sec_str = self.__set_time_str(self.secs)
130142
frame_str = self.__set_time_str(self.frs)
131-
timecode_str = "%s:%s:%s%s%s" % (hr_str, min_str, sec_str, ';' if self.drop_frame else ':', frame_str)
143+
drop_char = ':'
144+
if self.framerate == 'ms':
145+
drop_char = '.'
146+
elif self.drop_frame:
147+
drop_char = ';'
148+
timecode_str = "%s:%s:%s%s%s" % (hr_str, min_str, sec_str, drop_char, frame_str)
132149
return timecode_str
133150

134151
def __set_time_str(self, time):
135152
return str(int(time)).zfill(2)
136-
# return "{0:02f}".format(int(time))
137-
# if len(str(time)) > 1:
138-
# time_str = str(time)
139-
# else:
140-
# time_str = "0%s" % time
141-
# return time_str
142153

143154
def __iter__(self):
144155
return self
@@ -194,7 +205,7 @@ def __add__(self, other):
194205
added_frames = self.frames + other
195206
else:
196207
raise PyTimeCodeError('Type ' + str(type(other)) + ' not supported for arithmetic.')
197-
newtc = PyTimeCode(self.framerate, start_timecode=None, frames=added_frames, drop_frame=self.drop_frame)
208+
newtc = PyTimeCode(self.framerate, frames=added_frames, drop_frame=self.drop_frame)
198209
return newtc
199210

200211
def __sub__(self, other):
@@ -205,7 +216,7 @@ def __sub__(self, other):
205216
subtracted_frames = self.frames - other
206217
else:
207218
raise PyTimeCodeError('Type ' + str(type(other)) + ' not supported for arithmetic.')
208-
newtc = PyTimeCode(self.framerate, start_timecode=None, frames=subtracted_frames, drop_frame=self.drop_frame)
219+
newtc = PyTimeCode(self.framerate, frames=subtracted_frames, drop_frame=self.drop_frame)
209220
return newtc
210221

211222
def __mul__(self, other):
@@ -216,7 +227,7 @@ def __mul__(self, other):
216227
mult_frames = self.frames * other
217228
else:
218229
raise PyTimeCodeError('Type ' + str(type(other)) + ' not supported for arithmetic.')
219-
newtc = PyTimeCode(self.framerate, start_timecode=None, frames=mult_frames, drop_frame=self.drop_frame)
230+
newtc = PyTimeCode(self.framerate, frames=mult_frames, drop_frame=self.drop_frame)
220231
return newtc
221232

222233
def __div__(self, other):
@@ -227,12 +238,11 @@ def __div__(self, other):
227238
div_frames = self.frames // other
228239
else:
229240
raise PyTimeCodeError('Type ' + str(type(other)) + ' not supported for arithmetic.')
230-
newtc = PyTimeCode(self.framerate, start_timecode=None, frames=div_frames, drop_frame=self.drop_frame)
241+
newtc = PyTimeCode(self.framerate, frames=div_frames, drop_frame=self.drop_frame)
231242
return newtc
232243

233244
def __repr__(self):
234245
return self.make_timecode()
235246

236-
237247
class PyTimeCodeError(Exception):
238248
pass

tests/test_pytimecode.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,21 @@ def test_case_1(self):
532532
self.assertEquals(diff2.frames, 1820)
533533
self.assertEquals(diff2.make_timecode(), "00:01:00;22")
534534

535+
# def test_case_2(self):
536+
# pass
537+
# tc = pytimecode.PyTimeCode('ms', '00:00:01:500')
538+
# self.assertEquals(tc.frames, 1500)
539+
# tc.convert_fps('29.97', True)
540+
# #self.assertEquals(tc.make_timecode(), "00:00:01;15")
541+
# self.assertEquals(tc.frames, 1500)
542+
# self.assertEquals(tc.make_timecode(), "00:03:25;06")
543+
#
544+
#
545+
# tc2 = pytimecode.PyTimeCode('29.97', '00:01:01;01')
546+
# tc2.convert_fps('60', False)
547+
# # self.assertEquals(tc2.make_timecode(), '00:01:01:02')
548+
# self.assertEquals(tc2.make_timecode(), '00:00:30:29')
549+
535550
def test_exceptions(self):
536551
e = None
537552
try:

0 commit comments

Comments
 (0)