pythonvideotimecodes

How to do timecode calculation?


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?


Solution

  • 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"