SubtitleEdit/libse/SubtitleFormats/UtxFrames.cs

88 lines
2.9 KiB
C#
Raw Normal View History

2016-02-08 21:11:03 +01:00
using System;
using System.Collections.Generic;
using System.Text;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
public class UtxFrames : SubtitleFormat
{
2017-08-03 12:43:52 +02:00
public override string Extension => ".utx";
2016-02-08 21:11:03 +01:00
2017-08-03 12:43:52 +02:00
public override string Name => "UTX (frames)";
2016-02-08 21:11:03 +01:00
public override string ToText(Subtitle subtitle, string title)
{
//I'd forgotten.
//#2060,2188
//Were you somewhere far away?
//- Yes, four years in Switzerland.
//#3885,3926
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();
var text = new StringBuilder();
for (int i = 0; i < lines.Count; i++)
{
string line = lines[i].Trim();
if (line.StartsWith('#'))
{
var timeParts = line.Split(new[] { '#', ',' }, StringSplitOptions.RemoveEmptyEntries);
if (timeParts.Length == 2)
{
try
{
TimeCode start = DecodeTimeCode(timeParts[0]);
TimeCode end = DecodeTimeCode(timeParts[1]);
subtitle.Paragraphs.Add(new Paragraph(start, end, text.ToString().Trim()));
}
catch
{
_errorCount++;
}
}
}
else if (line.Length > 0)
{
text.AppendLine(line.Trim());
if (text.Length > 5000)
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
return;
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
}
else
{
2017-01-20 22:25:06 +01:00
text.Clear();
2016-02-08 21:11:03 +01:00
}
}
subtitle.Renumber();
}
private static string EncodeTimeCode(TimeCode time)
{
long frames = (long)(time.TotalMilliseconds / (TimeCode.BaseUnit / Configuration.Settings.General.CurrentFrameRate));
return frames.ToString();
}
private static TimeCode DecodeTimeCode(string timePart)
{
int milliseconds = (int)Math.Round(TimeCode.BaseUnit / Configuration.Settings.General.CurrentFrameRate * int.Parse(timePart));
2016-02-08 21:11:03 +01:00
return new TimeCode(milliseconds);
}
}
}