As in, "Ugh, there's a blog?"

Main menu

Post navigation

Drop Frame Timecode [UPDATED X2]

In a few moments in this post, I’m going to include some pseudocode for converting between drop frame timecode and a frame number and vice versa, regardless of framerate. You can skip to that if you want. Or, stay tuned for a little bit of background info. Either way, click through the jump…

I’ve been working on a program recently that will hopefully be posted soon that deals with timecode and Quicktime movies. Specifically, adding timecode tracks and doing timecode addition and subtraction. In order to do this, you need to convert the timecode, which is in an hours:minutes:seconds:frames format into a number representing the total number of frames, do your math with frame numbers, and convert back to the timecode format. For non-drop frame timecode, this is trivial. It gets complicated when you’re dealing with drop frame timecode. If you don’t know what that is, it’s a way of accurately measuring the running time of video that doesn’t run at an even frame rate. In America, video runs at 29.97 frames per second, but we usually count it at 30 fps. This means after a while, the number we use to count is wrong. The solution to this problem is drop frame timecode, which is sort of like leap year for timecode. At 29.97 fps, every minute (except ones divisible by ten), you skip counting the first two frames. You know you’re looking at drop frame timecode because the colon between minutes and frames is usually replaced by a semicolon. For example, you go:

It’s pretty difficult to calculate all of this if you’re given a frame number, or to calculate a frame number if you’re given this. After a lot of searching, I’ve only been able to come up with one website that sets out a formula for how to calculate this, Andrew Duncan’s excellent timecode page. The code I’m about to post is more or less borrowed straight from that site (with Andrew’s permission). I’m really making only two changes. First, I’m expanding a couple steps to make it a little more like a computer program, and second, I’m generalizing it so it can calculate drop frame timecode regardless of the frame rate. Andrew’s site does 29.97 drop frame only, but you can also have drop frame timecode for 59.94 and even 23.976 (although I don’t believe it’s part of the SMPTE spec and I know Final Cut doesn’t support 24p DF). For 59.94DF, you drop the first four frames per minute. And with 23.976, you drop the first two like in 29.97. So, what I’m presenting here are basically Andrew’s formulas, fleshed out a little bit, and generalized to other frame rates. It’s worth mentioning also that Quicktime has a bug when it comes to dealing with 59.94 DF timecode. In my next post, I’ll explain how to work around that bug. So, without further ado, here’s some pseudocode in a vaguely Java style. First, we’ll convert a frame number into hours, minutes, seconds, and frames.

[This code has been updated on 8/8/2010 to correct a bug on line 27 caught by Jean-Baptiste Mardelle of Kdenlive. It was updated again on 1/4/2012 to fix a minor syntax error and correct a display issue with < and > symbols. Thanks to Martin Baker for catching those.]

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

//CONVERT A FRAME NUMBER TO DROP FRAME TIMECODE

//Code by David Heidelberger, adapted from Andrew Duncan

//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.

intd;

intm;

intdropFrames=round(framerate*.066666);//Number of frames to drop on the minute marks is the nearest integer to 6% of the framerate

intframesPerHour=round(framerate*60*60);//Number of frames in an hour

intframesPer24Hours=framesPerHour*24;//Number of frames in a day - timecode rolls over after 24 hours

intframesPer10Minutes=round(framerate*60*10);//Number of frames per ten minutes

intframesPerMinute=(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)

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.