Skip to content

Commit c198677

Browse files
committed
refactor(Exporter): extract LRC time formatting to utils/time
1 parent b8981e2 commit c198677

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

src/core/Exporter.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import { type SubtitleItem } from '@/types/subtitle'
1414
import { type ExportFormat } from '@/types/subtitle'
15-
import { _decomposeWithRemainder } from '@/utils/time'
15+
import { _decomposeWithRemainder, formatTimeLrc } from '@/utils/time'
1616

1717
// ─── 模块级常量(避免每调用重建)───────────────────────────────
1818
// ASS escape sequences — MUST process backslash FIRST to avoid double-escaping
@@ -183,10 +183,7 @@ function formatLRC(subs: SubtitleItem[]): string {
183183
184184
`
185185
const content = subs.map(sub => {
186-
const min = Math.floor(sub.startTime / 60)
187-
const sec = Math.floor(sub.startTime % 60)
188-
const ms = Math.floor((sub.startTime % 1) * 100)
189-
return `[${pad2(min)}:${pad2(sec)}.${pad2(ms)}]${sub.text}`
186+
return `${formatTimeLrc(sub.startTime)}${sub.text}`
190187
}).join('\n\n')
191188

192189
return `${header}${content}`

src/utils/time.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,17 @@ export function formatTimePrecise(seconds: number): string {
115115
return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}.${ms.toString().padStart(3, '0')}`
116116
}
117117

118+
/**
119+
* Format seconds to LRC format ([MM:SS.xx])
120+
* Used for .lrc lyric files
121+
*/
122+
export function formatTimeLrc(seconds: number): string {
123+
const mins = Math.floor(seconds / MINUTES_IN_SECONDS)
124+
const secs = Math.floor(seconds % MINUTES_IN_SECONDS)
125+
const cs = Math.floor((seconds % 1) * 100)
126+
return `[${_pad2(mins)}:${_pad2(secs)}.${_pad2(cs)}]`
127+
}
128+
118129
/**
119130
* Format frame number with thousand separators
120131
*/

0 commit comments

Comments
 (0)