@@ -55,4 +55,47 @@ extension Lyrics {
5555
5656 self . init ( lines: lines, idTags: idTags)
5757 }
58+
59+ convenience init ? ( netEaseYrcContent content: String ) {
60+ var idTags : [ IDTagKey : String ] = [ : ]
61+ id3TagRegex. matches ( in: content) . forEach { match in
62+ if let key = match [ 1 ] ? . content. trimmingCharacters ( in: . whitespaces) ,
63+ let value = match [ 2 ] ? . content. trimmingCharacters ( in: . whitespaces) ,
64+ !key. isEmpty,
65+ !value. isEmpty {
66+ idTags [ . init( key) ] = value
67+ }
68+ }
69+
70+ let lines : [ LyricsLine ] = krcLineRegex. matches ( in: content) . map { match in
71+ let timeTagStr = match [ 1 ] !. content
72+ let timeTag = TimeInterval ( timeTagStr) ! / 1000
73+
74+ let durationStr = match [ 2 ] !. content
75+ let duration = TimeInterval ( durationStr) ! / 1000
76+
77+ var lineContent = " "
78+ var attachment = LyricsLine . Attachments. InlineTimeTag ( tags: [ . init( index: 0 , time: 0 ) ] , duration: duration)
79+ var dt = 0.0
80+ netEaseYrcInlineTagRegex. matches ( in: content, range: match [ 3 ] !. range) . forEach { m in
81+ let t1 = Int ( m [ 1 ] !. content) ! - Int( timeTagStr) !
82+ let t2 = Int ( m [ 2 ] !. content) !
83+ let t = TimeInterval ( t1 + t2) / 1000
84+ let fragment = m [ 3 ] !. content
85+ let prevCount = lineContent. count
86+ lineContent += fragment
87+ if lineContent. count > prevCount {
88+ attachment. tags. append ( . init( index: lineContent. count, time: t) )
89+ }
90+ }
91+
92+ let att = LyricsLine . Attachments ( attachments: [ . timetag: attachment] )
93+ return LyricsLine ( content: lineContent, position: timeTag, attachments: att)
94+ }
95+ guard !lines. isEmpty else {
96+ return nil
97+ }
98+
99+ self . init ( lines: lines, idTags: idTags)
100+ }
58101}
0 commit comments