From 5e5053473238b80b3330d56ebeb00e509117880c Mon Sep 17 00:00:00 2001 From: Nikolaj Olsson Date: Mon, 16 May 2016 21:27:53 +0200 Subject: [PATCH] Added new subtitle format + minor fix for format support --- libse/LibSE.csproj | 1 + libse/SubtitleFormats/DlDd.cs | 85 +++++++++++++++++++++++++++++++++++ src/Forms/JoinSubtitles.cs | 39 +++++++++++++--- src/Forms/Main.cs | 48 +++++++++++++++++++- 4 files changed, 166 insertions(+), 7 deletions(-) create mode 100644 libse/SubtitleFormats/DlDd.cs diff --git a/libse/LibSE.csproj b/libse/LibSE.csproj index 3bb083ce6..ad0675c07 100644 --- a/libse/LibSE.csproj +++ b/libse/LibSE.csproj @@ -367,6 +367,7 @@ + diff --git a/libse/SubtitleFormats/DlDd.cs b/libse/SubtitleFormats/DlDd.cs new file mode 100644 index 000000000..7906b98c8 --- /dev/null +++ b/libse/SubtitleFormats/DlDd.cs @@ -0,0 +1,85 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Xml; +using Nikse.SubtitleEdit.Core.Enums; + +namespace Nikse.SubtitleEdit.Core.SubtitleFormats +{ + public class DlDd : SubtitleFormat + { + public override string Extension + { + get { return ".htm"; } + } + + public override string Name + { + get { return "dl dd span"; } + } + + public override bool IsTimeBased + { + get { return true; } + } + + public override bool IsMine(List lines, string fileName) + { + var subtitle = new Subtitle(); + LoadSubtitle(subtitle, lines, fileName); + return subtitle.Paragraphs.Count > _errorCount; + } + + public override string ToText(Subtitle subtitle, string title) + { + throw new NotImplementedException(); + } + + public override void LoadSubtitle(Subtitle subtitle, List lines, string fileName) + { + _errorCount = 0; + var sb = new StringBuilder(); + lines.ForEach(line => sb.AppendLine(line)); + var xmlAsText = sb.ToString().Trim(); + if (!xmlAsText.Contains("") || !xmlAsText.Contains(" data-time")) + { + return; + } + + try + { + var xml = new XmlDocument { XmlResolver = null }; + xml.LoadXml(xmlAsText); + foreach (XmlNode node in xml.DocumentElement.SelectNodes("dd/span")) + { + try + { + var timeCodeIn = new TimeCode(Convert.ToDouble(node.Attributes["data-time"].InnerText)); + var timeCodeOut = new TimeCode(timeCodeIn.TotalMilliseconds + Utilities.GetOptimalDisplayMilliseconds(node.InnerText)); + var p = new Paragraph(timeCodeIn, timeCodeOut, Utilities.AutoBreakLine(node.InnerText)); + subtitle.Paragraphs.Add(p); + } + catch (Exception ex) + { + System.Diagnostics.Debug.WriteLine(ex.Message); + _errorCount++; + } + } + subtitle.Sort(SubtitleSortCriteria.StartTime); + for (int index = 0; index < subtitle.Paragraphs.Count - 1; index++) + { + var paragraph = subtitle.Paragraphs[index]; + var next = subtitle.GetParagraphOrDefault(index + 1); + if (next.StartTime.TotalMilliseconds <= paragraph.EndTime.TotalMilliseconds) + paragraph.EndTime.TotalMilliseconds = next.StartTime.TotalMilliseconds - 1; + } + subtitle.Renumber(); + } + catch (Exception) + { + _errorCount++; + } + } + + } +} diff --git a/src/Forms/JoinSubtitles.cs b/src/Forms/JoinSubtitles.cs index a7da5e9a7..7acf82e93 100644 --- a/src/Forms/JoinSubtitles.cs +++ b/src/Forms/JoinSubtitles.cs @@ -10,7 +10,7 @@ namespace Nikse.SubtitleEdit.Forms { public sealed partial class JoinSubtitles : PositionAndSizeForm { - private List _fileNamesToJoin = new List(); + private readonly List _fileNamesToJoin = new List(); public Subtitle JoinedSubtitle { get; set; } public SubtitleFormat JoinedFormat { get; private set; } @@ -93,6 +93,35 @@ namespace Nikse.SubtitleEdit.Forms var sub = new Subtitle(); Encoding encoding; var format = sub.LoadSubtitle(fileName, out encoding, null); + + if (format == null) + { + var ebu = new Ebu(); + if (ebu.IsMine(null, fileName)) + { + ebu.LoadSubtitle(sub, null, fileName); + format = ebu; + } + } + if (format == null) + { + var pac = new Pac(); + if (pac.IsMine(null, fileName)) + { + pac.LoadSubtitle(sub, null, fileName); + format = pac; + } + } + if (format == null) + { + var cavena890 = new Cavena890(); + if (cavena890.IsMine(null, fileName)) + { + cavena890.LoadSubtitle(sub, null, fileName); + format = cavena890; + } + } + if (format == null) { for (int j = k; j < _fileNamesToJoin.Count; j++) @@ -143,7 +172,7 @@ namespace Nikse.SubtitleEdit.Forms int i = 0; foreach (string fileName in _fileNamesToJoin) { - Subtitle sub = subtitles[i]; + var sub = subtitles[i]; var lvi = new ListViewItem(string.Format("{0:#,###,###}", sub.Paragraphs.Count)); if (sub.Paragraphs.Count > 0) { @@ -162,11 +191,11 @@ namespace Nikse.SubtitleEdit.Forms listViewParts.EndUpdate(); JoinedSubtitle = new Subtitle(); - if (JoinedFormat.FriendlyName != SubRip.NameOfFormat) + if (JoinedFormat != null && JoinedFormat.FriendlyName != SubRip.NameOfFormat) JoinedSubtitle.Header = header; - foreach (Subtitle sub in subtitles) + foreach (var sub in subtitles) { - foreach (Paragraph p in sub.Paragraphs) + foreach (var p in sub.Paragraphs) { JoinedSubtitle.Paragraphs.Add(p); } diff --git a/src/Forms/Main.cs b/src/Forms/Main.cs index 7471627d1..73a158da2 100644 --- a/src/Forms/Main.cs +++ b/src/Forms/Main.cs @@ -2157,6 +2157,22 @@ namespace Nikse.SubtitleEdit.Forms } } + if (format == null) + { + var f = new DlDd(); + var list = new List(File.ReadAllLines(fileName, LanguageAutoDetect.GetEncodingFromFile(fileName))); + if (f.IsMine(list, fileName)) + { + f.LoadSubtitle(_subtitle, list, fileName); + _oldSubtitleFormat = f; + SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat); + SetEncoding(Configuration.Settings.General.DefaultEncoding); + encoding = GetCurrentEncoding(); + justConverted = true; + format = GetCurrentSubtitleFormat(); + } + } + if (format == null) { try @@ -5162,6 +5178,35 @@ namespace Nikse.SubtitleEdit.Forms { Encoding encoding; format = subtitleToAppend.LoadSubtitle(fileName, out encoding, null); + + if (format == null) + { + var ebu = new Ebu(); + if (ebu.IsMine(null, fileName)) + { + ebu.LoadSubtitle(subtitleToAppend, null, fileName); + format = ebu; + } + } + if (format == null) + { + var pac = new Pac(); + if (pac.IsMine(null, fileName)) + { + pac.LoadSubtitle(subtitleToAppend, null, fileName); + format = pac; + } + } + if (format == null) + { + var cavena890 = new Cavena890(); + if (cavena890.IsMine(null, fileName)) + { + cavena890.LoadSubtitle(subtitleToAppend, null, fileName); + format = cavena890; + } + } + if (GetCurrentSubtitleFormat().IsFrameBased) subtitleToAppend.CalculateTimeCodesFromFrameNumbers(CurrentFrameRate); else @@ -18312,8 +18357,7 @@ namespace Nikse.SubtitleEdit.Forms ReloadFromSourceView(); using (var joinSubtitles = new JoinSubtitles()) { - if (joinSubtitles.ShowDialog(this) == DialogResult.OK - && joinSubtitles.JoinedSubtitle != null && joinSubtitles.JoinedSubtitle.Paragraphs.Count > 0 && ContinueNewOrExit()) + if (joinSubtitles.ShowDialog(this) == DialogResult.OK && joinSubtitles.JoinedSubtitle != null && joinSubtitles.JoinedSubtitle.Paragraphs.Count > 0 && ContinueNewOrExit()) { MakeHistoryForUndo(_language.BeforeDisplaySubtitleJoin);