VideoHelp Forum

Our website is made possible by displaying online advertisements to our visitors. Consider supporting us by disable your adblocker or Try ConvertXtoDVD and convert all your movies to DVD. Free trial ! :)
+ Reply to Thread
Results 1 to 1 of 1
Thread
  1. Here is a Python script which converts an integer frame number to drop-frame SMPTE time code.

    Suggestions for improvement, modifications and bug reports are welcome.

    Code:
    # Code by David Heidelberger, adapted from Andrew Duncan
    # http://www.davidheidelberger.com/blog/?p=29
    # Given an int called framenumber and a double called framerate
    # Framerate should be 29.97, 59.94, or 23.976, otherwise the calculations will be off.
    
    import time
    import math
    
    d = 0
    m = 0
    framerate = 30 / 1.001  #29.97
    framenumber = 0
    framePeriod = 1 / framerate
    
    while framenumber < 120:
    
      dropFrames = round(framerate * .066666)  #Number of frames to drop on the minute marks is the nearest integer to 6% of the framerate
      framesPerHour = round(framerate*60*60)  #Number of frames in an hour
      framesPer24Hours = framesPerHour*24  #Number of frames in a day - timecode rolls over after 24 hours
      framesPer10Minutes = round(framerate * 60 * 10)  #Number of frames per ten minutes
      framesPerMinute = (round(framerate)*60)-  dropFrames  #Number of frames per minute is the round of the framerate * 60 minus the number of dropped frames
    
      if framenumber < 0: #Negative time. Add 24 hours.
    
        framenumber=framesPer24Hours+framenumber
    
    
    #If framenumber is greater than 24 hrs, next operation will rollover clock
        framenumber = framenumber % framesPer24Hours  #% is the modulus operator, which returns a remainder. a % b = the remainder of a/b
    
        #d = framenumber\framesPer10Minutes  # \ means integer division, which is a/b without a remainder. Some languages you could use floor(a/b)
        d = math.floor(framenumber/framesPer10Minutes)
        m = framenumber % framesPer10Minutes
    
      #In the original post, the next line read m>1, which only worked for 29.97. Jean-Baptiste Mardelle correctly pointed out that m should be compared to dropFrames.
      if m>dropFrames:
    
          framenumber=framenumber + (dropFrames*9*d) + math.floor(dropFrames*((m-dropFrames)/framesPerMinute))
    
      else:
    
          framenumber = framenumber + dropFrames*9*d
    
      frRound = round(framerate)
      frames = framenumber % frRound
      seconds = math.floor(framenumber / frRound) % 60
      minutes = math.floor(math.floor(framenumber / frRound) / 60) % 60
      hours = math.floor(math.floor(math.floor(framenumber / frRound) / 60) / 60)
    
      print (round(hours),":",round(minutes),":",round(seconds),":",round(frames))
    
      framenumber += 1
      time.sleep(framePeriod)
    Last edited by chris319; 15th Jun 2019 at 15:25.
    Quote Quote  



Similar Threads