SubtitleEdit/libse/Subtitle.cs

719 lines
28 KiB
C#
Raw Normal View History

2016-02-08 21:11:03 +01:00
using Nikse.SubtitleEdit.Core.Enums;
using Nikse.SubtitleEdit.Core.SubtitleFormats;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace Nikse.SubtitleEdit.Core
{
public class Subtitle
{
public const int MaxFileSize = 1024 * 1024 * 20; // 20 MB
2020-03-07 07:34:42 +01:00
public List<Paragraph> Paragraphs { get; private set; }
2017-03-14 12:08:54 +01:00
public string Header { get; set; } = string.Empty;
public string Footer { get; set; } = string.Empty;
2016-02-08 21:11:03 +01:00
public string FileName { get; set; }
public static int MaximumHistoryItems => 100;
2016-02-08 21:11:03 +01:00
2018-01-27 13:50:44 +01:00
public SubtitleFormat OriginalFormat { get; private set; }
2016-02-08 21:11:03 +01:00
2020-03-07 07:34:42 +01:00
public List<HistoryItem> HistoryItems { get; }
public bool CanUndo => HistoryItems.Count > 0;
2016-02-08 21:11:03 +01:00
public Subtitle()
{
2020-03-07 07:34:42 +01:00
Paragraphs = new List<Paragraph>();
HistoryItems = new List<HistoryItem>();
2016-02-08 21:11:03 +01:00
FileName = "Untitled";
}
public Subtitle(List<HistoryItem> historyItems)
: this()
{
2020-03-07 07:34:42 +01:00
HistoryItems = historyItems;
2016-02-08 21:11:03 +01:00
}
public Subtitle(List<Paragraph> paragraphs, List<HistoryItem> historyItems)
2019-06-04 09:27:51 +02:00
: this()
{
2020-03-07 07:34:42 +01:00
HistoryItems = historyItems;
Paragraphs = paragraphs;
}
2016-02-08 21:11:03 +01:00
/// <summary>
/// Copy constructor (only paragraphs)
/// </summary>
/// <param name="subtitle">Subtitle to copy</param>
/// <param name="generateNewId">Generate new ID (guid) for paragraphs</param>
public Subtitle(Subtitle subtitle, bool generateNewId = true)
: this()
{
if (subtitle == null)
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
return;
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
2020-03-07 07:34:42 +01:00
foreach (var p in subtitle.Paragraphs)
2016-02-08 21:11:03 +01:00
{
2020-03-07 07:34:42 +01:00
Paragraphs.Add(new Paragraph(p, generateNewId));
2016-02-08 21:11:03 +01:00
}
Header = subtitle.Header;
Footer = subtitle.Footer;
FileName = subtitle.FileName;
2016-02-08 21:11:03 +01:00
}
public Subtitle(List<Paragraph> paragraphs) : this()
{
2020-03-07 07:34:42 +01:00
Paragraphs = paragraphs;
}
2016-02-08 21:11:03 +01:00
/// <summary>
/// Get the paragraph of index, null if out of bounds
/// </summary>
/// <param name="index">Index of wanted paragraph</param>
/// <returns>Paragraph, null if index is index is out of bounds</returns>
public Paragraph GetParagraphOrDefault(int index)
{
2020-03-07 07:34:42 +01:00
if (Paragraphs == null || Paragraphs.Count <= index || index < 0)
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
return null;
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
2020-03-07 07:34:42 +01:00
return Paragraphs[index];
2016-02-08 21:11:03 +01:00
}
public Paragraph GetParagraphOrDefaultById(string id)
{
2020-03-07 07:34:42 +01:00
return Paragraphs.FirstOrDefault(p => p.Id == id);
2016-02-08 21:11:03 +01:00
}
public SubtitleFormat ReloadLoadSubtitle(List<string> lines, string fileName, SubtitleFormat format)
{
Paragraphs.Clear();
if (format != null && format.IsMine(lines, fileName))
{
format.LoadSubtitle(this, lines, fileName);
2018-01-27 13:50:44 +01:00
OriginalFormat = format;
2016-02-08 21:11:03 +01:00
return format;
}
2020-03-07 07:34:42 +01:00
foreach (var subtitleFormat in SubtitleFormat.AllSubtitleFormats)
2016-02-08 21:11:03 +01:00
{
if (subtitleFormat.IsMine(lines, fileName))
{
subtitleFormat.LoadSubtitle(this, lines, fileName);
2018-01-27 13:50:44 +01:00
OriginalFormat = subtitleFormat;
2016-02-08 21:11:03 +01:00
return subtitleFormat;
}
}
return null;
}
public SubtitleFormat LoadSubtitle(string fileName, out Encoding encoding, Encoding useThisEncoding)
{
return LoadSubtitle(fileName, out encoding, useThisEncoding, false);
}
2019-08-25 19:26:50 +02:00
public SubtitleFormat LoadSubtitle(string fileName, out Encoding encoding, Encoding useThisEncoding, bool batchMode, double? sourceFrameRate = null, bool loadSubtitle = true)
2016-02-08 21:11:03 +01:00
{
FileName = fileName;
2020-03-07 07:34:42 +01:00
Paragraphs = new List<Paragraph>();
2016-02-08 21:11:03 +01:00
StreamReader sr;
if (useThisEncoding != null)
{
try
{
sr = new StreamReader(fileName, useThisEncoding);
}
catch (Exception exception)
{
System.Diagnostics.Debug.WriteLine(exception.Message);
encoding = Encoding.UTF8;
return null;
}
}
else
{
try
{
sr = new StreamReader(fileName, LanguageAutoDetect.GetEncodingFromFile(fileName), true);
}
catch
{
try
{
var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
2016-02-08 21:11:03 +01:00
sr = new StreamReader(fs);
}
catch (Exception exception)
{
System.Diagnostics.Debug.WriteLine(exception.Message);
encoding = Encoding.UTF8;
return null;
}
}
}
encoding = sr.CurrentEncoding;
var lines = sr.ReadToEnd().SplitToLines();
2016-02-08 21:11:03 +01:00
sr.Close();
var ext = Path.GetExtension(fileName).ToLowerInvariant();
foreach (var subtitleFormat in SubtitleFormat.AllSubtitleFormats.Where(p => p.Extension == ext && !p.Name.StartsWith("Unknown", StringComparison.Ordinal)))
2016-02-08 21:11:03 +01:00
{
if (subtitleFormat.IsMine(lines, fileName))
{
2019-08-25 19:26:50 +02:00
return FinalizeFormat(fileName, batchMode, sourceFrameRate, lines, subtitleFormat, loadSubtitle);
}
}
foreach (var subtitleFormat in SubtitleFormat.AllSubtitleFormats.Where(p => p.Extension != ext || p.Name.StartsWith("Unknown", StringComparison.Ordinal)))
{
if (subtitleFormat.IsMine(lines, fileName))
{
2019-08-25 19:26:50 +02:00
return FinalizeFormat(fileName, batchMode, sourceFrameRate, lines, subtitleFormat, loadSubtitle);
2016-02-08 21:11:03 +01:00
}
}
if (useThisEncoding == null)
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
return LoadSubtitle(fileName, out encoding, Encoding.Unicode);
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
return null;
}
2019-08-25 19:26:50 +02:00
private SubtitleFormat FinalizeFormat(string fileName, bool batchMode, double? sourceFrameRate, List<string> lines, SubtitleFormat subtitleFormat, bool loadSubtitle)
{
Header = null;
subtitleFormat.BatchMode = batchMode;
subtitleFormat.BatchSourceFrameRate = sourceFrameRate;
2019-08-25 19:26:50 +02:00
if (loadSubtitle)
{
subtitleFormat.LoadSubtitle(this, lines, fileName);
}
OriginalFormat = subtitleFormat;
return subtitleFormat;
}
2016-02-08 21:11:03 +01:00
public void MakeHistoryForUndo(string description, SubtitleFormat subtitleFormat, DateTime fileModified, Subtitle original, string originalSubtitleFileName, int lineNumber, int linePosition, int linePositionAlternate)
{
// don't fill memory with history - use a max rollback points
2020-03-07 07:34:42 +01:00
if (HistoryItems.Count > MaximumHistoryItems)
2019-01-19 14:40:37 +01:00
{
2020-03-07 07:34:42 +01:00
HistoryItems.RemoveAt(0);
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
2020-03-07 07:34:42 +01:00
HistoryItems.Add(new HistoryItem(HistoryItems.Count, this, description, FileName, fileModified, subtitleFormat.FriendlyName, original, originalSubtitleFileName, lineNumber, linePosition, linePositionAlternate));
2016-02-08 21:11:03 +01:00
}
public string UndoHistory(int index, out string subtitleFormatFriendlyName, out DateTime fileModified, out Subtitle originalSubtitle, out string originalSubtitleFileName)
{
2020-03-07 07:34:42 +01:00
Paragraphs.Clear();
foreach (var p in HistoryItems[index].Subtitle.Paragraphs)
2019-01-19 14:40:37 +01:00
{
2020-03-07 07:34:42 +01:00
Paragraphs.Add(new Paragraph(p));
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
2020-03-07 07:34:42 +01:00
subtitleFormatFriendlyName = HistoryItems[index].SubtitleFormatFriendlyName;
FileName = HistoryItems[index].FileName;
fileModified = HistoryItems[index].FileModified;
originalSubtitle = new Subtitle(HistoryItems[index].OriginalSubtitle);
originalSubtitleFileName = HistoryItems[index].OriginalSubtitleFileName;
Header = HistoryItems[index].Subtitle.Header;
2016-02-08 21:11:03 +01:00
return FileName;
}
/// <summary>
2016-09-07 04:38:47 +02:00
/// Creates subtitle as text in its native format.
2016-02-08 21:11:03 +01:00
/// </summary>
/// <param name="format">Format to output</param>
/// <returns>Native format as text string</returns>
public string ToText(SubtitleFormat format)
{
return format.ToText(this, Path.GetFileNameWithoutExtension(FileName));
}
public void AddTimeToAllParagraphs(TimeSpan time)
{
2017-05-05 18:20:57 +02:00
double milliseconds = time.TotalMilliseconds;
foreach (var p in Paragraphs)
2016-02-08 21:11:03 +01:00
{
2017-05-05 18:20:57 +02:00
p.StartTime.TotalMilliseconds += milliseconds;
p.EndTime.TotalMilliseconds += milliseconds;
2016-02-08 21:11:03 +01:00
}
}
public void ChangeFrameRate(double oldFrameRate, double newFrameRate)
{
var factor = SubtitleFormat.GetFrameForCalculation(oldFrameRate) / SubtitleFormat.GetFrameForCalculation(newFrameRate);
foreach (var p in Paragraphs)
2016-02-08 21:11:03 +01:00
{
p.StartTime.TotalMilliseconds *= factor;
p.EndTime.TotalMilliseconds *= factor;
2016-02-08 21:11:03 +01:00
}
}
public void AdjustDisplayTimeUsingPercent(double percent, List<int> selectedIndexes)
{
2020-03-07 07:34:42 +01:00
for (int i = 0; i < Paragraphs.Count; i++)
2016-02-08 21:11:03 +01:00
{
if (selectedIndexes == null || selectedIndexes.Contains(i))
{
double nextStartMilliseconds = double.MaxValue;
2020-03-07 07:34:42 +01:00
if (i + 1 < Paragraphs.Count)
2019-01-19 14:40:37 +01:00
{
2020-03-07 07:34:42 +01:00
nextStartMilliseconds = Paragraphs[i + 1].StartTime.TotalMilliseconds;
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
2020-03-07 07:34:42 +01:00
double newEndMilliseconds = Paragraphs[i].EndTime.TotalMilliseconds;
newEndMilliseconds = Paragraphs[i].StartTime.TotalMilliseconds + (((newEndMilliseconds - Paragraphs[i].StartTime.TotalMilliseconds) * percent) / 100.0);
2016-02-08 21:11:03 +01:00
if (newEndMilliseconds > nextStartMilliseconds)
2019-01-19 14:40:37 +01:00
{
newEndMilliseconds = nextStartMilliseconds - Configuration.Settings.General.MinimumMillisecondsBetweenLines;
2019-01-19 14:40:37 +01:00
}
2020-03-07 07:34:42 +01:00
if (percent > 100 && newEndMilliseconds > Paragraphs[i].EndTime.TotalMilliseconds || percent < 100)
2019-01-19 14:40:37 +01:00
{
2020-03-07 07:34:42 +01:00
Paragraphs[i].EndTime.TotalMilliseconds = newEndMilliseconds;
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
}
}
}
public void AdjustDisplayTimeUsingSeconds(double seconds, List<int> selectedIndexes)
{
if (Math.Abs(seconds) < 0.01)
{
return;
}
2020-03-08 18:36:48 +01:00
var adjustMs = seconds * TimeCode.BaseUnit;
if (selectedIndexes != null)
2016-02-08 21:11:03 +01:00
{
foreach (var idx in selectedIndexes)
2016-02-08 21:11:03 +01:00
{
2020-03-09 16:04:58 +01:00
AdjustDisplayTimeUsingMilliseconds(idx, adjustMs);
}
}
else
{
for (int idx = 0; idx < Paragraphs.Count; idx++)
{
2020-03-09 16:04:58 +01:00
AdjustDisplayTimeUsingMilliseconds(idx, adjustMs);
}
}
}
2016-02-08 21:11:03 +01:00
2020-03-09 16:04:58 +01:00
private void AdjustDisplayTimeUsingMilliseconds(int idx, double ms)
{
2020-03-08 18:36:48 +01:00
var p = Paragraphs[idx];
var nextStartTimeInMs = double.MaxValue;
if (idx + 1 < Paragraphs.Count)
{
nextStartTimeInMs = Paragraphs[idx + 1].StartTime.TotalMilliseconds;
}
2020-03-08 18:36:48 +01:00
var newEndTimeInMs = p.EndTime.TotalMilliseconds + ms;
2020-03-08 18:36:48 +01:00
// handle overlap with next
if (newEndTimeInMs > nextStartTimeInMs)
{
2020-03-08 18:36:48 +01:00
newEndTimeInMs = nextStartTimeInMs - Configuration.Settings.General.MinimumMillisecondsBetweenLines;
}
// fix too short duration
2020-03-08 18:36:48 +01:00
var minDur = Math.Max(Configuration.Settings.General.SubtitleMinimumDisplayMilliseconds, 100);
if (p.StartTime.TotalMilliseconds + minDur > newEndTimeInMs)
{
2020-03-08 18:36:48 +01:00
newEndTimeInMs = p.StartTime.TotalMilliseconds + minDur;
}
2020-03-08 18:36:48 +01:00
if (ms > 0 && newEndTimeInMs < p.EndTime.TotalMilliseconds || ms < 0 && newEndTimeInMs > p.EndTime.TotalMilliseconds)
{
2020-03-08 18:36:48 +01:00
return; // do not adjust wrong way
2016-02-08 21:11:03 +01:00
}
2020-03-08 18:36:48 +01:00
p.EndTime.TotalMilliseconds = newEndTimeInMs;
2016-02-08 21:11:03 +01:00
}
2020-03-02 17:17:58 +01:00
public void RecalculateDisplayTimes(double maxCharPerSec, List<int> selectedIndexes, double optimalCharPerSec, bool extendOnly = false)
2016-02-08 21:11:03 +01:00
{
2020-03-02 17:17:58 +01:00
if (selectedIndexes != null)
2016-02-08 21:11:03 +01:00
{
2020-03-02 17:17:58 +01:00
foreach (var index in selectedIndexes)
{
RecalculateDisplayTime(maxCharPerSec, index, optimalCharPerSec, extendOnly);
}
}
else
{
2020-03-07 07:34:42 +01:00
for (int i = 0; i < Paragraphs.Count; i++)
2016-02-08 21:11:03 +01:00
{
2020-03-02 17:17:58 +01:00
RecalculateDisplayTime(maxCharPerSec, i, optimalCharPerSec, extendOnly);
2016-02-08 21:11:03 +01:00
}
}
}
public void RecalculateDisplayTime(double maxCharactersPerSecond, int index, double optimalCharactersPerSeconds, bool extendOnly = false)
{
2020-03-07 07:34:42 +01:00
var p = GetParagraphOrDefault(index);
if (p == null)
2019-01-19 14:40:37 +01:00
{
return;
2019-01-19 14:40:37 +01:00
}
2020-03-07 07:34:42 +01:00
var originalEndTime = p.EndTime.TotalMilliseconds;
2020-03-07 07:34:42 +01:00
var duration = Utilities.GetOptimalDisplayMilliseconds(p.Text, optimalCharactersPerSeconds);
p.EndTime.TotalMilliseconds = p.StartTime.TotalMilliseconds + duration;
2019-01-29 21:33:20 +01:00
var numberOfCharacters = p.Text.CountCharacters(Configuration.Settings.General.CharactersPerSecondsIgnoreWhiteSpace);
while (Utilities.GetCharactersPerSecond(p, numberOfCharacters) > maxCharactersPerSecond)
{
duration++;
p.EndTime.TotalMilliseconds = p.StartTime.TotalMilliseconds + duration;
}
if (extendOnly && p.EndTime.TotalMilliseconds < originalEndTime)
{
p.EndTime.TotalMilliseconds = originalEndTime;
}
2020-03-07 07:34:42 +01:00
var next = GetParagraphOrDefault(index + 1);
if (next != null && p.StartTime.TotalMilliseconds + duration + Configuration.Settings.General.MinimumMillisecondsBetweenLines > next.StartTime.TotalMilliseconds)
{
p.EndTime.TotalMilliseconds = next.StartTime.TotalMilliseconds - Configuration.Settings.General.MinimumMillisecondsBetweenLines;
if (p.Duration.TotalMilliseconds <= 0)
2019-01-19 14:40:37 +01:00
{
p.EndTime.TotalMilliseconds = p.StartTime.TotalMilliseconds + 1;
2019-01-19 14:40:37 +01:00
}
}
}
public void SetFixedDuration(List<int> selectedIndexes, double fixedDurationMilliseconds)
{
2020-03-07 07:34:42 +01:00
for (int i = 0; i < Paragraphs.Count; i++)
{
if (selectedIndexes == null || selectedIndexes.Contains(i))
{
2020-03-07 07:34:42 +01:00
var p = GetParagraphOrDefault(i);
if (p == null)
2019-01-19 14:40:37 +01:00
{
continue;
2019-01-19 14:40:37 +01:00
}
p.EndTime.TotalMilliseconds = p.StartTime.TotalMilliseconds + fixedDurationMilliseconds;
2020-03-07 07:34:42 +01:00
var next = GetParagraphOrDefault(i + 1);
if (next != null && p.StartTime.TotalMilliseconds + fixedDurationMilliseconds + Configuration.Settings.General.MinimumMillisecondsBetweenLines > next.StartTime.TotalMilliseconds)
{
p.EndTime.TotalMilliseconds = next.StartTime.TotalMilliseconds - Configuration.Settings.General.MinimumMillisecondsBetweenLines;
if (p.Duration.TotalMilliseconds <= 0)
2019-01-19 14:40:37 +01:00
{
p.EndTime.TotalMilliseconds = p.StartTime.TotalMilliseconds + 1;
2019-01-19 14:40:37 +01:00
}
}
}
}
}
2016-02-08 21:11:03 +01:00
public void Renumber(int startNumber = 1)
{
var number = startNumber;
2020-03-07 07:34:42 +01:00
int l = Paragraphs.Count + number;
2019-02-23 09:48:12 +01:00
while (number < l)
2016-02-08 21:11:03 +01:00
{
2020-03-07 07:34:42 +01:00
var p = Paragraphs[number - startNumber];
p.Number = number++;
2016-02-08 21:11:03 +01:00
}
}
public int GetIndex(Paragraph p)
{
if (p == null)
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
return -1;
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
2020-03-07 07:34:42 +01:00
int index = Paragraphs.IndexOf(p);
2016-02-08 21:11:03 +01:00
if (index >= 0)
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
return index;
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
2020-03-07 07:34:42 +01:00
for (int i = 0; i < Paragraphs.Count; i++)
2016-02-08 21:11:03 +01:00
{
2020-03-07 07:34:42 +01:00
if (p.Id == Paragraphs[i].Id)
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
return i;
2019-01-19 14:40:37 +01:00
}
2020-03-07 07:34:42 +01:00
if (i < Paragraphs.Count - 1 && p.Id == Paragraphs[i + 1].Id)
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
return i + 1;
2019-01-19 14:40:37 +01:00
}
2020-03-07 07:34:42 +01:00
if (Math.Abs(p.StartTime.TotalMilliseconds - Paragraphs[i].StartTime.TotalMilliseconds) < 0.1 &&
Math.Abs(p.EndTime.TotalMilliseconds - Paragraphs[i].EndTime.TotalMilliseconds) < 0.1)
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
return i;
2019-01-19 14:40:37 +01:00
}
2020-03-07 07:34:42 +01:00
if (p.Number == Paragraphs[i].Number && (Math.Abs(p.StartTime.TotalMilliseconds - Paragraphs[i].StartTime.TotalMilliseconds) < 0.1 ||
Math.Abs(p.EndTime.TotalMilliseconds - Paragraphs[i].EndTime.TotalMilliseconds) < 0.1))
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
return i;
2019-01-19 14:40:37 +01:00
}
2020-03-07 07:34:42 +01:00
if (p.Text == Paragraphs[i].Text && (Math.Abs(p.StartTime.TotalMilliseconds - Paragraphs[i].StartTime.TotalMilliseconds) < 0.1 ||
Math.Abs(p.EndTime.TotalMilliseconds - Paragraphs[i].EndTime.TotalMilliseconds) < 0.1))
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
return i;
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
}
return -1;
}
/// <summary>
/// Get paragraph index by time in seconds
/// </summary>
public int GetIndex(double seconds)
{
var totalMilliseconds = seconds * TimeCode.BaseUnit;
for (int i = 0; i < Paragraphs.Count; i++)
{
var p = Paragraphs[i];
if (totalMilliseconds >= p.StartTime.TotalMilliseconds && totalMilliseconds <= p.EndTime.TotalMilliseconds)
{
return i;
}
}
return -1;
}
public Paragraph GetFirstAlike(Paragraph p)
{
2020-03-07 07:34:42 +01:00
foreach (var item in Paragraphs)
2016-02-08 21:11:03 +01:00
{
if (Math.Abs(p.StartTime.TotalMilliseconds - item.StartTime.TotalMilliseconds) < 0.1 &&
Math.Abs(p.EndTime.TotalMilliseconds - item.EndTime.TotalMilliseconds) < 0.1 &&
p.Text == item.Text)
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
return item;
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
}
return null;
}
public int RemoveEmptyLines()
{
2020-03-07 07:34:42 +01:00
int count = Paragraphs.Count;
2020-03-01 15:50:39 +01:00
if (count <= 0)
2016-02-08 21:11:03 +01:00
{
2020-03-01 15:50:39 +01:00
return 0;
}
2020-03-07 07:34:42 +01:00
int firstNumber = Paragraphs[0].Number;
for (int i = Paragraphs.Count - 1; i >= 0; i--)
2020-03-01 15:50:39 +01:00
{
2020-03-07 07:34:42 +01:00
var p = Paragraphs[i];
2020-03-01 15:50:39 +01:00
if (p.Text.IsOnlyControlCharactersOrWhiteSpace())
2019-01-19 14:40:37 +01:00
{
2020-03-07 07:34:42 +01:00
Paragraphs.RemoveAt(i);
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
}
2020-03-07 07:34:42 +01:00
if (count != Paragraphs.Count)
2020-03-01 15:50:39 +01:00
{
Renumber(firstNumber);
}
2020-03-07 07:34:42 +01:00
return count - Paragraphs.Count;
2016-02-08 21:11:03 +01:00
}
/// <summary>
2020-03-01 15:50:39 +01:00
/// Removes paragraphs by a list of indices
2016-02-08 21:11:03 +01:00
/// </summary>
2020-03-01 15:50:39 +01:00
/// <param name="indices">Indices of paragraphs/lines to delete</param>
2016-02-08 21:11:03 +01:00
/// <returns>Number of lines deleted</returns>
public int RemoveParagraphsByIndices(IEnumerable<int> indices)
{
int count = 0;
foreach (var index in indices.OrderByDescending(p => p))
{
2020-03-07 07:34:42 +01:00
if (index >= 0 && index < Paragraphs.Count)
2016-02-08 21:11:03 +01:00
{
2020-03-07 07:34:42 +01:00
Paragraphs.RemoveAt(index);
2016-02-08 21:11:03 +01:00
count++;
}
}
return count;
}
/// <summary>
2020-03-01 15:50:39 +01:00
/// Removes paragraphs by a list of IDs
2016-02-08 21:11:03 +01:00
/// </summary>
2020-03-01 15:50:39 +01:00
/// <param name="ids">IDs of paragraphs/lines to delete</param>
2016-02-08 21:11:03 +01:00
/// <returns>Number of lines deleted</returns>
public int RemoveParagraphsByIds(IEnumerable<string> ids)
{
2020-03-07 07:34:42 +01:00
int beforeCount = Paragraphs.Count;
Paragraphs = Paragraphs.Where(p => !ids.Contains(p.Id)).ToList();
return beforeCount - Paragraphs.Count;
2016-02-08 21:11:03 +01:00
}
/// <summary>
/// Sort subtitle paragraphs
/// </summary>
/// <param name="sortCriteria">Paragraph sort criteria</param>
public void Sort(SubtitleSortCriteria sortCriteria)
{
switch (sortCriteria)
{
case SubtitleSortCriteria.Number:
2020-03-07 07:34:42 +01:00
Paragraphs = Paragraphs.OrderBy(p => p.Number).ThenBy(p => p.StartTime.TotalMilliseconds).ToList();
2016-02-08 21:11:03 +01:00
break;
case SubtitleSortCriteria.StartTime:
2020-03-07 07:34:42 +01:00
Paragraphs = Paragraphs.OrderBy(p => p.StartTime.TotalMilliseconds).ThenBy(p => p.Number).ToList();
2016-02-08 21:11:03 +01:00
break;
case SubtitleSortCriteria.EndTime:
2020-03-07 07:34:42 +01:00
Paragraphs = Paragraphs.OrderBy(p => p.EndTime.TotalMilliseconds).ThenBy(p => p.Number).ToList();
2016-02-08 21:11:03 +01:00
break;
case SubtitleSortCriteria.Duration:
2020-03-07 07:34:42 +01:00
Paragraphs = Paragraphs.OrderBy(p => p.Duration.TotalMilliseconds).ThenBy(p => p.Number).ToList();
2016-02-08 21:11:03 +01:00
break;
case SubtitleSortCriteria.Text:
2020-03-07 07:34:42 +01:00
Paragraphs = Paragraphs.OrderBy(p => p.Text, StringComparer.Ordinal).ThenBy(p => p.Number).ToList();
2016-02-08 21:11:03 +01:00
break;
case SubtitleSortCriteria.TextMaxLineLength:
2020-03-07 07:34:42 +01:00
Paragraphs = Paragraphs.OrderBy(p => Utilities.GetMaxLineLength(p.Text)).ThenBy(p => p.Number).ToList();
2016-02-08 21:11:03 +01:00
break;
case SubtitleSortCriteria.TextTotalLength:
2020-03-07 07:34:42 +01:00
Paragraphs = Paragraphs.OrderBy(p => p.Text.Length).ThenBy(p => p.Number).ToList();
2016-02-08 21:11:03 +01:00
break;
case SubtitleSortCriteria.TextNumberOfLines:
2020-03-07 07:34:42 +01:00
Paragraphs = Paragraphs.OrderBy(p => p.NumberOfLines).ThenBy(p => p.Number).ToList();
2016-02-08 21:11:03 +01:00
break;
case SubtitleSortCriteria.TextCharactersPerSeconds:
2020-03-07 07:34:42 +01:00
Paragraphs = Paragraphs.OrderBy(Utilities.GetCharactersPerSecond).ThenBy(p => p.Number).ToList();
2016-02-08 21:11:03 +01:00
break;
case SubtitleSortCriteria.WordsPerMinute:
2020-03-07 07:34:42 +01:00
Paragraphs = Paragraphs.OrderBy(p => p.WordsPerMinute).ThenBy(p => p.Number).ToList();
2016-02-08 21:11:03 +01:00
break;
case SubtitleSortCriteria.Style:
2020-03-07 07:34:42 +01:00
Paragraphs = Paragraphs.OrderBy(p => p.Extra, StringComparer.Ordinal).ThenBy(p => p.Number).ToList();
2016-02-08 21:11:03 +01:00
break;
case SubtitleSortCriteria.Actor:
2020-03-07 07:34:42 +01:00
Paragraphs = Paragraphs.OrderBy(p => p.Actor, StringComparer.Ordinal).ThenBy(p => p.Number).ToList();
break;
2016-02-08 21:11:03 +01:00
}
}
2019-01-12 23:20:36 +01:00
public int InsertParagraphInCorrectTimeOrder(Paragraph newParagraph)
2016-02-08 21:11:03 +01:00
{
for (int i = 0; i < Paragraphs.Count; i++)
{
2020-03-07 07:34:42 +01:00
var p = Paragraphs[i];
2016-02-08 21:11:03 +01:00
if (newParagraph.StartTime.TotalMilliseconds < p.StartTime.TotalMilliseconds)
{
Paragraphs.Insert(i, newParagraph);
2019-01-12 23:20:36 +01:00
return i;
2016-02-08 21:11:03 +01:00
}
}
Paragraphs.Add(newParagraph);
2019-01-12 23:20:36 +01:00
return Paragraphs.Count - 1;
2016-02-08 21:11:03 +01:00
}
2019-09-29 14:33:26 +02:00
public Paragraph GetFirstParagraphOrDefaultByTime(double milliseconds)
{
foreach (var p in Paragraphs)
{
if (p.StartTime.TotalMilliseconds < milliseconds && milliseconds < p.EndTime.TotalMilliseconds)
{
return p;
}
}
return null;
}
2016-02-08 21:11:03 +01:00
/// <summary>
/// Fast hash code for subtitle - includes pre (encoding atm) + header + number + start + end + text + style + actor + extra.
2016-02-08 21:11:03 +01:00
/// </summary>
/// <returns>Hash value that can be used for quick compare</returns>
2019-02-10 12:23:33 +01:00
public int GetFastHashCode(string pre)
2016-02-08 21:11:03 +01:00
{
2019-02-10 12:23:33 +01:00
unchecked // Overflow is fine, just wrap
2019-01-19 14:40:37 +01:00
{
2019-02-10 12:23:33 +01:00
int hash = 17;
if (pre != null)
{
hash = hash * 23 + pre.GetHashCode();
2019-02-10 12:27:19 +01:00
}
2019-02-10 12:23:33 +01:00
if (Header != null)
{
hash = hash * 23 + Header.Trim().GetHashCode();
}
var max = Paragraphs.Count;
for (int i = 0; i < max; i++)
{
var p = Paragraphs[i];
hash = hash * 23 + p.Number.GetHashCode();
hash = hash * 23 + p.StartTime.TotalMilliseconds.GetHashCode();
hash = hash * 23 + p.EndTime.TotalMilliseconds.GetHashCode();
hash = hash * 23 + p.Text.GetHashCode();
if (p.Style != null)
{
hash = hash * 23 + p.Style.GetHashCode();
}
if (p.Extra != null)
{
hash = hash * 23 + p.Extra.GetHashCode();
}
if (p.Actor != null)
{
hash = hash * 23 + p.Actor.GetHashCode();
}
2019-02-10 12:23:33 +01:00
}
return hash;
2016-02-08 21:11:03 +01:00
}
}
/// <summary>
2020-03-09 07:18:02 +01:00
/// Concatenates all Paragraphs Text property, using the default NewLine string between each Text.
/// </summary>
2020-03-09 07:18:02 +01:00
/// <returns>Concatenated Text property of all Paragraphs.</returns>
public string GetAllTexts()
{
int max = Paragraphs.Count;
2020-03-09 07:18:02 +01:00
const int averageLength = 40;
var sb = new StringBuilder(max * averageLength);
for (var index = 0; index < max; index++)
{
sb.AppendLine(Paragraphs[index].Text);
}
return sb.ToString();
}
/// <summary>
/// Concatenates all Paragraphs Text property, using the default NewLine string between each Text.
/// </summary>
/// <returns>Concatenated Text property of all Paragraphs.</returns>
public string GetAllTexts(int stopAfterBytes)
{
int max = Paragraphs.Count;
const int averageLength = 40;
var sb = new StringBuilder(max * averageLength);
for (var index = 0; index < max; index++)
{
sb.AppendLine(Paragraphs[index].Text);
if (sb.Length > stopAfterBytes)
{
return sb.ToString();
}
}
return sb.ToString();
}
2016-02-08 21:11:03 +01:00
}
}