Skip to main content
russellaugust
Inspiring
May 14, 2022
Answered

Time() class seconds math producing incorrect results?

I was doing some extendscript work with dropping in markers and had to do some Time() math.  I chose to do this with the .seconds method because tick math is a bit of a pain as it is stored as a string.

 

So as I was doing the math, I discovered that my results were randomly off by a frame.  Dug a little deeper and found the issue was happening with performing float math with seconds.

 

var demo1 = new Time();
var demo2 = new Time();

// Ticks are a form of time measurement in Adobeland.
demo1.ticks = "13558104000000";
demo2.ticks = "12743136000000";

// Setting two variables to experiment with, as named.
var demo_seconds    = new Time();
var demo_ticks      = new Time();

// Seconds math is simple and done with float.
demo_seconds.seconds = demo1.seconds + demo2.seconds;

// Ticks, for some reason, are stored as strings and need to be converted to int, then converted back to a string to store.
var demo_ticks_string   = parseInt(demo1.ticks) + parseInt(demo2.ticks);
demo_ticks.ticks        = demo_ticks_string.toString();

// This is pulling some stuff Premiere to prepare to convert time to timecode HH:MM:SS:FF
var currentSeqSettings	    = app.project.activeSequence.getSettings();

// Formatting the results of the seconds
var timeAsText_secs         = demo_seconds.getFormatted(currentSeqSettings.videoFrameRate, app.project.activeSequence.videoDisplayFormat);

// Formatting the results of the ticks
var timeAsText_ticks        = demo_ticks.getFormatted(currentSeqSettings.videoFrameRate, app.project.activeSequence.videoDisplayFormat);

// Display results in seconds
$.writeln(demo1.seconds + " + " + demo2.seconds + " = " + demo_seconds.seconds);
$.writeln(timeAsText_secs);

// Display results in in ticks
$.writeln(demo1.seconds + " + " + demo2.seconds + " = " + demo_ticks.seconds);
$.writeln(timeAsText_ticks);

 

Seconds Math:

53.375 + 50.1666666666667 = 103.541666666663
00:01:43:12

Ticks Math (when converted to an integer)
53.375 + 50.1666666666667 = 103.541666666667
00:01:43:13

 

So what's going on here?  Am I missing something obvious?  Why is the float math seemingly wrong?

This topic is closed to new replies. Start a new post to keep the conversation going.
billgarv2007Correct answer
Inspiring
May 25, 2022

I had a similar issues with markers on the timeline for a panel I was working on.  I reached out to Bruce and he mentioned the frame boundary.  I finally figured out that when you're gettting a markers In or Out point programmically it uses the audio time units as the base unit.  I called these "Audio Frames".  An Audio frame and a video frame do not align so an audio frame could start in a video frame and then end in the following video frame.  This accounts for the "rounding".  Here is the comment I put in my script.

/*Because Premiere rounds to "frame boundaries" when setting In and Out points programatically, we need to set a constant in ticks to add to the in and out point so the In and Out's are set on the correct video frame. The smallest "frame" unit is based on the audio sample rate of 48000 hz.  Since we're working in Ticks and there are 254,016,000,000 ticks/second we divide 254,016,000,000 by 48000 to get 5292000 ticks per "audio frame". An "Audio Frame" can span 2 video frames. If our markers In/Out happens to be on one of these spanning audio frames the In/Out Point is set on the preceeding video frame.  When we add 5292000 to the In and Out point we move our In/Out Point one "Audio Frame" forward making sure we aren't on a spanning audio frame.*/

I hope this helps.

 

4 replies

russellaugust
Inspiring
May 14, 2022

It seems like there is some rounding issue in Time.getFormatted(). What is a workaround to this to get frame accuracy?

Translate
Bruce Bullis
Legend
May 16, 2022

It's likely forcing the value to a frame boundary.

Translate