public static TimeSpan LTCTimecodeToTimeSpan(this string timeCode, RationalNumber rate) { if (!validateLTC.IsMatch(timeCode)) { throw new FormatException("Bad LTC timecode format"); } int frames = Int32.Parse(timeCode.Substring(0, 2)); int seconds = Int32.Parse(timeCode.Substring(2, 2)); int minutes = Int32.Parse(timeCode.Substring(4, 2)); int hours = Int32.Parse(timeCode.Substring(6)); int days = hours / 24; hours = hours % 24; if ((hours > 24) || (minutes >= 60) || (seconds >= 60) || (frames >= (int)(rate.Num / rate.Den))) { throw new FormatException("LTC Timecode out of range"); } return(new TimeSpan(days, hours, minutes, seconds, frames * (int)rate.Den * 1000 / (int)rate.Num)); }
public static string ToSmpteTimecodeString(this TimeSpan t, RationalNumber rate) { if (rate.IsZero) { return(t.ToString()); } var minus = t < TimeSpan.Zero; var value = minus ? -t : t; var days = value.Days; var hours = value.Hours; var minutes = value.Minutes; var seconds = value.Seconds; var frames = ((value.Ticks % TimeSpan.TicksPerSecond) * rate.Num + (rate.Den * TimeSpan.TicksPerSecond / 2)) / (rate.Den * TimeSpan.TicksPerSecond); // rounding if (days > 0) { return($"{(minus ? "-" : "")}{days}:{hours:D2}:{minutes:D2}:{seconds:D2}:{frames:D2}"); } else { return($"{(minus ? "-" : "")}{hours:D2}:{minutes:D2}:{seconds:D2}:{frames:D2}"); } }
public static bool IsValidSMPTETimecode(this string timeCode, RationalNumber rate) { if (rate.IsZero) { TimeSpan t; return(TimeSpan.TryParse(timeCode, out t)); } else { string[] times = timeCode.Split(':'); if (times.Length < 4 || times.Length > 5) { return(false); } int index = -1; int days = 0; int hours, minutes, seconds, frames; if (!((times.Length == 5 && int.TryParse(times[++index], out days) || times.Length == 4) && int.TryParse(times[++index], out hours) && int.TryParse(times[++index], out minutes) && int.TryParse(times[++index], out seconds) && int.TryParse(times[++index], out frames))) { return(false); } if ((days != 0 && hours < 0) || (Math.Abs(hours) >= 24) || minutes >= 60 || minutes < 0 || seconds >= 60 || seconds < 0 || frames >= (int)(rate.Num / rate.Den) || frames < 0) { return(false); } } return(true); }
public static VideoFormatDescription Match(Size imageSize, RationalNumber frameRate, RationalNumber sar, bool interlaced) { var result = Descriptions.Values.FirstOrDefault((v) => v.ImageSize.Equals(imageSize) && (v.FrameRate.Equals(frameRate) || frameRate.Equals(RationalNumber.Zero)) && (v.SAR.Equals(sar) || sar.Equals(RationalNumber.Zero)) && v.Interlaced == interlaced); return(result != null ? result : new VideoFormatDescription(imageSize, frameRate, sar, interlaced)); }
private VideoFormatDescription(TVideoFormat format) { Format = format; switch (format) { case TVideoFormat.Other: case TVideoFormat.PAL_FHA: Interlaced = true; ImageSize = new Size(720, 576); FrameRate = new RationalNumber(25, 1); SAR = new RationalNumber(64, 45); IsWideScreen = true; break; case TVideoFormat.PAL: Interlaced = true; ImageSize = new Size(720, 576); FrameRate = new RationalNumber(25, 1); SAR = new RationalNumber(16, 15); IsWideScreen = false; break; case TVideoFormat.PAL_FHA_P: Interlaced = false; ImageSize = new Size(720, 576); FrameRate = new RationalNumber(25, 1); SAR = new RationalNumber(64, 45); IsWideScreen = true; break; case TVideoFormat.PAL_P: Interlaced = false; ImageSize = new Size(720, 576); FrameRate = new RationalNumber(25, 1); SAR = new RationalNumber(16, 15); IsWideScreen = false; break; case TVideoFormat.NTSC_FHA: Interlaced = true; ImageSize = new Size(720, 480); FrameRate = new RationalNumber(30000, 1001); SAR = new RationalNumber(32, 27); IsWideScreen = true; break; case TVideoFormat.NTSC: Interlaced = true; ImageSize = new Size(720, 480); FrameRate = new RationalNumber(30000, 1001); SAR = new RationalNumber(8, 9); IsWideScreen = false; break; case TVideoFormat.HD1080i5000: Interlaced = true; ImageSize = new Size(1920, 1080); FrameRate = new RationalNumber(25, 1); SAR = new RationalNumber(1, 1); IsWideScreen = true; break; case TVideoFormat.HD1080i5994: Interlaced = true; ImageSize = new Size(1920, 1080); FrameRate = new RationalNumber(30000, 1001); SAR = new RationalNumber(1, 1); IsWideScreen = true; break; case TVideoFormat.HD1080i6000: Interlaced = true; ImageSize = new Size(1920, 1080); FrameRate = new RationalNumber(30, 1); SAR = new RationalNumber(1, 1); IsWideScreen = true; break; case TVideoFormat.HD1080p2398: Interlaced = false; ImageSize = new Size(1920, 1080); FrameRate = new RationalNumber(2398, 100); SAR = new RationalNumber(1, 1); IsWideScreen = true; break; case TVideoFormat.HD1080p2400: Interlaced = false; ImageSize = new Size(1920, 1080); FrameRate = new RationalNumber(24, 1); SAR = new RationalNumber(1, 1); IsWideScreen = true; break; case TVideoFormat.HD1080p2500: Interlaced = false; ImageSize = new Size(1920, 1080); FrameRate = new RationalNumber(25, 1); SAR = new RationalNumber(1, 1); IsWideScreen = true; break; case TVideoFormat.HD1080p2997: Interlaced = false; ImageSize = new Size(1920, 1080); FrameRate = new RationalNumber(30000, 1001); SAR = new RationalNumber(1, 1); IsWideScreen = true; break; case TVideoFormat.HD1080p3000: Interlaced = false; ImageSize = new Size(1920, 1080); FrameRate = new RationalNumber(30, 1); SAR = new RationalNumber(1, 1); IsWideScreen = true; break; case TVideoFormat.HD1080p5000: Interlaced = false; ImageSize = new Size(1920, 1080); FrameRate = new RationalNumber(50, 1); SAR = new RationalNumber(1, 1); IsWideScreen = true; break; case TVideoFormat.HD1080p5994: Interlaced = false; ImageSize = new Size(1920, 1080); FrameRate = new RationalNumber(5994, 100); SAR = new RationalNumber(1, 1); IsWideScreen = true; break; case TVideoFormat.HD1080p6000: Interlaced = false; ImageSize = new Size(1920, 1080); FrameRate = new RationalNumber(60, 1); SAR = new RationalNumber(1, 1); IsWideScreen = true; break; case TVideoFormat.HD2160p2398: Interlaced = false; ImageSize = new Size(3840, 2160); FrameRate = new RationalNumber(2398, 100); SAR = new RationalNumber(1, 1); IsWideScreen = true; break; case TVideoFormat.HD2160p2400: Interlaced = false; ImageSize = new Size(3840, 2160); FrameRate = new RationalNumber(24, 1); SAR = new RationalNumber(1, 1); IsWideScreen = true; break; case TVideoFormat.HD2160p2500: Interlaced = false; ImageSize = new Size(3840, 2160); FrameRate = new RationalNumber(25, 1); SAR = new RationalNumber(1, 1); IsWideScreen = true; break; case TVideoFormat.HD2160p2997: Interlaced = false; ImageSize = new Size(3840, 2160); FrameRate = new RationalNumber(30000, 1001); SAR = new RationalNumber(1, 1); IsWideScreen = true; break; case TVideoFormat.HD2160p3000: Interlaced = false; ImageSize = new Size(3840, 2160); FrameRate = new RationalNumber(30, 1); SAR = new RationalNumber(1, 1); IsWideScreen = true; break; case TVideoFormat.HD2160p5000: Interlaced = false; ImageSize = new Size(3840, 2160); FrameRate = new RationalNumber(50, 1); SAR = new RationalNumber(1, 1); IsWideScreen = true; break; case TVideoFormat.HD2160p5994: Interlaced = false; ImageSize = new Size(3840, 2160); FrameRate = new RationalNumber(5994, 100); SAR = new RationalNumber(1, 1); IsWideScreen = true; break; case TVideoFormat.HD2160p6000: Interlaced = false; ImageSize = new Size(3840, 2160); FrameRate = new RationalNumber(60, 1); SAR = new RationalNumber(1, 1); IsWideScreen = true; break; case TVideoFormat.HD720p2500: Interlaced = false; ImageSize = new Size(1440, 720); FrameRate = new RationalNumber(25, 1); SAR = new RationalNumber(1, 1); IsWideScreen = true; break; case TVideoFormat.HD720p5000: Interlaced = false; ImageSize = new Size(1440, 720); FrameRate = new RationalNumber(50, 1); SAR = new RationalNumber(1, 1); IsWideScreen = true; break; case TVideoFormat.HD720p5994: Interlaced = false; ImageSize = new Size(1440, 720); FrameRate = new RationalNumber(5994, 100); SAR = new RationalNumber(1, 1); IsWideScreen = true; break; case TVideoFormat.HD720p6000: Interlaced = false; ImageSize = new Size(1440, 720); FrameRate = new RationalNumber(60, 1); SAR = new RationalNumber(1, 1); IsWideScreen = true; break; } }
public static VideoFormatDescription Match(Size imageSize, RationalNumber frameRate, RationalNumber sar, bool interlaced) { var result = Descriptions.Values.FirstOrDefault(v => v.ImageSize.Equals(imageSize) && (v.FrameRate.Equals(frameRate) || frameRate.Equals(RationalNumber.Zero)) && (v.SAR.Equals(sar) || sar.Equals(RationalNumber.Zero)) && v.Interlaced == interlaced); if (result != null) { return(result); } if (imageSize.Height == Descriptions[TVideoFormat.PAL].ImageSize.Height && frameRate == Descriptions[TVideoFormat.PAL].FrameRate && imageSize.Width == 768) { return(interlaced ? Descriptions[TVideoFormat.PAL] : Descriptions[TVideoFormat.PAL_P]); } if (imageSize.Height == Descriptions[TVideoFormat.PAL_FHA].ImageSize.Height && frameRate == Descriptions[TVideoFormat.PAL_FHA].FrameRate && (imageSize.Width == 1024 || imageSize.Width == 1050)) { return(interlaced ? Descriptions[TVideoFormat.PAL_FHA] : Descriptions[TVideoFormat.PAL_FHA_P]); } return(new VideoFormatDescription(imageSize, frameRate, sar, interlaced)); }