Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Nikse.SubtitleEdit.Core
- {
- public static class MergeLinesSameTextUtils
- {
- private static void AdvancedMergeLines(Subtitle subtitle, bool fixIncrementing, double gapsThreshold)
- {
- int gaps = (int)Math.Round(gapsThreshold);
- for (int i = 0; i < subtitle.Paragraphs.Count; i++)
- {
- Paragraph p = subtitle.Paragraphs[i];
- for (int j = subtitle.Paragraphs.Count - 1; j > i; j--)
- {
- // can merge safely, this can happen when the paragraph is very bad sorted
- if ((subtitle.Paragraphs[j].StartTime.TotalMilliseconds - p.EndTime.TotalMilliseconds) > gapsThreshold)
- {
- continue;
- }
- if (QualifiesForMerge(p, subtitle.Paragraphs[j], gaps) || (fixIncrementing && QualifiesForMergeIncrement(p, subtitle.Paragraphs[j], gaps)))
- {
- p.EndTime.TotalMilliseconds = p.EndTime.TotalMilliseconds > subtitle.Paragraphs[j].EndTime.TotalMilliseconds ?
- p.EndTime.TotalMilliseconds : subtitle.Paragraphs[j].EndTime.TotalMilliseconds;
- // remove the just merged paragraph
- subtitle.Paragraphs.RemoveAt(j);
- }
- }
- }
- // merge paragraphs with short text
- // fix long diplay time
- // - in this case is not optimal to fix long duration here 'cause the narrator may keep saying same thing for long duration
- // fix short display time
- // - seems okay as long as there is no overlaps
- subtitle.Sort(Enums.SubtitleSortCriteria.StartTime);
- subtitle.Renumber();
- }
- public static Subtitle MergeLinesWithSameTextInSubtitle(Subtitle subtitle, bool fixIncrementing, bool lineAfterNext, int maxMsBetween)
- {
- //AdvancedMergeLines(subtitle, fixIncrementing, maxMsBetween);
- //return subtitle;
- var mergedSubtitle = new Subtitle();
- bool lastMerged = false;
- Paragraph p = null;
- for (int i = 1; i < subtitle.Paragraphs.Count; i++)
- {
- if (!lastMerged)
- {
- p = new Paragraph(subtitle.GetParagraphOrDefault(i - 1));
- mergedSubtitle.Paragraphs.Add(p);
- }
- var next = subtitle.GetParagraphOrDefault(i);
- var afterNext = subtitle.GetParagraphOrDefault(i + 1);
- if (next != null)
- {
- if (QualifiesForMerge(p, next, maxMsBetween) || fixIncrementing && QualifiesForMergeIncrement(p, next, maxMsBetween))
- {
- p.Text = next.Text;
- p.EndTime = next.EndTime;
- lastMerged = true;
- }
- else if (lineAfterNext && QualifiesForMerge(p, afterNext, maxMsBetween) && p.Duration.TotalMilliseconds > afterNext.Duration.TotalMilliseconds)
- {
- lastMerged = true;
- }
- else
- {
- lastMerged = false;
- }
- }
- else
- {
- lastMerged = false;
- }
- }
- if (!lastMerged)
- {
- mergedSubtitle.Paragraphs.Add(new Paragraph(subtitle.GetParagraphOrDefault(subtitle.Paragraphs.Count - 1)));
- }
- if (mergedSubtitle.Paragraphs.Count > 0)
- {
- mergedSubtitle.Renumber();
- }
- return mergedSubtitle;
- }
- public static bool QualifiesForMerge(Paragraph p, Paragraph next, int maxMsBetween)
- {
- if (p == null || next == null)
- {
- return false;
- }
- if (next.StartTime.TotalMilliseconds - p.EndTime.TotalMilliseconds > maxMsBetween)
- {
- return false;
- }
- if (p.Text != null && next.Text != null)
- {
- string s = HtmlUtil.RemoveHtmlTags(p.Text.Trim());
- string s2 = HtmlUtil.RemoveHtmlTags(next.Text.Trim());
- return string.Compare(s, s2, StringComparison.OrdinalIgnoreCase) == 0;
- }
- return false;
- }
- public static bool QualifiesForMergeIncrement(Paragraph p, Paragraph next, int maxMsBetween)
- {
- if (p == null || next == null)
- {
- return false;
- }
- if (next.StartTime.TotalMilliseconds - p.EndTime.TotalMilliseconds > maxMsBetween)
- {
- return false;
- }
- if (p.Text != null && next.Text != null)
- {
- var s = HtmlUtil.RemoveHtmlTags(p.Text.Trim());
- var s2 = HtmlUtil.RemoveHtmlTags(next.Text.Trim());
- if (!string.IsNullOrEmpty(s) && s2.Length >= s.Length && s2.StartsWith(s, StringComparison.OrdinalIgnoreCase))
- {
- return true;
- }
- }
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement