I'm using this script to turn frames into timecode. Why do I get 00:30:00:00
as result when using 43156.8
as value, but 00:29:59:23
when passing the exact same value as a variable?
function framesToTC($frames, $framerate) {
$hours = floor( $frames / ( $framerate * 60 * 60 ) );
$framesleft = $frames - ($hours * $framerate * 60 * 60);
$minutes = floor( $framesleft / ( $framerate * 60 ) );
$framesleft -= ( $minutes * $framerate * 60 );
$seconds = floor( $framesleft / ( $framerate ) );
$framesleft -= ( $seconds * $framerate );
$tc = sprintf("%02d:%02d:%02d:%02d", $hours, $minutes, $seconds, $framesleft );
return $tc;
}
$frames = 86400000/48000*23.976; //43156.8
echo framesToTC($frames, 23.976) . '<br />'; //00:29:59:23
echo framesToTC(43156.8, 23.976); //00:30:00:00
I exploded you function (when in doubt print it out, that's my motto)
Outputs
See it here. http://sandbox.onlinephpfunctions.com/code/918f5187e5dbab3f03812112a54f6b57754f5e7e
You can "fix" i use the word loosly by rounding the
$framesleft
before it. HereOutputs
Check it out here
http://sandbox.onlinephpfunctions.com/code/c78037e52d97ff50094cbc3b045309d155a302fa
I'm guessing it has something to do with float precision, but that's above my pay grade. But at least that gets rid of the deviation in this case. You may have to use
ceil
orround
I don't know what the correct answer should be. I just poke at stuff tell it works or breaks all the way ... lol