@@ -3,7 +3,7 @@ import { songLyric, songLyricTTML } from "@/api/song";
33import { type SongLyric } from "@/types/lyric" ;
44import { type LyricLine , parseLrc , parseTTML , parseYrc } from "@applemusic-like-lyrics/lyric" ;
55import { isElectron } from "@/utils/env" ;
6- import { isEmpty } from "lodash-es" ;
6+ import { isEmpty , max } from "lodash-es" ;
77import { useCacheManager } from "@/core/resource/CacheManager" ;
88
99class LyricManager {
@@ -101,13 +101,14 @@ class LyricManager {
101101
102102 /**
103103 * 对齐本地歌词
104- * @param lyrics 本地歌词数据
105- * @param otherLyrics 其他歌词数据
104+ * @param lyricData 本地歌词数据
106105 * @returns 对齐后的本地歌词数据
107106 */
108107 private alignLocalLyrics ( lyricData : SongLyric ) : SongLyric {
109108 // 同一时间的两/三行分别作为主句、翻译、音译
110109 const toTime = ( line : LyricLine ) => Number ( line ?. startTime ?? line ?. words ?. [ 0 ] ?. startTime ?? 0 ) ;
110+ // 获取结束时间
111+ const toEndTime = ( line : LyricLine ) => Number ( line ?. endTime ?? line ?. words ?. [ line ?. words ?. length - 1 ] ?. endTime ?? 0 ) ;
111112 // 取内容
112113 const toText = ( line : LyricLine ) => String ( line ?. words ?. [ 0 ] ?. word || "" ) . trim ( ) ;
113114 const lrc = lyricData . lrcData || [ ] ;
@@ -124,10 +125,16 @@ class LyricManager {
124125 // 组装:第 1 行主句;第 2 行翻译;第 3 行音译;不调整时长
125126 const aligned = groups . map ( ( group ) => {
126127 const base = { ...group [ 0 ] } as LyricLine ;
127- const tran = group [ 1 ] ? toText ( group [ 1 ] ) : "" ;
128- const roma = group [ 2 ] ? toText ( group [ 2 ] ) : "" ;
129- if ( ! base . translatedLyric ) base . translatedLyric = tran ;
130- if ( ! base . romanLyric ) base . romanLyric = roma ;
128+ const tran = group [ 1 ] ;
129+ const roma = group [ 2 ] ;
130+ if ( ! base . translatedLyric && tran ) {
131+ base . translatedLyric = toText ( tran ) ;
132+ base . endTime = max ( [ toEndTime ( base ) , toEndTime ( tran ) ] ) ;
133+ }
134+ if ( ! base . romanLyric && roma ) {
135+ base . romanLyric = toText ( roma ) ;
136+ base . endTime = max ( [ toEndTime ( base ) , toEndTime ( roma ) ] ) ;
137+ }
131138 return base ;
132139 } ) ;
133140 return { lrcData : aligned , yrcData : lyricData . yrcData } ;
0 commit comments