Updated generic subtitle parser to handle time code lines starting with line# - thx Hanno :)

This commit is contained in:
Nikolaj Olsson 2017-03-12 15:46:43 +01:00
parent 21ce28eb6b
commit 965668673b
2 changed files with 31 additions and 19 deletions

View File

@ -5,6 +5,9 @@ using System.Text.RegularExpressions;
namespace Nikse.SubtitleEdit.Core
{
/// <summary>
/// Generic subtitle format parser
/// </summary>
public class UknownFormatImporter
{
private static readonly char[] ExpectedSplitChars = { '.', ',', ';', ':' };
@ -264,6 +267,7 @@ namespace Nikse.SubtitleEdit.Core
Paragraph p = null;
var subtitle = new Subtitle();
var sb = new StringBuilder();
char[] splitChars = { ' ', '\t', '-', '>', '<', '{', '}', '[', ']' };
for (int idx = 0; idx < lines.Length; idx++)
{
string line = lines[idx];
@ -272,11 +276,14 @@ namespace Nikse.SubtitleEdit.Core
foreach (char c in lineWithPerhapsOnlyNumbers)
{
if (!char.IsDigit(c))
{
allNumbers = false;
break;
}
}
if (allNumbers && lineWithPerhapsOnlyNumbers.Length > 5)
{
string[] arr = line.Replace('-', ' ').Replace('>', ' ').Replace('{', ' ').Replace('}', ' ').Replace('[', ' ').Replace(']', ' ').Trim().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
string[] arr = line.Split(splitChars, StringSplitOptions.RemoveEmptyEntries);
if (arr.Length == 1)
{
string[] tc = arr[0].Trim().Split(ExpectedSplitChars, StringSplitOptions.RemoveEmptyEntries);
@ -441,7 +448,7 @@ namespace Nikse.SubtitleEdit.Core
Paragraph p = null;
var subtitle = new Subtitle();
var sb = new StringBuilder();
char[] SplitChar = { ' ' };
char[] splitChar = { ' ' };
for (int idx = 0; idx < lines.Length; idx++)
{
string line = lines[idx];
@ -451,8 +458,8 @@ namespace Nikse.SubtitleEdit.Core
matches = regexTimeCodes2.Matches(line);
if (matches.Count == 2)
{
string[] start = matches[0].ToString().Split(SplitChar, StringSplitOptions.RemoveEmptyEntries);
string[] end = matches[1].ToString().Split(SplitChar, StringSplitOptions.RemoveEmptyEntries);
string[] start = matches[0].ToString().Split(splitChar, StringSplitOptions.RemoveEmptyEntries);
string[] end = matches[1].ToString().Split(splitChar, StringSplitOptions.RemoveEmptyEntries);
if ((start.Length == 3 || start.Length == 4) && (end.Length == 3 || end.Length == 4))
{
if (p != null)
@ -491,7 +498,7 @@ namespace Nikse.SubtitleEdit.Core
Paragraph p = null;
var subtitle = new Subtitle();
var sb = new StringBuilder();
char[] SplitChars = { ' ', '\t' };
char[] splitChars = { ' ', '\t' };
for (int idx = 0; idx < lines.Length; idx++)
{
string line = lines[idx];
@ -500,11 +507,14 @@ namespace Nikse.SubtitleEdit.Core
foreach (char c in lineWithPerhapsOnlyNumbers)
{
if (!char.IsDigit(c))
{
allNumbers = false;
break;
}
}
if (allNumbers && lineWithPerhapsOnlyNumbers.Length > 5)
{
string[] arr = line.Replace('-', ' ').Replace('>', ' ').Replace('{', ' ').Replace('}', ' ').Replace('[', ' ').Replace(']', ' ').Trim().Split(SplitChars, StringSplitOptions.RemoveEmptyEntries);
string[] arr = line.Replace('-', ' ').Replace('>', ' ').Replace('{', ' ').Replace('}', ' ').Replace('[', ' ').Replace(']', ' ').Trim().Split(splitChars, StringSplitOptions.RemoveEmptyEntries);
if (arr.Length == 2)
{
string[] start = arr[0].Trim().Split(ExpectedSplitChars, StringSplitOptions.RemoveEmptyEntries);
@ -522,18 +532,20 @@ namespace Nikse.SubtitleEdit.Core
p.EndTime = DecodeTime(end);
}
}
else if (arr.Length == 3)
else if (arr.Length > 3)
{
string[] start = arr[0].Trim().Split(ExpectedSplitChars, StringSplitOptions.RemoveEmptyEntries);
string[] end = arr[1].Trim().Split(ExpectedSplitChars, StringSplitOptions.RemoveEmptyEntries);
string[] duration = arr[2].Trim().Split(ExpectedSplitChars, StringSplitOptions.RemoveEmptyEntries);
if (start.Length < 3)
string[] start;
string[] end;
if (arr[0].Length > 9)
{
start = end;
end = duration;
start = arr[0].Trim().Split(ExpectedSplitChars, StringSplitOptions.RemoveEmptyEntries);
end = arr[1].Trim().Split(ExpectedSplitChars, StringSplitOptions.RemoveEmptyEntries);
}
else
{
start = arr[1].Trim().Split(ExpectedSplitChars, StringSplitOptions.RemoveEmptyEntries);
end = arr[2].Trim().Split(ExpectedSplitChars, StringSplitOptions.RemoveEmptyEntries);
}
if ((start.Length == 3 || start.Length == 4) && (end.Length == 3 || end.Length == 4))
{
if (p != null)
@ -596,7 +608,7 @@ namespace Nikse.SubtitleEdit.Core
Paragraph p = null;
var subtitle = new Subtitle();
var sb = new StringBuilder();
char[] SplitChar = new[] { ' ' };
char[] splitChar = { ' ' };
for (int idx = 0; idx < lines.Length; idx++)
{
string line = lines[idx];
@ -609,7 +621,7 @@ namespace Nikse.SubtitleEdit.Core
}
if (allNumbers && lineWithPerhapsOnlyNumbers.Length > 5)
{
string[] arr = line.Replace('-', ' ').Replace('>', ' ').Replace('{', ' ').Replace('}', ' ').Replace('[', ' ').Replace(']', ' ').Trim().Split(SplitChar, StringSplitOptions.RemoveEmptyEntries);
string[] arr = line.Replace('-', ' ').Replace('>', ' ').Replace('{', ' ').Replace('}', ' ').Replace('[', ' ').Replace(']', ' ').Trim().Split(splitChar, StringSplitOptions.RemoveEmptyEntries);
if (arr.Length == 2)
{
string[] start = arr[0].Trim().Split(ExpectedSplitChars, StringSplitOptions.RemoveEmptyEntries);

View File

@ -52,7 +52,7 @@ namespace Nikse.SubtitleEdit.Forms
int i = 0;
comboBoxFrom.SelectedIndex = 0;
foreach (Nikse.SubtitleEdit.Forms.GoogleTranslate.ComboBoxItem item in comboBoxFrom.Items)
foreach (GoogleTranslate.ComboBoxItem item in comboBoxFrom.Items)
{
if (item.Value == defaultFromLanguage)
{
@ -64,7 +64,7 @@ namespace Nikse.SubtitleEdit.Forms
i = 0;
comboBoxTo.SelectedIndex = 0;
foreach (Nikse.SubtitleEdit.Forms.GoogleTranslate.ComboBoxItem item in comboBoxTo.Items)
foreach (GoogleTranslate.ComboBoxItem item in comboBoxTo.Items)
{
if (item.Value == defaultToLanguage)
{