Skip to content

Commit

Permalink
Merge pull request #90 from jitwxs/dev
Browse files Browse the repository at this point in the history
Release v4.3
  • Loading branch information
jitwxs authored May 15, 2022
2 parents 30b60a7 + 244ee92 commit ae06d6c
Show file tree
Hide file tree
Showing 13 changed files with 366 additions and 229 deletions.
4 changes: 2 additions & 2 deletions MusicLyricApp/Api/QQMusicApiV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ protected override LyricVo GetLyricVo0(string songId)

var lyricVo = new LyricVo();

lyricVo.SetLyric(resp.lyric);
lyricVo.SetTranslateLyric(resp.trans);
lyricVo.SetLyric(resp.Lyric);
lyricVo.SetTranslateLyric(resp.Trans);

return lyricVo;
}
Expand Down
2 changes: 1 addition & 1 deletion MusicLyricApp/Bean/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace MusicLyricApp.Bean
{
public static class Constants
{
public const string Version = "v4.2";
public const string Version = "v4.3";

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

Expand Down
181 changes: 150 additions & 31 deletions MusicLyricApp/Bean/MusicLyricsVO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using MusicLyricApp.Exception;
using MusicLyricApp.Utils;
Expand Down Expand Up @@ -196,76 +197,194 @@ public bool IsEmpty()
return string.IsNullOrEmpty(Lyric) && string.IsNullOrEmpty(TranslateLyric);
}
}

public class LyricTimestamp : IComparable
{
public long Minute { get; private set; }

public string MinuteS { get; private set; }

public long Second { get; private set; }

public string SecondS { get; private set; }

public long Millisecond { get; private set; }

public string MillisecondS { get; private set; }

public long TimeOffset { get;}

public LyricTimestamp(long millisecond)
{
TimeOffset = millisecond;

Millisecond = millisecond % 1000;

millisecond /= 1000;

Second = millisecond % 60;

Minute = millisecond / 60;

UpdateMinute(Minute);
UpdateSecond(Second);
UpdateMillisecond(Millisecond);
}

public LyricTimestamp(string timestamp)
{
if (string.IsNullOrWhiteSpace(timestamp) || timestamp[0] != '[' || timestamp[timestamp.Length - 1] != ']')
{
// 不支持的格式
}
else
{
timestamp = timestamp.Substring(1, timestamp.Length - 2);

var split = timestamp.Split(':');

Minute = GlobalUtils.toInt(split[0], 0);

split = split[1].Split('.');

Second = GlobalUtils.toInt(split[0], 0);

if (split.Length > 1)
{
Millisecond = GlobalUtils.toInt(split[1], 0);
}
}

UpdateMinute(Minute);
UpdateSecond(Second);
UpdateMillisecond(Millisecond);

TimeOffset = (Minute * 60 + Second) * 1000 + Millisecond;
}

public string ToString(OutputFormatEnum outputFormat)
{
if (outputFormat == OutputFormatEnum.LRC)
{
return "[" + MinuteS + ":" + SecondS + "." + MillisecondS + "]";
}
else
{
return "00:" + MinuteS + ":" + SecondS + "," + MillisecondS;
}
}

public int CompareTo(object input)
{
if (!(input is LyricTimestamp obj))
{
throw new MusicLyricException(ErrorMsg.SYSTEM_ERROR);
}

if (TimeOffset == obj.TimeOffset)
{
return 0;
}

if (TimeOffset == -1)
{
return -1;
}

if (obj.TimeOffset == -1)
{
return 1;
}

if (TimeOffset > obj.TimeOffset)
{
return 1;
}
else
{
return -1;
}
}

private void UpdateMinute(long value)
{
Minute = value;
MinuteS = value.ToString("00");
}

private void UpdateSecond(long value)
{
Second = value;
SecondS = value.ToString("00");
}

public void UpdateMillisecond(long value, int scale = 3)
{
var format = new StringBuilder().Insert(0, "0", scale).ToString();

Millisecond = value;
MillisecondS = Millisecond.ToString(format);
}
}

/// <summary>
/// 当行歌词信息
/// </summary>
public class LyricLineVo : IComparable
{
/// <summary>
/// 时间戳字符串
/// </summary>
public string Timestamp { get; set; }

/**
* 时间偏移量
*/
public long TimeOffset { get; set; }
public LyricTimestamp Timestamp { get; set; }

/// <summary>
/// 歌词正文
/// </summary>
public string Content { get; set; }

public LyricLineVo(string lyricLine)
public LyricLineVo(string lyricLine = "")
{
var index = lyricLine.IndexOf("]");
if (index == -1)
{
Timestamp = "";
TimeOffset = -1;
Timestamp = new LyricTimestamp("");
Content = lyricLine;
}
else
{
Timestamp = lyricLine.Substring(0, index + 1);
Timestamp = new LyricTimestamp(lyricLine.Substring(0, index + 1));
Content = lyricLine.Substring(index + 1);
TimeOffset = GlobalUtils.TimestampStrToLong(Timestamp);
}
}

public LyricLineVo()
{
}

public int CompareTo(object input)
{
if (!(input is LyricLineVo obj))
{
throw new MusicLyricException(ErrorMsg.SYSTEM_ERROR);
}

if (TimeOffset == -1 && obj.TimeOffset == -1)
{
return 0;
}

if (TimeOffset == -1)

return Timestamp.CompareTo(obj.Timestamp);
}

/// <summary>
/// 是否是无效的内容
/// </summary>
public bool IsIllegalContent()
{
if (string.IsNullOrWhiteSpace(Content))
{
return -1;
return true;
}

if (obj.TimeOffset == -1)
if ("//".Equals(Content))
{
return 1;
return true;
}

return (int) (TimeOffset - obj.TimeOffset);
return false;
}

public override string ToString()
{
return Timestamp + Content;
return Timestamp.ToString(OutputFormatEnum.LRC) + Content;
}
}

Expand Down
10 changes: 4 additions & 6 deletions MusicLyricApp/Bean/QQMusicBean.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,15 @@ public class LyricResult
{
public long Code { get; set; }

public string lyric { get; set; }
public string Lyric { get; set; }

public string trans { get; set; }
public string Trans { get; set; }

public LyricResult Decode()
{
var decode = Encoding.UTF8.GetString(Convert.FromBase64String(lyric));
lyric = Regex.Replace(decode, "\n", "\r\n");
Lyric = Encoding.UTF8.GetString(Convert.FromBase64String(Lyric));

decode = Encoding.UTF8.GetString(Convert.FromBase64String(trans));
trans = Regex.Replace(decode, "\n", "\r\n");
Trans = Encoding.UTF8.GetString(Convert.FromBase64String(Trans));

return this;
}
Expand Down
46 changes: 0 additions & 46 deletions MusicLyricApp/Utils/GlobalUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,52 +106,6 @@ private static bool CheckNum(string s)
{
return Regex.IsMatch(s, "^\\d+$");
}

public static long TimestampStrToLong(string timestamp)
{
// 不支持的格式
if (string.IsNullOrWhiteSpace(timestamp) || timestamp[0] != '[' || timestamp[timestamp.Length - 1] != ']')
{
return -1;
}

timestamp = timestamp.Substring(1, timestamp.Length - 2);

var split = timestamp.Split(':');

var min = toInt(split[0], 0);

split = split[1].Split('.');

var second = toInt(split[0], 0);

var ms = 0;

if (split.Length > 1)
{
ms = toInt(split[1], 0);
}

return (min * 60 + second) * 1000 + ms;
}

public static string TimestampLongToStr(long timestamp, string msScale)
{
if (timestamp < 0)
{
throw new MusicLyricException(ErrorMsg.SYSTEM_ERROR);
}

var ms = timestamp % 1000;

timestamp /= 1000;

var seconds = timestamp % 60;

var min = timestamp / 60;

return "[" + min.ToString("00") + ":" + seconds.ToString("00") + "." + ms.ToString(msScale) + "]";
}

/**
* 获取输出文件名
Expand Down
Loading

0 comments on commit ae06d6c

Please sign in to comment.