Added yet another subtitle format

git-svn-id: https://subtitleedit.googlecode.com/svn/trunk@616 99eadd0c-20b8-1223-b5c4-2a2b2df33de2
This commit is contained in:
niksedk 2011-09-08 19:09:18 +00:00
parent 9b615ca17a
commit 036cf2afdb
3 changed files with 121 additions and 0 deletions

View File

@ -68,6 +68,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
new UnknownSubtitle5(),
new UnknownSubtitle6(),
new UnknownSubtitle7(),
new UnknownSubtitle8(),
new UTSubtitleXml(),
new YouTubeAnnotations(),
new YouTubeSbv(),

View File

@ -0,0 +1,119 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
{
public class UnknownSubtitle8 : SubtitleFormat
{
//00:04:04.219
//The city council of long beach
readonly Regex _regexTimeCodes = new Regex(@"^\d\d:\d\d:\d\d.\d\d\d$", RegexOptions.Compiled);
public override string Extension
{
get { return ".txt"; }
}
public override string Name
{
get { return "Unknown8"; }
}
public override bool HasLineNumber
{
get { return false; }
}
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)
{
const string paragraphWriteFormat = "{0}\r\n{1}\r\n";
var sb = new StringBuilder();
foreach (Paragraph p in subtitle.Paragraphs)
{
sb.Append(string.Format(paragraphWriteFormat, p.StartTime.ToString().Replace(",","."), p.Text));
}
return sb.ToString().Trim();
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
Paragraph paragraph = new Paragraph();
_errorCount = 0;
subtitle.Paragraphs.Clear();
for (int i = 0; i < lines.Count; i++)
{
string line = lines[i].TrimEnd();
string next = string.Empty;
if (i + 1 < lines.Count)
next = lines[i + 1];
if (line.Contains(":") && !next.Contains(":") && _regexTimeCodes.IsMatch(line) && !_regexTimeCodes.IsMatch(next))
{
paragraph = new Paragraph();
if (TryReadTimeCodesLine(line, paragraph))
{
paragraph.Text = next;
if (paragraph.Text.Trim().Length > 0)
subtitle.Paragraphs.Add(paragraph);
}
else if (line.Trim().Length > 0)
{
_errorCount++;
}
}
}
foreach (Paragraph p in subtitle.Paragraphs)
p.Text = p.Text.Replace(Environment.NewLine + Environment.NewLine, Environment.NewLine);
int index = 0;
foreach (Paragraph p in subtitle.Paragraphs)
{
index++;
Paragraph nextParagraph = subtitle.GetParagraphOrDefault(index);
if (nextParagraph != null)
p.EndTime.TotalMilliseconds = nextParagraph.StartTime.TotalMilliseconds + 1;
else
p.EndTime.TotalMilliseconds = p.StartTime.TotalMilliseconds + 2500;
p.Text = p.Text.Replace(Environment.NewLine + Environment.NewLine, Environment.NewLine);
}
subtitle.Renumber(1);
}
private bool TryReadTimeCodesLine(string line, Paragraph paragraph)
{
string[] parts = line.Split(':', '.');
try
{
int startHours = int.Parse(parts[0]);
int startMinutes = int.Parse(parts[1]);
int startSeconds = int.Parse(parts[2]);
int startMilliseconds = int.Parse(parts[3]);
paragraph.StartTime = new TimeCode(startHours, startMinutes, startSeconds, startMilliseconds);
return true;
}
catch
{
return false;
}
}
}
}

View File

@ -597,6 +597,7 @@
<Compile Include="Logic\SubtitleFormats\UnknownSubtitle6.cs" />
<Compile Include="Logic\SubtitleFormats\UnknownSubtitle7.cs" />
<Compile Include="Logic\SubtitleFormats\DigiBeta.cs" />
<Compile Include="Logic\SubtitleFormats\UnknownSubtitle8.cs" />
<Compile Include="Logic\SubtitleFormats\UTSubtitleXml.cs" />
<Compile Include="Logic\SubtitleFormats\Wsb.cs" />
<Compile Include="Logic\SubtitleFormats\YouTubeAnnotations.cs" />