SubtitleEdit/libse/SubtitleFormats/UnknownSubtitle80.cs
2016-02-17 19:14:50 +01:00

130 lines
4.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
public class UnknownSubtitle80 : SubtitleFormat
{
// 1<HT>01033902/01034028<HT>xxx
private static readonly Regex RegexTimeCode = new Regex(@"^\d+\t\d+\/\d+\t", RegexOptions.Compiled);
public override string Extension
{
get { return ".cap"; }
}
public override string Name
{
get { return "Unknown 80"; }
}
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}\t{1}/{2}\t{3}";
var sb = new StringBuilder();
sb.AppendLine("Lambda字幕V4\tDF1+1\tSCENE\"和文標準\"");
sb.AppendLine();
int count = 1;
foreach (Paragraph p in subtitle.Paragraphs)
{
var text = HtmlUtil.RemoveHtmlTags(p.Text.Trim()).Replace(Environment.NewLine, Environment.NewLine + "\t\t\t\t");
sb.AppendLine(string.Format(paragraphWriteFormat, count, EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime), text));
count++;
}
return sb.ToString();
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
Paragraph paragraph = null;
_errorCount = 0;
subtitle.Paragraphs.Clear();
var text = new StringBuilder();
foreach (string line in lines)
{
string s = line.Trim();
if (s.Length > 19 && RegexTimeCode.IsMatch(s))
{
var lineParts = s.Split('\t');
var parts = lineParts[1].Split('/');
if (parts.Length == 2)
{
if (text.Length > 0 && paragraph != null)
{
paragraph.Text = text.ToString().Trim();
}
try
{
var startTime = parts[0].Trim();
var endTime = parts[1].Trim();
string[] startTimeParts = { startTime.Substring(0, 2), startTime.Substring(2, 2), startTime.Substring(4, 2), startTime.Substring(6, 2) };
string[] endTimeParts = { endTime.Substring(0, 2), endTime.Substring(2, 2), endTime.Substring(4, 2), endTime.Substring(6, 2) };
paragraph = new Paragraph { StartTime = DecodeTimeCodeFramesFourParts(startTimeParts), EndTime = DecodeTimeCodeFramesFourParts(endTimeParts) };
subtitle.Paragraphs.Add(paragraph);
text = new StringBuilder();
s = s.Remove(0, 18 + lineParts[0].Length).Trim();
var idxA = s.IndexOf('');
if (idxA > 0)
{
s = s.Substring(0, idxA - 1).Trim();
}
text.Append(s);
}
catch (Exception)
{
_errorCount++;
}
}
else
{
_errorCount++;
}
}
else if (paragraph != null && text.Length < 150)
{
var idxA = s.IndexOf('');
if (idxA > 0)
{
s = s.Substring(0, idxA - 1).Trim();
}
text.Append(Environment.NewLine + s);
}
else
{
_errorCount++;
if (_errorCount > 10)
return;
}
}
if (text.Length > 0 && paragraph != null)
{
paragraph.Text = text.ToString().Trim();
}
subtitle.RemoveEmptyLines();
subtitle.Renumber();
}
private static string EncodeTimeCode(TimeCode time)
{
return time.ToHHMMSSFF().Replace(":", string.Empty);
}
}
}