Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Nikse.SubtitleEdit.Extensions
- {
- public static class SubtitleExtensions
- {
- public static void Merge(this Subtitle subtitle, IEnumerable<Paragraph> paragraphs)
- {
- var mergingParagraphs = paragraphs.OrderBy(p => p.Number).ToArray();
- if (mergingParagraphs.Length < 2)
- {
- return;
- }
- // check if sequential
- if (!IsSequential(paragraphs))
- {
- return;
- }
- // Array.Sort(paragraphs, (p, other) => p.Number.CompareTo(other.Number));
- var pivotParagraph = mergingParagraphs.First();
- pivotParagraph.EndTime = mergingParagraphs.Last().EndTime;
- pivotParagraph.Text = Utilities.AutoBreakLine(string.Join(Environment.NewLine, mergingParagraphs.Select(p => p.Text)));
- // note: this required that the paragraph number is sequential and starting from 1
- subtitle.Paragraphs.RemoveRange(pivotParagraph.Number - 1, mergingParagraphs.Skip(1).Count());
- bool IsSequential(IEnumerable<Paragraph> sequentialParagraphs)
- {
- int sequenceStart = sequentialParagraphs.First().Number;
- var range = Enumerable.Range(sequenceStart, sequentialParagraphs.Count());
- var indexedParagraph = sequentialParagraphs.Zip(range, (p, i) => new ValueTuple<int, Paragraph>(i, p));
- return sequentialParagraphs.Skip(1).All(p => p.Number == sequenceStart + 1);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement