SubtitleEdit/libse/SubtitleFormats/TimelineAscii.cs

216 lines
7.4 KiB
C#
Raw Normal View History

2016-02-06 07:52:53 +01:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
/// <summary>
2016-01-26 22:02:22 +01:00
/// Timeline Ascii export - THE MOVIE TITRE EDITOR - http://www.pld.ttu.ee/~priidu/timeline/ by priidu@pld.ttu.ee
2016-02-06 07:52:53 +01:00
///
/// Sample:
/// 1.
/// 00:00:43.02
/// 00:00:47.03
/// ±NE/SEVÎ
/// ³ÂÍÅ/ÑÅÁß
///
/// 2.
/// 00:01:36.00
/// 00:01:37.00
/// ±Viòð ir klât.
/// ³Îí ïðèøåë.
/// </summary>
public class TimeLineAscii : 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 => "Timeline ascii";
2016-02-06 07:52:53 +01:00
public override bool IsMine(List<string> lines, string fileName)
{
if (fileName == null || !fileName.EndsWith(Extension, StringComparison.OrdinalIgnoreCase))
2019-01-19 14:40:37 +01:00
{
2016-02-06 07:52:53 +01:00
return false;
2019-01-19 14:40:37 +01:00
}
2016-02-06 07:52:53 +01:00
2017-08-03 12:43:52 +02:00
return base.IsMine(lines, fileName);
2016-02-06 07:52:53 +01:00
}
public override string ToText(Subtitle subtitle, string title)
{
return string.Empty;
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
Paragraph paragraph = null;
var expecting = ExpectingLine.Number;
_errorCount = 0;
byte firstLineCode = 0;
byte secondLineCode = 0;
subtitle.Paragraphs.Clear();
IEnumerable<byte[]> byteLines = SplitBytesToLines(File.ReadAllBytes(fileName));
foreach (byte[] bytes in byteLines)
{
var line = Encoding.ASCII.GetString(bytes);
if (line.EndsWith('.') && Utilities.IsInteger(line.TrimEnd('.')))
{
if (paragraph != null && !string.IsNullOrEmpty(paragraph.Text))
2019-01-19 14:40:37 +01:00
{
2016-02-06 07:52:53 +01:00
subtitle.Paragraphs.Add(paragraph);
2019-01-19 14:40:37 +01:00
}
2016-02-06 07:52:53 +01:00
paragraph = new Paragraph();
expecting = ExpectingLine.TimeStart;
}
else if (paragraph != null && expecting == ExpectingLine.TimeStart && RegexTimeCode.IsMatch(line))
{
string[] parts = line.Split(new[] { ':', '.' }, 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(new[] { ':', '.' }, 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 (bytes.Length > 1)
{
// get text from encoding
var enc = GetEncodingFromLanguage(bytes[0]);
string s = enc.GetString(bytes, 1, bytes.Length - 1).Trim();
// italic text
if (s.StartsWith('#'))
2019-01-19 14:40:37 +01:00
{
2016-02-06 07:52:53 +01:00
s = "<i>" + s.Remove(0, 1) + "</i>";
2019-01-19 14:40:37 +01:00
}
2016-02-06 07:52:53 +01:00
paragraph.Text = (paragraph.Text + Environment.NewLine + s).Trim();
if (paragraph.Text.Length > 2000)
{
_errorCount += 100;
return;
}
if (paragraph.Text.Contains(Environment.NewLine))
{
if (secondLineCode == 0)
2019-01-19 14:40:37 +01:00
{
2016-02-06 07:52:53 +01:00
secondLineCode = bytes[0];
2019-01-19 14:40:37 +01:00
}
2016-02-06 07:52:53 +01:00
if (secondLineCode != bytes[0])
2019-01-19 14:40:37 +01:00
{
2016-02-06 07:52:53 +01:00
_errorCount++;
2019-01-19 14:40:37 +01:00
}
2016-02-06 07:52:53 +01:00
}
else
{
if (firstLineCode == 0)
2019-01-19 14:40:37 +01:00
{
2016-02-06 07:52:53 +01:00
firstLineCode = bytes[0];
2019-01-19 14:40:37 +01:00
}
2016-02-06 07:52:53 +01:00
if (firstLineCode != bytes[0])
2019-01-19 14:40:37 +01:00
{
2016-02-06 07:52:53 +01:00
_errorCount++;
2019-01-19 14:40:37 +01:00
}
2016-02-06 07:52:53 +01:00
}
}
}
}
}
if (paragraph != null && !string.IsNullOrEmpty(paragraph.Text))
2019-01-19 14:40:37 +01:00
{
2016-02-06 07:52:53 +01:00
subtitle.Paragraphs.Add(paragraph);
2019-01-19 14:40:37 +01:00
}
2016-02-06 07:52:53 +01:00
subtitle.Renumber();
}
2019-01-25 21:04:18 +01:00
internal static IEnumerable<byte[]> SplitBytesToLines(byte[] bytes)
2016-02-06 07:52:53 +01:00
{
var list = new List<byte[]>();
int start = 0;
int index = 0;
while (index < bytes.Length)
{
if (bytes[index] == 13)
{
int length = index - start;
var lineBytes = new byte[length];
Array.Copy(bytes, start, lineBytes, 0, length);
list.Add(lineBytes);
index += 2;
start = index;
}
else
{
index++;
}
}
return list;
}
private Encoding GetEncodingFromLanguage(byte language)
{
if (language == 179) // Russian
2019-01-19 14:40:37 +01:00
{
2016-02-06 07:52:53 +01:00
return Encoding.GetEncoding(1251);
2019-01-19 14:40:37 +01:00
}
2016-02-06 07:52:53 +01:00
if (language == 177) // Baltic
2019-01-19 14:40:37 +01:00
{
2016-02-06 07:52:53 +01:00
return Encoding.GetEncoding(1257);
2019-01-19 14:40:37 +01:00
}
2016-02-06 07:52:53 +01:00
return Encoding.GetEncoding(1252);
}
}
}