SubtitleEdit/libse/SubtitleFormats/Tx3gTextOnly.cs

57 lines
2.0 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
public class Tx3GTextOnly : SubtitleFormat
{
public override string Extension => ".tx3g";
public override string Name => "tx3g";
2017-08-10 19:48:50 +02:00
public override string ToText(Subtitle subtitle, string title)
{
throw new NotImplementedException();
}
2017-08-10 19:48:50 +02:00
public static int GetUInt(byte[] buffer, int index)
{
return (buffer[index] << 24) + (buffer[index + 1] << 16) + (buffer[index + 2] << 8) + buffer[index + 3];
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
subtitle.Paragraphs.Clear();
if (!string.IsNullOrEmpty(fileName) && fileName.EndsWith(Extension, StringComparison.OrdinalIgnoreCase) && File.Exists(fileName))
{
var buffer = FileUtil.ReadAllBytesShared(fileName);
int i = 0;
while (i + 4 < buffer.Length)
{
var boxLength = GetUInt(buffer, i);
if (boxLength > 500 || i + 4 + boxLength > buffer.Length)
2019-01-19 14:40:37 +01:00
{
break;
2019-01-19 14:40:37 +01:00
}
if (boxLength > 10 && buffer[i + 4] == 0x73 && buffer[i + 4 + 1] == 0x74 && buffer[i + 4 + 2] == 0x79 && buffer[i + 4 + 3] == 0x6C && buffer[i + 4 + 4] == 0) // styl + 0
{
i += boxLength; // "styl" mp4 box
}
else
{
var text = Encoding.UTF8.GetString(buffer, i + 4, boxLength);
text = string.Join(Environment.NewLine, text.SplitToLines());
subtitle.Paragraphs.Add(new Paragraph { Text = text });
i += boxLength + 4;
}
}
subtitle.Renumber();
}
}
}
}