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:

and then you skip to…

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



And here’s the code to convert drop frame timecode to a frame number:



And that’s it for this entry. In a post to hopefully come in the near future, I’ll talk about the horrible quicktime 59.94 drop frame bug and how to get around it.

One thought on “Drop Frame Timecode [UPDATED X2]

  1. Pingback: The Editblog » Linkage: June 2010

Leave a Reply