2016-02-08 21:11:03 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
2016-02-23 00:32:36 +01:00
|
|
|
|
using System.Text.RegularExpressions;
|
2016-02-08 21:11:03 +01:00
|
|
|
|
|
|
|
|
|
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
|
|
|
|
|
{
|
|
|
|
|
public class Utx : SubtitleFormat
|
|
|
|
|
{
|
2016-02-23 00:32:36 +01:00
|
|
|
|
// #0:03:03.23,0:03:08.05
|
|
|
|
|
private static readonly Regex RegexTimeCode = new Regex(@"^#\d\d?:\d\d:\d\d\.\d\d,\d:\d\d:\d\d\.\d\d$", RegexOptions.Compiled);
|
2016-02-08 21:11:03 +01:00
|
|
|
|
|
|
|
|
|
public override string Extension
|
|
|
|
|
{
|
|
|
|
|
get { return ".utx"; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string Name
|
|
|
|
|
{
|
|
|
|
|
get { return "UTX"; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
//I'd forgotten.
|
|
|
|
|
//#0:02:58.21,0:03:00.16
|
|
|
|
|
|
|
|
|
|
//Were you somewhere far away?
|
|
|
|
|
//- Yes, four years in Switzerland.
|
|
|
|
|
//#0:03:02.15,0:03:06.14
|
|
|
|
|
|
|
|
|
|
const string paragraphWriteFormat = "{0}{1}#{2},{3}{1}";
|
|
|
|
|
|
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
foreach (Paragraph p in subtitle.Paragraphs)
|
|
|
|
|
{
|
|
|
|
|
sb.AppendLine(string.Format(paragraphWriteFormat, p.Text, Environment.NewLine, EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime)));
|
|
|
|
|
}
|
|
|
|
|
return sb.ToString().Trim();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
|
|
|
|
|
{
|
|
|
|
|
_errorCount = 0;
|
|
|
|
|
subtitle.Paragraphs.Clear();
|
2016-02-23 00:32:36 +01:00
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
char[] splitChars = { '#', ',' };
|
|
|
|
|
char[] splitChars2 = { ':', '.' };
|
2016-02-08 21:11:03 +01:00
|
|
|
|
for (int i = 0; i < lines.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
string line = lines[i].Trim();
|
2016-02-23 00:32:36 +01:00
|
|
|
|
if (line.StartsWith('#') && RegexTimeCode.IsMatch(line))
|
2016-02-08 21:11:03 +01:00
|
|
|
|
{
|
2016-02-23 00:32:36 +01:00
|
|
|
|
var timeParts = line.Split(splitChars, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
try
|
2016-02-08 21:11:03 +01:00
|
|
|
|
{
|
2016-02-23 00:32:36 +01:00
|
|
|
|
string[] startParts = timeParts[0].Split(splitChars2, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
string[] endParts = timeParts[1].Split(splitChars2, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
TimeCode start = DecodeTimeCodeFramesFourParts(startParts);
|
|
|
|
|
TimeCode end = DecodeTimeCodeFramesFourParts(endParts);
|
|
|
|
|
subtitle.Paragraphs.Add(new Paragraph(start, end, sb.ToString().Trim()));
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
_errorCount++;
|
2016-02-08 21:11:03 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (line.Length > 0)
|
|
|
|
|
{
|
2016-02-23 00:32:36 +01:00
|
|
|
|
sb.AppendLine(line);
|
|
|
|
|
if (sb.Length > 5000)
|
2016-02-08 21:11:03 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-02-23 00:32:36 +01:00
|
|
|
|
sb.Clear();
|
2016-02-08 21:11:03 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
subtitle.Renumber();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string EncodeTimeCode(TimeCode time)
|
|
|
|
|
{
|
|
|
|
|
//0:03:02.15
|
|
|
|
|
return string.Format("{0}:{1:00}:{2:00}.{3:00}", time.Hours, time.Minutes, time.Seconds, MillisecondsToFramesMaxFrameRate(time.Milliseconds));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|