using System.Globalization;
namespace Titano.Core.Video;
/// An exact frame rate such as 24000/1001.
public readonly record struct Rational(int Numerator, int Denominator)
{
public double Value => (double)Numerator / Denominator;
public static Rational Fps23976 { get; } = new(24000, 1001);
public static Rational Fps2997 { get; } = new(30000, 1001);
public static Rational Fps5994 { get; } = new(60000, 1001);
public static Rational FromFps(double fps)
{
if (Math.Abs(fps - 23.976) < 0.01) return Fps23976;
if (Math.Abs(fps - 29.97) < 0.01) return Fps2997;
if (Math.Abs(fps - 59.94) < 0.01) return Fps5994;
if (Math.Abs(fps - Math.Round(fps)) < 1e-6) return new((int)Math.Round(fps), 1);
return new((int)Math.Round(fps * 1000), 1000);
}
/// Duration of one frame in 90 kHz ticks (MP4 timescale used by Titano).
public long FrameTicks90k => (long)Math.Round(90000.0 * Denominator / Numerator);
public override string ToString() => Denominator == 1
? Numerator.ToString(CultureInfo.InvariantCulture)
: Value.ToString("0.###", CultureInfo.InvariantCulture);
}