SubtitleEdit/libse/SubtitleFormats/UnknownSubtitle1.cs

99 lines
3.6 KiB
C#
Raw Normal View History

2016-02-08 21:11:03 +01:00
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
public class UnknownSubtitle1 : SubtitleFormat
{
//0:01 0:11
private static readonly Regex RegexTimeCodes = new Regex(@"^\d+:\d\d \d+:\d\d ", RegexOptions.Compiled);
2017-08-03 12:43:52 +02:00
public override string Extension => ".txt";
2016-02-08 21:11:03 +01:00
2017-08-03 12:43:52 +02:00
public override string Name => "Unknown 1";
2016-02-08 21:11:03 +01:00
public override string ToText(Subtitle subtitle, string title)
{
//0:01 0:11 "My vengeance needs blood!" -Marquis De Sade
//[Laughter, thunder]
//0:17 0:19 - On this 5th day of December -
//0:19 0:22 in the year of our Lord 1648, -
const string paragraphWriteFormat = "{0} {1} {2}";
var sb = new StringBuilder();
const string format = "{0:0}:{1:00}";
2016-02-08 21:11:03 +01:00
foreach (Paragraph p in subtitle.Paragraphs)
{
int seconds = p.StartTime.Seconds;
if (p.StartTime.Milliseconds >= 500)
seconds++;
string startTime = string.Format(format, p.StartTime.Minutes + p.StartTime.Hours * 60, seconds);
2016-02-08 21:11:03 +01:00
seconds = p.EndTime.Seconds;
if (p.EndTime.Milliseconds >= 500)
seconds++;
string timeOut = string.Format(format, p.EndTime.Minutes + p.EndTime.Hours * 60, seconds);
2016-02-08 21:11:03 +01:00
sb.AppendLine(string.Format(paragraphWriteFormat, startTime, timeOut, p.Text));
}
return sb.ToString().Trim();
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
Paragraph p = null;
_errorCount = 0;
subtitle.Paragraphs.Clear();
var sb = new StringBuilder();
char[] splitChars = { '', ' ' };
2016-02-08 21:11:03 +01:00
foreach (string line in lines)
{
2016-06-07 06:31:03 +02:00
Match match;
if (line.Length > 11 && (match = RegexTimeCodes.Match(line)).Success)
2016-02-08 21:11:03 +01:00
{
if (p != null)
2016-06-07 06:31:03 +02:00
p.Text = (p.Text + Environment.NewLine + sb).Trim();
var parts = line.Substring(0, match.Length).Split(splitChars, StringSplitOptions.RemoveEmptyEntries);
2016-02-08 21:11:03 +01:00
try
{
p = new Paragraph { StartTime = DecodeTimeCode(parts[0]), EndTime = DecodeTimeCode(parts[1]) };
p.Text = line.Substring(match.Length).Trim();
2016-02-08 21:11:03 +01:00
subtitle.Paragraphs.Add(p);
sb.Clear();
2016-02-08 21:11:03 +01:00
}
catch
{
p = null;
_errorCount++;
}
}
else if (p == null)
{
_errorCount++;
}
else
{
sb.AppendLine(line);
2016-02-08 21:11:03 +01:00
}
if (_errorCount > 20)
return;
}
if (p != null)
2016-06-07 06:31:03 +02:00
p.Text = (p.Text + Environment.NewLine + sb).Trim();
2016-02-08 21:11:03 +01:00
subtitle.Renumber();
}
private static TimeCode DecodeTimeCode(string code)
{
//68:20 (minutes:seconds)
var parts = code.Trim().Split(':');
return new TimeCode(0, int.Parse(parts[0]), int.Parse(parts[1]), 0);
}
}
}