为什么在变量中使用相同的值时会得到不同的时间码?

时间:2017-11-29 02:54:33

标签: php timecodes

我正在使用此脚本将帧转换为时间码。使用00:30:00:00作为值时,为什么会得到43156.8,而传递与变量完全相同的值时会00:29:59:23

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

1 个答案:

答案 0 :(得分:3)

我爆炸了你的功能(当有疑问打印出来时,那是我的座右铭)

<?php

function framesToTC($frames, $framerate) {
    echo "\n".str_pad(" START ", 40, "=", STR_PAD_BOTH)."\n";
    echo "frames: {$frames}\n";
    echo "framerate: {$framerate}\n";

    $hours = floor( $frames / ( $framerate * 60 * 60 ) );
    echo "hours: {$hours}\n";

    $framesleft = $frames - ($hours * $framerate * 60 * 60);
    echo "framesleft: {$framesleft}\n";

    $x = $framesleft / ( $framerate * 60 );
    echo "x: {$x}\n";
    echo str_pad(" DEVIATION ", 40, "-", STR_PAD_BOTH)."\n";
    $minutes = floor( $x );
    echo "minute: {$minutes}\n";

    $framesleft -= ( $minutes * $framerate * 60 );
    echo "framesleft: {$framesleft}\n";

    $seconds = floor( $framesleft / ( $framerate ) );
    echo "seconds: {$seconds}\n";

    $framesleft -= ( $seconds * $framerate );
    echo "framesleft: {$framesleft}\n";

    $tc = sprintf("%02d:%02d:%02d:%02d", $hours, $minutes, $seconds, $framesleft );
    echo "tc: {$tc}\n";

    return $tc;
}

$frames = (86400000/48000*23.976); //43156.8
echo framesToTC($frames, 23.976) . "\n"; //00:29:59:23
echo framesToTC(43156.8, 23.976); //00:30:00:00

$frames = (86400000/48000*23.976); //43156.8
echo framesToTC($frames, 23.976) . "\n"; //00:29:59:23
echo framesToTC(43156.8, 23.976); //00:30:00:00

输出

================ START =================
frames: 43156.8
framerate: 23.976
hours: 0
framesleft: 43156.8
x: 30
-------------- DEVIATION ---------------
minute: 29
framesleft: 1438.56
seconds: 59
framesleft: 23.975999999998
tc: 00:29:59:23
00:29:59:23

================ START =================
frames: 43156.8
framerate: 23.976
hours: 0
framesleft: 43156.8
x: 30
-------------- DEVIATION ---------------
minute: 30
framesleft: 7.2759576141834E-12
seconds: 0
framesleft: 7.2759576141834E-12
tc: 00:30:00:00
00:30:00:00

在此处查看。 http://sandbox.onlinephpfunctions.com/code/918f5187e5dbab3f03812112a54f6b57754f5e7e

你可以修复&#34;我通过四舍五入$framesleft来使用“松散”这个词。这里

echo "framesleft: {$framesleft}\n";

$framesleft = floor($framesleft);

$x = $framesleft / ( $framerate * 60 );

输出

================ START =================
frames: 43156.8
framerate: 23.976
hours: 0
framesleft: 43156.8
x: 29.999443888333
-------------- DEVIATION ---------------
minute: 29
framesleft: 1437.76
seconds: 59
framesleft: 23.176000000002
tc: 00:29:59:23
00:29:59:23

================ START =================
frames: 43156.8
framerate: 23.976
hours: 0
framesleft: 43156.8
x: 29.999443888333
-------------- DEVIATION ---------------
minute: 29
framesleft: 1437.76
seconds: 59
framesleft: 23.176000000002
tc: 00:29:59:23
00:29:59:23

在这里查看

http://sandbox.onlinephpfunctions.com/code/c78037e52d97ff50094cbc3b045309d155a302fa

我猜它与浮动精度有关,但是它高于我的工资等级。但至少在这种情况下摆脱了偏差。您可能必须使用ceilround我不知道答案应该是什么。我只是捅东西告诉它工作或一路打破...大声笑