Skip to content

Commit ae06d6c

Browse files
authored
Merge pull request #90 from jitwxs/dev
Release v4.3
2 parents 30b60a7 + 244ee92 commit ae06d6c

File tree

13 files changed

+366
-229
lines changed

13 files changed

+366
-229
lines changed

MusicLyricApp/Api/QQMusicApiV2.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ protected override LyricVo GetLyricVo0(string songId)
7272

7373
var lyricVo = new LyricVo();
7474

75-
lyricVo.SetLyric(resp.lyric);
76-
lyricVo.SetTranslateLyric(resp.trans);
75+
lyricVo.SetLyric(resp.Lyric);
76+
lyricVo.SetTranslateLyric(resp.Trans);
7777

7878
return lyricVo;
7979
}

MusicLyricApp/Bean/Constants.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace MusicLyricApp.Bean
44
{
55
public static class Constants
66
{
7-
public const string Version = "v4.2";
7+
public const string Version = "v4.3";
88

99
public static readonly string SettingPath = Environment.CurrentDirectory + "\\MusicLyricAppSetting.json";
1010

MusicLyricApp/Bean/MusicLyricsVO.cs

Lines changed: 150 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.ComponentModel;
44
using System.Linq;
5+
using System.Text;
56
using System.Web;
67
using MusicLyricApp.Exception;
78
using MusicLyricApp.Utils;
@@ -196,76 +197,194 @@ public bool IsEmpty()
196197
return string.IsNullOrEmpty(Lyric) && string.IsNullOrEmpty(TranslateLyric);
197198
}
198199
}
200+
201+
public class LyricTimestamp : IComparable
202+
{
203+
public long Minute { get; private set; }
204+
205+
public string MinuteS { get; private set; }
206+
207+
public long Second { get; private set; }
208+
209+
public string SecondS { get; private set; }
210+
211+
public long Millisecond { get; private set; }
212+
213+
public string MillisecondS { get; private set; }
214+
215+
public long TimeOffset { get;}
216+
217+
public LyricTimestamp(long millisecond)
218+
{
219+
TimeOffset = millisecond;
220+
221+
Millisecond = millisecond % 1000;
222+
223+
millisecond /= 1000;
224+
225+
Second = millisecond % 60;
226+
227+
Minute = millisecond / 60;
228+
229+
UpdateMinute(Minute);
230+
UpdateSecond(Second);
231+
UpdateMillisecond(Millisecond);
232+
}
233+
234+
public LyricTimestamp(string timestamp)
235+
{
236+
if (string.IsNullOrWhiteSpace(timestamp) || timestamp[0] != '[' || timestamp[timestamp.Length - 1] != ']')
237+
{
238+
// 不支持的格式
239+
}
240+
else
241+
{
242+
timestamp = timestamp.Substring(1, timestamp.Length - 2);
243+
244+
var split = timestamp.Split(':');
245+
246+
Minute = GlobalUtils.toInt(split[0], 0);
247+
248+
split = split[1].Split('.');
249+
250+
Second = GlobalUtils.toInt(split[0], 0);
251+
252+
if (split.Length > 1)
253+
{
254+
Millisecond = GlobalUtils.toInt(split[1], 0);
255+
}
256+
}
257+
258+
UpdateMinute(Minute);
259+
UpdateSecond(Second);
260+
UpdateMillisecond(Millisecond);
261+
262+
TimeOffset = (Minute * 60 + Second) * 1000 + Millisecond;
263+
}
264+
265+
public string ToString(OutputFormatEnum outputFormat)
266+
{
267+
if (outputFormat == OutputFormatEnum.LRC)
268+
{
269+
return "[" + MinuteS + ":" + SecondS + "." + MillisecondS + "]";
270+
}
271+
else
272+
{
273+
return "00:" + MinuteS + ":" + SecondS + "," + MillisecondS;
274+
}
275+
}
276+
277+
public int CompareTo(object input)
278+
{
279+
if (!(input is LyricTimestamp obj))
280+
{
281+
throw new MusicLyricException(ErrorMsg.SYSTEM_ERROR);
282+
}
283+
284+
if (TimeOffset == obj.TimeOffset)
285+
{
286+
return 0;
287+
}
288+
289+
if (TimeOffset == -1)
290+
{
291+
return -1;
292+
}
293+
294+
if (obj.TimeOffset == -1)
295+
{
296+
return 1;
297+
}
298+
299+
if (TimeOffset > obj.TimeOffset)
300+
{
301+
return 1;
302+
}
303+
else
304+
{
305+
return -1;
306+
}
307+
}
308+
309+
private void UpdateMinute(long value)
310+
{
311+
Minute = value;
312+
MinuteS = value.ToString("00");
313+
}
314+
315+
private void UpdateSecond(long value)
316+
{
317+
Second = value;
318+
SecondS = value.ToString("00");
319+
}
320+
321+
public void UpdateMillisecond(long value, int scale = 3)
322+
{
323+
var format = new StringBuilder().Insert(0, "0", scale).ToString();
324+
325+
Millisecond = value;
326+
MillisecondS = Millisecond.ToString(format);
327+
}
328+
}
199329

200330
/// <summary>
201331
/// 当行歌词信息
202332
/// </summary>
203333
public class LyricLineVo : IComparable
204334
{
205-
/// <summary>
206-
/// 时间戳字符串
207-
/// </summary>
208-
public string Timestamp { get; set; }
209-
210-
/**
211-
* 时间偏移量
212-
*/
213-
public long TimeOffset { get; set; }
335+
public LyricTimestamp Timestamp { get; set; }
214336

215337
/// <summary>
216338
/// 歌词正文
217339
/// </summary>
218340
public string Content { get; set; }
219341

220-
public LyricLineVo(string lyricLine)
342+
public LyricLineVo(string lyricLine = "")
221343
{
222344
var index = lyricLine.IndexOf("]");
223345
if (index == -1)
224346
{
225-
Timestamp = "";
226-
TimeOffset = -1;
347+
Timestamp = new LyricTimestamp("");
227348
Content = lyricLine;
228349
}
229350
else
230351
{
231-
Timestamp = lyricLine.Substring(0, index + 1);
352+
Timestamp = new LyricTimestamp(lyricLine.Substring(0, index + 1));
232353
Content = lyricLine.Substring(index + 1);
233-
TimeOffset = GlobalUtils.TimestampStrToLong(Timestamp);
234354
}
235355
}
236356

237-
public LyricLineVo()
238-
{
239-
}
240-
241357
public int CompareTo(object input)
242358
{
243359
if (!(input is LyricLineVo obj))
244360
{
245361
throw new MusicLyricException(ErrorMsg.SYSTEM_ERROR);
246362
}
247-
248-
if (TimeOffset == -1 && obj.TimeOffset == -1)
249-
{
250-
return 0;
251-
}
252-
253-
if (TimeOffset == -1)
363+
364+
return Timestamp.CompareTo(obj.Timestamp);
365+
}
366+
367+
/// <summary>
368+
/// 是否是无效的内容
369+
/// </summary>
370+
public bool IsIllegalContent()
371+
{
372+
if (string.IsNullOrWhiteSpace(Content))
254373
{
255-
return -1;
374+
return true;
256375
}
257376

258-
if (obj.TimeOffset == -1)
377+
if ("//".Equals(Content))
259378
{
260-
return 1;
379+
return true;
261380
}
262381

263-
return (int) (TimeOffset - obj.TimeOffset);
382+
return false;
264383
}
265-
384+
266385
public override string ToString()
267386
{
268-
return Timestamp + Content;
387+
return Timestamp.ToString(OutputFormatEnum.LRC) + Content;
269388
}
270389
}
271390

MusicLyricApp/Bean/QQMusicBean.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,15 @@ public class LyricResult
2626
{
2727
public long Code { get; set; }
2828

29-
public string lyric { get; set; }
29+
public string Lyric { get; set; }
3030

31-
public string trans { get; set; }
31+
public string Trans { get; set; }
3232

3333
public LyricResult Decode()
3434
{
35-
var decode = Encoding.UTF8.GetString(Convert.FromBase64String(lyric));
36-
lyric = Regex.Replace(decode, "\n", "\r\n");
35+
Lyric = Encoding.UTF8.GetString(Convert.FromBase64String(Lyric));
3736

38-
decode = Encoding.UTF8.GetString(Convert.FromBase64String(trans));
39-
trans = Regex.Replace(decode, "\n", "\r\n");
37+
Trans = Encoding.UTF8.GetString(Convert.FromBase64String(Trans));
4038

4139
return this;
4240
}

MusicLyricApp/Utils/GlobalUtils.cs

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -106,52 +106,6 @@ private static bool CheckNum(string s)
106106
{
107107
return Regex.IsMatch(s, "^\\d+$");
108108
}
109-
110-
public static long TimestampStrToLong(string timestamp)
111-
{
112-
// 不支持的格式
113-
if (string.IsNullOrWhiteSpace(timestamp) || timestamp[0] != '[' || timestamp[timestamp.Length - 1] != ']')
114-
{
115-
return -1;
116-
}
117-
118-
timestamp = timestamp.Substring(1, timestamp.Length - 2);
119-
120-
var split = timestamp.Split(':');
121-
122-
var min = toInt(split[0], 0);
123-
124-
split = split[1].Split('.');
125-
126-
var second = toInt(split[0], 0);
127-
128-
var ms = 0;
129-
130-
if (split.Length > 1)
131-
{
132-
ms = toInt(split[1], 0);
133-
}
134-
135-
return (min * 60 + second) * 1000 + ms;
136-
}
137-
138-
public static string TimestampLongToStr(long timestamp, string msScale)
139-
{
140-
if (timestamp < 0)
141-
{
142-
throw new MusicLyricException(ErrorMsg.SYSTEM_ERROR);
143-
}
144-
145-
var ms = timestamp % 1000;
146-
147-
timestamp /= 1000;
148-
149-
var seconds = timestamp % 60;
150-
151-
var min = timestamp / 60;
152-
153-
return "[" + min.ToString("00") + ":" + seconds.ToString("00") + "." + ms.ToString(msScale) + "]";
154-
}
155109

156110
/**
157111
* 获取输出文件名

0 commit comments

Comments
 (0)