我有一段视频的持续时间634.567
!我已使用此代码成功将其转换为HH:MM:SS:ms格式:
var pad = function(num, size) { return ('000' + num).slice(size * -1); },
time = parseFloat(timeInSeconds).toFixed(3),
hours = Math.floor(time / 60 / 60),
minutes = Math.floor(time / 60) % 60,
seconds = Math.floor(time - minutes * 60),
milliseconds = time.slice(-3);
return pad(hours, 2) + ':' + pad(minutes, 2) + ':' + pad(seconds, 2) + ',' + pad(milliseconds, 3);
这会返回"00:10:34,567"
现在,我想把这个时间分成5秒的时间间隔。所以,它变成了一个看起来像这样的JS数组
<00> 00.00.05,000 | 00.00.10,000 | 00.00.15,000 |直到数组中的最后一行是00:10:34,567(所以它们都是相等的部分)。如何将时间分成更小的时间然后将其推入数组?
答案 0 :(得分:1)
为此,最好使用原始持续时间timeInSeconds
,以5秒的步长迭代并转换每个步骤的相应值,例如:
var pad = function(num, size) { return ('000' + num).slice(size * -1); };
function conv(t){
var time = parseFloat(t).toFixed(3),
hours = Math.floor(time / 60 / 60),
minutes = Math.floor(time / 60) % 60,
seconds = Math.floor(time - minutes * 60),
milliseconds = time.slice(-3);
return pad(hours, 2) + ':' + pad(minutes, 2) + ':' + pad(seconds, 2) + ',' + pad(milliseconds, 3);
}
var timeInSeconds = 634.567;
//required step
var delta = 5;
/*
the condition (t < timeInSeconds + delta) in combination
with Math.min(t, timeInSeconds) ensures that the for
loop will consider the value timeInSeconds even if it
is not divisible by delta
*/
var arr = [];
for(var t = 0; t < timeInSeconds + delta; t += delta){
arr.push(conv(Math.min(t, timeInSeconds)));
}
console.log(arr);
例如,对于timeInSeconds = 11.0
,你会得到:
[ '00:00:00,000', '00:00:05,000', '00:00:10,000', '00:00:11,000' ]