4

I got a question regarding calculating time code delta.
I read metadata from a movie file containing a timecode formated HH:MM:SS:FF

(FF = frame, 00->23 for example. So its like 00 to framerate-1)

So i get some data like 15:41:08:02 and from another refrence file i get 15:41:07:00
Now I have to calculate the timeoffset (like timedelta but just with frames).
How would i go around doing this?

wRAR
  • 25,009
  • 4
  • 84
  • 97
Malu05
  • 119
  • 2
  • 3
  • 10

4 Answers4

7
framerate = 24

def timecode_to_frames(timecode):
    return sum(f * int(t) for f,t in zip((3600*framerate, 60*framerate, framerate, 1), timecode.split(':')))

print timecode_to_frames('15:41:08:02') - timecode_to_frames('15:41:07:00')
# returns 26

def frames_to_timecode(frames):
    return '{0:02d}:{1:02d}:{2:02d}:{3:02d}'.format(frames / (3600*framerate),
                                                    frames / (60*framerate) % 60,
                                                    frames / framerate % 60,
                                                    frames % framerate)

print frames_to_timecode(26)
# returns "00:00:01:02"
eumiro
  • 207,213
  • 34
  • 299
  • 261
  • Thanks alot for all the quick answers everybody! Surprised how kind and fast people are here. – Malu05 Dec 13 '11 at 12:25
2

I'd just use gobal frame numbers for all computations, converting back to timecodes only for display

def tc_to_frame(hh, mm, ss, ff):
    return ff + (ss + mm*60 + hh*3600) * frame_rate

def frame_to_tc(fn):
    ff = fn % frame_rate
    s = fn // frame_rate
    return (s // 3600, s // 60 % 60, s % 60, ff)

for negative frame numbers I'd prepend a minus to the representation of the absolute value

6502
  • 112,025
  • 15
  • 165
  • 265
1

If the timecode is SMPTE timecode, you may need to take into account drop frames. Drop-frame timecodes drop frame numbers 0 and 1 of the first second of every minute, except when the number of minutes is divisible by 10.

This page provides some history background with formulas to convert between timecodes and frame numbers.

yannick
  • 73
  • 1
  • 6
  • For drop-frame timecode, the timeoffset between 01:08:59:29 and 01:09:00:02 should be 1, shouldn't it? – yannick Apr 10 '13 at 20:26
0

Using the timecode module this is quite easy:

import timecode as tc
tc1 = tc.Timecode(24, "15:41:08:02")
tc2 = tc.Timecode(24, "15:41:07:00")
delta = tc1 - tc2
print(delta.frames)

gives you 26

Spencer
  • 1,931
  • 1
  • 21
  • 44