Advertisement
ivandrofly

SubtitleEdit: MergeLinesSameTextUtils.cs

Apr 4th, 2020
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Nikse.SubtitleEdit.Core
  5. {
  6.     public static class MergeLinesSameTextUtils
  7.     {
  8.         private static void AdvancedMergeLines(Subtitle subtitle, bool fixIncrementing, double gapsThreshold)
  9.         {
  10.             int gaps = (int)Math.Round(gapsThreshold);
  11.  
  12.             for (int i = 0; i < subtitle.Paragraphs.Count; i++)
  13.             {
  14.                 Paragraph p = subtitle.Paragraphs[i];
  15.  
  16.                 for (int j = subtitle.Paragraphs.Count - 1; j > i; j--)
  17.                 {
  18.                     // can merge safely, this can happen when the paragraph is very bad sorted
  19.                     if ((subtitle.Paragraphs[j].StartTime.TotalMilliseconds - p.EndTime.TotalMilliseconds) > gapsThreshold)
  20.                     {
  21.                         continue;
  22.                     }
  23.  
  24.                     if (QualifiesForMerge(p, subtitle.Paragraphs[j], gaps) || (fixIncrementing && QualifiesForMergeIncrement(p, subtitle.Paragraphs[j], gaps)))
  25.                     {
  26.                         p.EndTime.TotalMilliseconds = p.EndTime.TotalMilliseconds > subtitle.Paragraphs[j].EndTime.TotalMilliseconds ?
  27.                             p.EndTime.TotalMilliseconds : subtitle.Paragraphs[j].EndTime.TotalMilliseconds;
  28.  
  29.                         // remove the just merged paragraph
  30.                         subtitle.Paragraphs.RemoveAt(j);
  31.                     }
  32.                 }
  33.  
  34.             }
  35.  
  36.             // merge paragraphs with short text
  37.  
  38.             // fix long diplay time
  39.             // - in this case is not optimal to fix long duration here 'cause the narrator may keep saying same thing for long duration
  40.             // fix short display time
  41.             // - seems okay as long as there is no overlaps
  42.  
  43.             subtitle.Sort(Enums.SubtitleSortCriteria.StartTime);
  44.             subtitle.Renumber();
  45.         }
  46.  
  47.         public static Subtitle MergeLinesWithSameTextInSubtitle(Subtitle subtitle, bool fixIncrementing, bool lineAfterNext, int maxMsBetween)
  48.         {
  49.             //AdvancedMergeLines(subtitle, fixIncrementing, maxMsBetween);
  50.             //return subtitle;
  51.  
  52.             var mergedSubtitle = new Subtitle();
  53.             bool lastMerged = false;
  54.             Paragraph p = null;
  55.             for (int i = 1; i < subtitle.Paragraphs.Count; i++)
  56.             {
  57.                 if (!lastMerged)
  58.                 {
  59.                     p = new Paragraph(subtitle.GetParagraphOrDefault(i - 1));
  60.                     mergedSubtitle.Paragraphs.Add(p);
  61.                 }
  62.                 var next = subtitle.GetParagraphOrDefault(i);
  63.                 var afterNext = subtitle.GetParagraphOrDefault(i + 1);
  64.                 if (next != null)
  65.                 {
  66.                     if (QualifiesForMerge(p, next, maxMsBetween) || fixIncrementing && QualifiesForMergeIncrement(p, next, maxMsBetween))
  67.                     {
  68.                         p.Text = next.Text;
  69.                         p.EndTime = next.EndTime;
  70.                         lastMerged = true;
  71.                     }
  72.                     else if (lineAfterNext && QualifiesForMerge(p, afterNext, maxMsBetween) && p.Duration.TotalMilliseconds > afterNext.Duration.TotalMilliseconds)
  73.                     {
  74.                         lastMerged = true;
  75.                     }
  76.                     else
  77.                     {
  78.                         lastMerged = false;
  79.                     }
  80.                 }
  81.                 else
  82.                 {
  83.                     lastMerged = false;
  84.                 }
  85.             }
  86.             if (!lastMerged)
  87.             {
  88.                 mergedSubtitle.Paragraphs.Add(new Paragraph(subtitle.GetParagraphOrDefault(subtitle.Paragraphs.Count - 1)));
  89.             }
  90.             if (mergedSubtitle.Paragraphs.Count > 0)
  91.             {
  92.                 mergedSubtitle.Renumber();
  93.             }
  94.             return mergedSubtitle;
  95.         }
  96.  
  97.         public static bool QualifiesForMerge(Paragraph p, Paragraph next, int maxMsBetween)
  98.         {
  99.             if (p == null || next == null)
  100.             {
  101.                 return false;
  102.             }
  103.  
  104.             if (next.StartTime.TotalMilliseconds - p.EndTime.TotalMilliseconds > maxMsBetween)
  105.             {
  106.                 return false;
  107.             }
  108.  
  109.             if (p.Text != null && next.Text != null)
  110.             {
  111.                 string s = HtmlUtil.RemoveHtmlTags(p.Text.Trim());
  112.                 string s2 = HtmlUtil.RemoveHtmlTags(next.Text.Trim());
  113.                 return string.Compare(s, s2, StringComparison.OrdinalIgnoreCase) == 0;
  114.             }
  115.             return false;
  116.         }
  117.  
  118.         public static bool QualifiesForMergeIncrement(Paragraph p, Paragraph next, int maxMsBetween)
  119.         {
  120.             if (p == null || next == null)
  121.             {
  122.                 return false;
  123.             }
  124.  
  125.             if (next.StartTime.TotalMilliseconds - p.EndTime.TotalMilliseconds > maxMsBetween)
  126.             {
  127.                 return false;
  128.             }
  129.  
  130.             if (p.Text != null && next.Text != null)
  131.             {
  132.                 var s = HtmlUtil.RemoveHtmlTags(p.Text.Trim());
  133.                 var s2 = HtmlUtil.RemoveHtmlTags(next.Text.Trim());
  134.                 if (!string.IsNullOrEmpty(s) && s2.Length >= s.Length && s2.StartsWith(s, StringComparison.OrdinalIgnoreCase))
  135.                 {
  136.                     return true;
  137.                 }
  138.             }
  139.             return false;
  140.         }
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement