Added new subtitle format + minor fix for format support

This commit is contained in:
Nikolaj Olsson 2016-05-16 21:27:53 +02:00
parent 8b6eee0b78
commit 5e50534732
4 changed files with 166 additions and 7 deletions

View File

@ -367,6 +367,7 @@
<Compile Include="SubtitleFormats\UnknownSubtitle29.cs" />
<Compile Include="SubtitleFormats\UnknownSubtitle3.cs" />
<Compile Include="SubtitleFormats\UnknownSubtitle30.cs" />
<Compile Include="SubtitleFormats\DlDd.cs" />
<Compile Include="SubtitleFormats\UnknownSubtitle82.cs" />
<Compile Include="SubtitleFormats\UnknownSubtitle81.cs" />
<Compile Include="SubtitleFormats\UnknownSubtitle80.cs" />

View File

@ -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<string> 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<string> lines, string fileName)
{
_errorCount = 0;
var sb = new StringBuilder();
lines.ForEach(line => sb.AppendLine(line));
var xmlAsText = sb.ToString().Trim();
if (!xmlAsText.Contains("</dl>") || !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++;
}
}
}
}

View File

@ -10,7 +10,7 @@ namespace Nikse.SubtitleEdit.Forms
{
public sealed partial class JoinSubtitles : PositionAndSizeForm
{
private List<string> _fileNamesToJoin = new List<string>();
private readonly List<string> _fileNamesToJoin = new List<string>();
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);
}

View File

@ -2157,6 +2157,22 @@ namespace Nikse.SubtitleEdit.Forms
}
}
if (format == null)
{
var f = new DlDd();
var list = new List<string>(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);