0%

JS 时间转换为字幕时间,生成字幕

记一下格式化时间算法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
function srtTimestamp(params) {
let milliseconds = params;
let seconds = Math.floor(milliseconds / 1000);
let minutes = Math.floor(seconds / 60);
let hours = Math.floor(minutes / 60);
milliseconds = milliseconds % 1000;
seconds = seconds % 60;
minutes = minutes % 60;
return (
(hours < 10 ? '0' : '') +
hours +
':' +
(minutes < 10 ? '0' : '') +
minutes +
':' +
(seconds < 10 ? '0' : '') +
seconds +
',' +
(milliseconds < 100 ? '0' : '') +
(milliseconds < 10 ? '0' : '') +
milliseconds
);
}

function inputToSRT(params, index) {
return (
index +
1 +
'\r\n' +
srtTimestamp(params.startTime) +
' --> ' +
srtTimestamp(params.endTime) +
'\r\n' +
params.text +
'\r\n\r\n'
);
}