Added new subtitle format

This commit is contained in:
niksedk 2014-03-14 20:49:19 +01:00
parent d42568d895
commit c6dd0c3d71
3 changed files with 177 additions and 0 deletions

View File

@ -109,6 +109,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
new SwiftInterchange2(),
new SwiftText(),
new SwiftTextLineNumber(),
new SwiftTextLineNOAndDur(),
new Tek(),
new TimeXml(),
new TimeXml2(),

View File

@ -0,0 +1,175 @@
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
{
public class SwiftTextLineNOAndDur : SubtitleFormat
{
enum ExpectingLine
{
TimeCodes,
Text
}
Paragraph _paragraph;
StringBuilder _text = new StringBuilder();
ExpectingLine _expecting = ExpectingLine.TimeCodes;
static readonly Regex RegexTimeCodes = new Regex(@"^SUBTITLE: \d+\s+TIMEIN:\s*[0123456789-]+:[0123456789-]+:[0123456789-]+:[0123456789-]+\s*DURATION:\s*[0123456789-]+:[0123456789-]+\s+TIMEOUT:\s*[0123456789-]+:[0123456789-]+:[0123456789-]+:[0123456789-]+$", RegexOptions.Compiled);
public override string Extension
{
get { return ".txt"; }
}
public override string Name
{
get { return "Swift text line# +dur"; }
}
public override bool IsTimeBased
{
get { return true; }
}
public override bool IsMine(List<string> lines, string fileName)
{
if (lines != null && lines.Count > 2 && !string.IsNullOrEmpty(lines[0]) && lines[0].Contains("{QTtext}"))
return false;
var subtitle = new Subtitle();
LoadSubtitle(subtitle, lines, fileName);
return subtitle.Paragraphs.Count > _errorCount;
}
public override string ToText(Subtitle subtitle, string title)
{
//SUBTITLE: 1 TIMEIN: 00:00:07:01 DURATION: 03:11 TIMEOUT: 00:00:10:12
//Voor de oorlog
//SUBTITLE: 2 TIMEIN: 00:00:10:16 DURATION: 01:08 TIMEOUT: 00:00:11:24
//Ik ben Marie Pinhas. Ik ben geboren
//in Thessaloniki in Griekenland,
//SUBTITLE: 3 TIMEIN: 00:00:12:12 DURATION: 02:10 TIMEOUT: 00:00:14:22
//op 6 maart '31,
//in een heel oude Griekse familie.
const string paragraphWriteFormat = "SUBTITLE: {1}\tTIMEIN: {0}\tDURATION: {4}\tTIMEOUT: {2}\r\n{3}\r\n";
var sb = new StringBuilder();
int count = 1;
foreach (Paragraph p in subtitle.Paragraphs)
{
string startTime = string.Format("{0:00}:{1:00}:{2:00}:{3:00}", p.StartTime.Hours, p.StartTime.Minutes, p.StartTime.Seconds, p.StartTime.Milliseconds / 10);
string timeOut = string.Format("{0:00}:{1:00}:{2:00}:{3:00}", p.EndTime.Hours, p.EndTime.Minutes, p.EndTime.Seconds, p.EndTime.Milliseconds / 10);
string timeDuration = string.Format("{0:00}:{1:00}", p.Duration.Seconds, p.Duration.Milliseconds / 10);
sb.AppendLine(string.Format(paragraphWriteFormat, startTime, count, timeOut, p.Text, timeDuration));
count++;
}
return sb.ToString().Trim();
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
_paragraph = new Paragraph();
_expecting = ExpectingLine.TimeCodes;
_errorCount = 0;
subtitle.Paragraphs.Clear();
foreach (string line in lines)
{
ReadLine(subtitle, line);
if (_text.Length > 1000)
return;
}
if (_text != null && _text.ToString().TrimStart().Length > 0)
{
_paragraph.Text = _text.ToString().Trim();
subtitle.Paragraphs.Add(_paragraph);
}
subtitle.Renumber(1);
}
private void ReadLine(Subtitle subtitle, string line)
{
switch (_expecting)
{
case ExpectingLine.TimeCodes:
if (TryReadTimeCodesLine(line, _paragraph))
{
_text = new StringBuilder();
_expecting = ExpectingLine.Text;
}
else if (line.Trim().Length > 0)
{
_errorCount++;
_expecting = ExpectingLine.Text; // lets go to next paragraph
}
break;
case ExpectingLine.Text:
if (line.Trim().Length > 0)
{
_text.AppendLine(line.TrimEnd());
}
else if (_paragraph != null && _paragraph.EndTime.TotalMilliseconds > 0)
{
_paragraph.Text = _text.ToString().Trim();
subtitle.Paragraphs.Add(_paragraph);
_paragraph = new Paragraph();
_expecting = ExpectingLine.TimeCodes;
}
else
{
_errorCount++;
}
break;
}
}
private bool TryReadTimeCodesLine(string line, Paragraph paragraph)
{
line = line.Trim();
if (RegexTimeCodes.IsMatch(line))
{
//SUBTITLE: 1 TIMEIN: 00:00:07:01 DURATION: 03:11 TIMEOUT: 00:00:10:12
string s = line.Replace("SUBTITLE:", string.Empty).Replace("TIMEIN", string.Empty).Replace("DURATION", string.Empty).Replace("TIMEOUT", string.Empty).Replace(" ", string.Empty).Replace("\t", string.Empty);
string[] parts = s.Split(':');
try
{
int startHours = int.Parse(parts[1]);
int startMinutes = int.Parse(parts[2]);
int startSeconds = int.Parse(parts[3]);
int startMilliseconds = int.Parse(parts[4]) * 10;
int endHours = 0;
if (parts[5+2] != "--")
endHours = int.Parse(parts[5 + 2]);
int endMinutes = 0;
if (parts[6 + 2] != "--")
endMinutes = int.Parse(parts[6 + 2]);
int endSeconds = 0;
if (parts[7 + 2] != "--")
endSeconds = int.Parse(parts[7 + 2]);
int endMilliseconds = 0;
if (parts[8 + 2] != "--")
endMilliseconds = int.Parse(parts[8 + 2]) * 10;
paragraph.StartTime = new TimeCode(startHours, startMinutes, startSeconds, startMilliseconds);
paragraph.EndTime = new TimeCode(endHours, endMinutes, endSeconds, endMilliseconds);
return true;
}
catch
{
return false;
}
}
return false;
}
}
}

View File

@ -882,6 +882,7 @@
<Compile Include="Logic\SubtitleFormats\CaraokeXml.cs" />
<Compile Include="Logic\SubtitleFormats\Cavena890.cs" />
<Compile Include="Logic\SubtitleFormats\CheetahCaption.cs" />
<Compile Include="Logic\SubtitleFormats\SwiftTextLineNoAndDur .cs" />
<Compile Include="Logic\SubtitleFormats\UnknownSubtitle70.cs" />
<Compile Include="Logic\SubtitleFormats\TimeXml2.cs" />
<Compile Include="Logic\SubtitleFormats\OresmeDocXDocument.cs" />