/// <summary> /// Set the duration of all entries. If a entry duration will overlap with the next entry and /// overlaps are not allowed then the new end time will be one millisecond less than the next start time. /// </summary> /// <param name="subRipTimeSpan">Time span to set all entries duration to.</param> /// <param name="doNotAllowOverlaps">Optional flag to indicate if end time should never overlap start time of next entry.</param> public void SetAbsoluteDuration(SubRipTimeSpan subRipTimeSpan, bool doNotAllowOverlaps = false) { for (var i = 0; i < Entries.Count; i++) { var entry = Entries[i]; if (doNotAllowOverlaps) { var nextEntry = Entries.GetNext(i); if (nextEntry == null) { // entry is last one entry.Duration.End = entry.Duration.Start.Add(subRipTimeSpan); } else { var ts = entry.Duration.Start.Add(subRipTimeSpan); if (ts > nextEntry.Duration.Start) { entry.Duration.End = nextEntry.Duration.Start.Subtract(new SubRipTimeSpan(0, 0, 0, 1)); } else { entry.Duration.End = ts; } } } else { entry.Duration.End = entry.Duration.Start.Add(subRipTimeSpan); } } }
/// <summary> /// Initializes a new instance of the <see cref="T:ByteDev.Subtitles.SubRip.SubRipDuration" /> class. /// </summary> /// <param name="start">Start time.</param> /// <param name="end">End time.</param> public SubRipDuration(SubRipTimeSpan start, SubRipTimeSpan end) { if (!IsValidStartAndEnd(start, end)) { throw new ArgumentException("Start time was after end time."); } Start = start; End = end; }
/// <summary> /// Sets the duration on any entries that are over the provided timespan. /// </summary> /// <param name="subRipTimeSpan">Time span to any entries duration to if over.</param> public void SetMaxDuration(SubRipTimeSpan subRipTimeSpan) { foreach (var entry in Entries) { if (entry.Duration.TotalActiveTime > subRipTimeSpan) { entry.Duration.End = entry.Duration.Start.Add(subRipTimeSpan); } } }
/// <summary> /// Initializes a new instance of the <see cref="T:ByteDev.Subtitles.SubRip.SubRipDuration" /> class. /// </summary> /// <param name="duration">SubRip duration in string format: "HH:MM:SS,MMM --> HH:MM:SS,MMM".</param> /// <exception cref="T:System.ArgumentException"><paramref name="duration" /> should be in format: "HH:MM:SS,MMM --> HH:MM:SS,MMM".</exception> public SubRipDuration(string duration) { if (!IsValidDuration(duration)) { throw new ArgumentException($"Duration string: '{duration}' is in invalid format."); } var start = new SubRipTimeSpan(duration.Substring(0, SubRipTimeSpan.ValidStringLength)); var end = new SubRipTimeSpan(duration.Substring(SubRipTimeSpan.ValidStringLength + TimeDelimiter.Length)); if (!IsValidStartAndEnd(start, end)) { throw new ArgumentException("Start time was after end time."); } Start = start; End = end; }
private static bool IsValidStartAndEnd(SubRipTimeSpan start, SubRipTimeSpan end) { return(start.TotalMilliseconds <= end.TotalMilliseconds); }