转换MP3持续时间

发布于 10-15 21:15 字数 281 浏览 2 评论 0原文

我正在使用 iTunes 库从大约 1,100 个 mp3 中获取数据,但在将库的持续时间转换为分钟和秒时遇到了一个小问题。

$duration = 1893642;
$minutes = bcmod(($duration / 60), 60);
$seconds = bcmod($duration, 60);
echo $minutes.":".$seconds; //returns 0:42

问题是这个特定的 MP3 实际上是 31:42。关于为什么这不起作用的任何想法?

I'm using my iTunes library to get data from about 1,100 mp3s and I'm running into a small issue in getting the duration of the library into minutes and seconds.

$duration = 1893642;
$minutes = bcmod(($duration / 60), 60);
$seconds = bcmod($duration, 60);
echo $minutes.":".$seconds; //returns 0:42

The problem is that this specific MP3 is actually 31:42. Any thoughts on why this isn't working?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

独自唱情﹋歌2024-10-22 21:15:27
$minutes = bcmod(($duration / 60), 60);

是以分钟为模 60。除非你的曲目超过一个小时,否则它总是显示 0。
你希望它是

$minutes = floor($duration / 60);
$minutes = bcmod(($duration / 60), 60);

is taking the minutes modulo 60. Unless your track is over an hour it will always say 0.
You want it to be

$minutes = floor($duration / 60);
明月松间行2024-10-22 21:15:27

尝试这个功能

function formatTime($secs) {
    $times = array(3600, 60, 1);
    $time = '';
    $tmp = '';
    for($i = 0; $i < 3; $i++) {
        $tmp = floor($secs / $times[$i]);
        if($tmp < 1) {
            $tmp = '00';
        }
        elseif($tmp < 10) {
            $tmp = '0' . $tmp;
        }
        $time .= $tmp;
        if($i < 2) {
            $time .= ':';
        }
        $secs = $secs % $times[$i];
    }
    return $time;
}

Try this function

function formatTime($secs) {
    $times = array(3600, 60, 1);
    $time = '';
    $tmp = '';
    for($i = 0; $i < 3; $i++) {
        $tmp = floor($secs / $times[$i]);
        if($tmp < 1) {
            $tmp = '00';
        }
        elseif($tmp < 10) {
            $tmp = '0' . $tmp;
        }
        $time .= $tmp;
        if($i < 2) {
            $time .= ':';
        }
        $secs = $secs % $times[$i];
    }
    return $time;
}
萌化2024-10-22 21:15:27

不确定在编写此问题时以下函数是否可用,但由于这是我一直在问自己的问题,所以这里就这样。

我使用了上面的答案:

$seconds = bcmod($row{'playtime_seconds'}, 60);
$minutes = floor($row{'playtime_seconds'} / 60);
$hours = floor($minutes / 60);

这在大多数情况下都有效,但没有填充 - 所以你可以在应该是 20:01 的时候得到 20:1 - 而且它不会超过一个小时 - 一个长度进来 length="1:70:9" - 所以有一种替代方案就是使用“日期”功能。

<?=date("H:i:s", $duration); ?>

从该秒数返回 00:31:42

Not sure if the following function was available when this question was written, but as it's a question I've been asking myself so here goes.

I used the answer above:

$seconds = bcmod($row{'playtime_seconds'}, 60);
$minutes = floor($row{'playtime_seconds'} / 60);
$hours = floor($minutes / 60);

Which works for the majority of times, but there is no padding - so you can end up with 20:1 when it should be 20:01 - and it's not to good over an hour - one length comes in at length="1:70:9" - so an alternative is to use the "date" function.

<?=date("H:i:s", $duration); ?>

which returns 00:31:42 from that number of seconds

青朷2024-10-22 21:15:27
$duration_str = sprintf('%s:%02s:%02s',
 floor($duration_int / 3600), // hours
 floor($duration_int / 60) - floor($duration_int / 3600) * 60, // minutes
 $duration_int % 60); // seconds

*printf 函数提供格式化功能。在本例中是前导零。

分钟行是最复杂的部分,因为您必须计算小时(持续时间 [s] / 3600 [s/h]),然后向下舍入为整数 (floor()),然后相乘用 60 转换为分钟,然后从总分钟数中减去该值(持续时间 [s] / 60 [s/m])。

如果您的持续时间短于一小时,则代码会简单得多:

$duration_str = sprintf('%s:%02s', floor($duration_int / 60), $duration_int % 60);

对于超过 59 分钟的持续时间,结果仍然正确,但可读性较差(示例中为 31560 分钟)。

$duration_str = sprintf('%s:%02s:%02s',
 floor($duration_int / 3600), // hours
 floor($duration_int / 60) - floor($duration_int / 3600) * 60, // minutes
 $duration_int % 60); // seconds

The *printf functions provide formatting. In this case the leading zero.

The minutes line is the most complex part, since you have to calculate the hours (duration [s] / 3600 [s/h]), then round down to integer (floor()), then multiply with 60 to transform to minutes, then subtract that from the total number of minutes (duration [s] / 60 [s/m]).

If your durations are shorter than an hour, the code is much simpler:

$duration_str = sprintf('%s:%02s', floor($duration_int / 60), $duration_int % 60);

The result is still correct for a duration greater than 59 minutes, but just not as readable (31560 minutes in the example).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文