Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't format LRC timestamp with DDdHH:MM:SS #297

Merged
merged 1 commit into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ATL.unit-test/Misc/UtilsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public void Utils_FormatTime()
Assert.AreEqual("01:01:00.0", Utils.EncodeTimecode_ms(60 * 60 * 1000 + 60 * 1000));
// Display d, h, m, s and ms
Assert.AreEqual("2d 01:01:00.0", Utils.EncodeTimecode_ms(48 * 60 * 60 * 1000 + 60 * 60 * 1000 + 60 * 1000));
// Display m, s and ms for very long durations in MM:SS.UUUU format
Assert.AreEqual("2941:01.0", Utils.EncodeTimecode_ms(48 * 60 * 60 * 1000 + 60 * 60 * 1000 + 60 * 1000 + 1000, true));
}

[TestMethod]
Expand Down
2 changes: 1 addition & 1 deletion ATL/Entities/LyricsInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ public string FormatSynchToLRC()
// Lyrics
foreach (var line in SynchronizedLyrics)
{
sb.Append('[').Append(Utils.EncodeTimecode_ms(line.TimestampMs)).Append(']').Append(line.Text).Append('\n');
sb.Append('[').Append(Utils.EncodeTimecode_ms(line.TimestampMs, true)).Append(']').Append(line.Text).Append('\n');
}

return sb.ToString();
Expand Down
34 changes: 31 additions & 3 deletions ATL/Utils/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,26 @@ public static string ProtectValue(string value)
/// <summary>
/// Format the given duration using the following format
/// DDdHH:MM:SS.UUUU
/// OR
/// MM:SS.UUUU
///
/// Where
/// DD is the number of days, if applicable (i.e. durations of less than 1 day won't display the "DDd" part)
/// HH is the number of hours, if applicable (i.e. durations of less than 1 hour won't display the "HH:" part)
/// MM is the number of minutes
/// MM is the number of minutes, when using MMSS format, this will extend beyond two digits if necessary
/// SS is the number of seconds
/// UUUU is the number of milliseconds
/// </summary>
/// <param name="milliseconds">Duration to format (in milliseconds)</param>
/// <param name="useMmSsFormat">Format in MM:SS.UUUU format. Default is false</param>
/// <returns>Formatted duration according to the abovementioned convention</returns>
public static string EncodeTimecode_ms(long milliseconds)
public static string EncodeTimecode_ms(long milliseconds, bool useMmSsFormat = false)
{
long seconds = Convert.ToInt64(Math.Floor(milliseconds / 1000.00));

var encodedString = useMmSsFormat ? EncodeMmSsTimecode_s(seconds) : EncodeTimecode_s(seconds);

return EncodeTimecode_s(seconds) + "." + (milliseconds - seconds * 1000);
return encodedString + "." + (milliseconds - seconds * 1000);
}

/// <summary>
Expand Down Expand Up @@ -93,6 +98,29 @@ public static string EncodeTimecode_s(long seconds)
if (h > 0) return hStr + ":" + mStr + ":" + sStr;
return mStr + ":" + sStr;
}

/// <summary>
/// Format the given duration using the following format
/// MM:SS
///
/// Where
/// MM is the number of minutes, this will extend beyond two digits if necessary
/// SS is the number of seconds
/// </summary>
/// <param name="seconds">Duration to format (in seconds)</param>
/// <returns>Formatted duration according to the abovementioned convention</returns>
public static string EncodeMmSsTimecode_s(long seconds)
{
var m = Convert.ToInt64(Math.Floor(seconds / 60.00));
var s = seconds - 60 * m;

var mStr = m.ToString();
if (1 == mStr.Length) mStr = "0" + mStr;
var sStr = s.ToString();
if (1 == sStr.Length) sStr = "0" + sStr;

return mStr + ":" + sStr;
}

/// <summary>
/// Convert the duration of the given timecode to milliseconds
Expand Down
Loading