Advertisement
ivandrofly

SubtitleEdit: Subtitle.cs:AdjustDisplayTimeUsingMilliseconds

Jul 21st, 2023
1,269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.16 KB | None | 0 0
  1. private void AdjustDisplayTimeUsingMilliseconds(int idx, double ms, List<double> shotChanges = null, bool enforceDurationLimits = true)
  2. {
  3.     var p = Paragraphs[idx];
  4.     var nextStartTimeInMs = GetNextStartTimeInMs(idx);
  5.  
  6.     var newEndTimeInMs = p.EndTime.TotalMilliseconds + ms;    
  7.     if (enforceDurationLimits)
  8.     {
  9.         newEndTimeInMs = FixShortDuration(p, newEndTimeInMs);
  10.     }
  11.  
  12.     // handle overlap with next
  13.     newEndTimeInMs = HandleOverlapWithNext(newEndTimeInMs, nextStartTimeInMs);
  14.  
  15.     // handle shot change if supplied -- keep earliest time
  16.     if (shotChanges != null)
  17.     {
  18.         newEndTimeInMs = HandleShotChange(shotChanges, newEndTimeInMs, p);
  19.     }
  20.  
  21.     // max duration
  22.     if (enforceDurationLimits)
  23.     {
  24.         newEndTimeInMs = HandleMaximumDuration(p, newEndTimeInMs);
  25.     }
  26.  
  27.     // do not adjust wrong way
  28.     if (!(ms > 0 && newEndTimeInMs < p.EndTime.TotalMilliseconds) && !(ms < 0 && newEndTimeInMs > p.EndTime.TotalMilliseconds))
  29.     {
  30.         p.EndTime.TotalMilliseconds = newEndTimeInMs;
  31.     }
  32. }
  33.  
  34. private double GetNextStartTimeInMs(int idx) => (idx + 1) < Paragraphs.Count ? Paragraphs[idx + 1].StartTime.TotalMilliseconds : double.MaxValue;
  35.  
  36. private double FixShortDuration(Paragraph p, double endTime)
  37. {
  38.     var minDur = Math.Max(Configuration.Settings.General.SubtitleMinimumDisplayMilliseconds, 100);
  39.     return Math.Max(p.StartTime.TotalMilliseconds + minDur, endTime);
  40. }
  41.  
  42. private double HandleOverlapWithNext(double endTime, double nextStartTime)
  43. {
  44.     return Math.Min(nextStartTime - Configuration.Settings.General.MinimumMillisecondsBetweenLines, endTime);
  45. }
  46.  
  47. private double HandleShotChange(List<double> shotChanges, double endTime, Paragraph p)
  48. {
  49.     return Math.Min(endTime, ShotChangeHelper.GetNextShotChangeMinusGapInMs(shotChanges, p.EndTime) ?? double.MaxValue);
  50. }
  51.  
  52. private double HandleMaximumDuration(Paragraph p, double endTime)
  53. {
  54.     var duration = endTime - p.StartTime.TotalMilliseconds;
  55.     return duration > Configuration.Settings.General.SubtitleMaximumDisplayMilliseconds ? p.StartTime.TotalMilliseconds + Configuration.Settings.General.SubtitleMaximumDisplayMilliseconds : endTime;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement