SubtitleEdit/libse/SubtitleFormats/UnknownSubtitle30.cs

144 lines
5.2 KiB
C#
Raw Normal View History

2016-02-06 07:52:53 +01:00
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
public class UnknownSubtitle30 : SubtitleFormat
{
private static readonly Regex RegexTimeCode = new Regex(@"^\d\d:\d\d:\d\d:\d\d$", RegexOptions.Compiled);
private enum ExpectingLine
{
Number,
TimeStart,
TimeEnd,
Text
}
2017-08-03 12:43:52 +02:00
public override string Extension => ".asc";
2016-02-06 07:52:53 +01:00
2017-08-03 12:43:52 +02:00
public override string Name => "Unknown 30";
2016-02-06 07:52:53 +01:00
public override string ToText(Subtitle subtitle, string title)
{
//@ headers
// 1.
// 00:00:04:12
// 00:00:06:05
// Berniukai, tik pažiūrėkit.
// 2.
// 00:00:06:16
// 00:00:07:20
// Argi ne puiku?
// 3.
// 00:00:08:02
// 00:00:10:20
// Tėti, ar galime čia paplaukioti?
// -Aišku, kad galim.
const string paragraphWriteFormat = "{4}.{3}{0}{3}{1}{3}{2}{3}";
var sb = new StringBuilder();
sb.AppendLine("@ headers");
sb.AppendLine();
int count = 0;
foreach (Paragraph p in subtitle.Paragraphs)
{
count++;
var text = HtmlUtil.RemoveOpenCloseTags(p.Text, HtmlUtil.TagFont);
sb.AppendLine(string.Format(paragraphWriteFormat, EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime), text, Environment.NewLine, count));
}
return sb.ToString().Trim();
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
Paragraph paragraph = null;
ExpectingLine expecting = ExpectingLine.Number;
_errorCount = 0;
subtitle.Paragraphs.Clear();
foreach (string line in lines)
{
if (line.EndsWith('.') && Utilities.IsInteger(line.TrimEnd('.')))
{
if (paragraph != null)
subtitle.Paragraphs.Add(paragraph);
paragraph = new Paragraph();
expecting = ExpectingLine.TimeStart;
}
else if (paragraph != null && expecting == ExpectingLine.TimeStart && RegexTimeCode.IsMatch(line))
{
string[] parts = line.Split(SplitCharColon, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 4)
{
try
{
var tc = DecodeTimeCodeFramesFourParts(parts);
2016-02-06 07:52:53 +01:00
paragraph.StartTime = tc;
expecting = ExpectingLine.TimeEnd;
}
catch
{
_errorCount++;
expecting = ExpectingLine.Number;
}
}
}
else if (paragraph != null && expecting == ExpectingLine.TimeEnd && RegexTimeCode.IsMatch(line))
{
string[] parts = line.Split(SplitCharColon, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length == 4)
{
try
{
var tc = DecodeTimeCodeFramesFourParts(parts);
2016-02-06 07:52:53 +01:00
paragraph.EndTime = tc;
expecting = ExpectingLine.Text;
}
catch
{
_errorCount++;
expecting = ExpectingLine.Number;
}
}
}
else
{
if (paragraph != null && expecting == ExpectingLine.Text)
{
if (line.Length > 0)
{
string s = line;
paragraph.Text = (paragraph.Text + Environment.NewLine + s).Trim();
if (paragraph.Text.Length > 2000)
{
_errorCount += 100;
return;
}
}
}
else if (line.Length > 0 && line != "@ headers")
{
_errorCount++;
}
}
}
if (paragraph != null && !string.IsNullOrEmpty(paragraph.Text))
subtitle.Paragraphs.Add(paragraph);
subtitle.Renumber();
}
private static string EncodeTimeCode(TimeCode time)
{
return string.Format("{0:00}:{1:00}:{2:00}:{3:00}", time.Hours, time.Minutes, time.Seconds, MillisecondsToFramesMaxFrameRate(time.Milliseconds));
}
}
}