convert timecode into seconds

Hi there I have been looking around the apple script forum for a way to use scripting to convert timecode (non drop) into seconds. In this case the frame rate of the clip is 23.98.

I want to do this so I can take the result and add it to an FCPX xml document.

I’ve found a few threads about converting timecode to frames but not seconds. I tried reverse engineering those scripts but honestly I think I am having trouble with the math of it.

The apple documentation on fcpxml reads:

Time values are expressed as a rational number of seconds with a 64-bit numerator and a 32-bit denominator. Frame rates for NTSC-compatible media, for example, use a frame duration of 1001/30000s (29.97 fps) or 1001/60000s (59.94 fps). If a time value is equal to a whole number of seconds, the fraction may be reduced into whole seconds (for example, 5s).

Any ideas?

Thanks!

Hello.

if you want to convert a number of frames that has a rate 23.98fps, then you take your number of frames,and multiplies them by 23.98, and you get the seconds.

If you want the new number of frames after having converted to a different framerate, then the new Number of frames becomes oldnumber of frames dives with old framerate.

set oldFrameRate to 23.98
set newFrameRate to 29.97
set oldFrameCount to "Undefined"
set newFramecount to (oldFrameCount * oldFrameRate) / newFrameRate

This part is how to get a whole number of seconds:

set clipTimeSpan to 5.0

if clipTimeSpan mod 1 < 1.0E-4 then
	set timeSpanText to "" & (clipTimeSpan div 1) & "s"
else
	set timeSpanText to "" & clipTimeSpan & "s"
end if

Hopefully this was what you were looking for.

Hi TB thanks for these ideas, but what I need to be able to do is convert from timecode to seconds. Not convert frame rate.

For example convert 11:38:09:04 knowing it’s frame rate is 23.98, and making that into seconds.

I was looking at the script in this post but not sure how to modify it to my purposes (not even sure if this is the write script to be heading towards):

http://macscripter.net/viewtopic.php?id=43530

Hello.

As I have understood it, you have 11:38:09:04 worth of footage, at a framerate of 23.98 frames per second.

I believe that you don’t have to involve 11 hours 38 minutes and 9 seconds and 9 hundreths with the frame rate in order to get the seconds. Then the task is to convert the time into seconds.

set res to makeSeconds from "11:38:09:04"
--> 4.188904E+4
--> (41889.04 seconds)
on makeSeconds from aString
	set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
	set bits to text items of aString
	set AppleScript's text item delimiters to tids
	if (count bits) ≠ 4 then error "makeSeconds: wrong format of input should be hh:mm:ss:ff"
	tell bits
		set h to text item 1 of it
		set m to text item 2 of it
		set s to text item 3 of it
		set f to text item 4 of it
	end tell
	return (h * 3600 + m * 60 + s + f / 100)
end makeSeconds

TB,

Thanks so much! I think this is super close and does what I asked, but when I check the math by comparing this timecode 11:38:09:04 in FCPX and then export that as an XML file from FCPX, within the XML file that timecode is expressed as 50317267/1200s.

I wonder what math I am missing to get that?

Perhaps that is beyond the scope of this forum?

Hello.

I’m sure someone can help you with this, but I can’t really. The thing is, is that 41889,04*1200 is not the number mentioned, so it is just not about expanding the number by 1200.

I tried to change the handler to do the calculation the same way as in the other thread, but I am nowhere near the number you are getting.

set res to makeSeconds from "11:38:09:04" by "23,97"
on makeSeconds from aString by frameRate
	set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
	set bits to text items of aString
	set AppleScript's text item delimiters to tids
	if (count bits) ≠ 4 then error "makeSeconds: wrong format of input should be hh:mm:ss:ff"
	tell bits
		set h to text item 1 of it
		set m to text item 2 of it
		set s to text item 3 of it
		set f to text item 4 of it
	end tell
	return (((h * 3600 + m * 60 + s) * frameRate) + f )
end makeSeconds

I suspect it is, though: 41889.04 * 1200 / 50317267 = 0.998997978169. I think what’s being used is a CMTime struct, about which the docs say:

Note that last sentence about rounding, and remember that AS doesn’t do 64-bit ints.

Hello Shane.

I was just about to perform the ratio-test. :slight_smile:

What you write is both interesting, and discomforting, because then AppleScript, “vanilla AppleScript” anyway, can’t be used to perform the calculation. Using AsObjC for manipulating 64-bit ints, is nothing I have done, nor know how to do.

The best you can do is use NSDecimalNumber, but you will get exactly the same result. The question is whether it really matters.

Put another way: going from an integer number of frames to a timecode when the frame rate is 1001/30000 will involve some rounding. Given that, you can’t expect to come up with an exact match when you go the other way, because the timecode you’re using has already been rounded.

Hello.

One could possibly get a better result than as of now, if one read in the number from a file, and then parsed the number as it should have been. I really have to pass on this one, simply because I am too rusty for this in Objective-C/Cocoa at the moment to make something work within a couple of hours.

The ratio for 23.98 = 575520/24000 (1/23.98 = 24000/575520), should someone feel the call. :slight_smile: