SubtitleEdit/libse/SubtitleFormats/Spt.cs

177 lines
5.4 KiB
C#
Raw Normal View History

2016-02-08 21:11:03 +01:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
public class Spt : SubtitleFormat
{
2017-08-03 12:43:52 +02:00
public override string Extension => ".spt";
2016-02-08 21:11:03 +01:00
public const string NameOfFormat = "spt";
2017-08-03 12:43:52 +02:00
public override string Name => NameOfFormat;
2016-02-08 21:11:03 +01:00
public static void Save(string fileName, Subtitle subtitle)
{
var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
2016-02-08 21:11:03 +01:00
// header
fs.WriteByte(1);
for (int i = 1; i < 23; i++)
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
fs.WriteByte(0);
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
fs.WriteByte(0x60);
// paragraphs
foreach (Paragraph p in subtitle.Paragraphs)
{
WriteParagraph(p);
}
// footer
fs.WriteByte(0xff);
for (int i = 0; i < 11; i++)
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
fs.WriteByte(0);
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
fs.WriteByte(0x11);
byte[] footerBuffer = Encoding.ASCII.GetBytes("dummy end of file");
fs.Write(footerBuffer, 0, footerBuffer.Length);
fs.Close();
}
private static void WriteParagraph(Paragraph p)
{
WriteTimeCode();
WriteTimeCode();
string text = p.Text;
if (Utilities.GetNumberOfLines(text) > 2)
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
text = Utilities.AutoBreakLine(p.Text);
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
var lines = text.SplitToLines();
int textLengthFirstLine = 0;
int textLengthSecondLine = 0;
if (lines.Count > 0)
2016-02-08 21:11:03 +01:00
{
textLengthFirstLine = lines[0].Length;
if (lines.Count > 1)
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
textLengthSecondLine = lines[1].Length;
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
}
}
private static void WriteTimeCode()
{
// write 8 bytes time code
}
public override bool IsMine(List<string> lines, string fileName)
{
if (!string.IsNullOrEmpty(fileName) && File.Exists(fileName))
{
try
{
FileInfo fi = new FileInfo(fileName);
if (fi.Length > 100 && fi.Length < 1024000) // not too small or too big
{
byte[] buffer = FileUtil.ReadAllBytesShared(fileName);
if (buffer[00] > 10 &&
buffer[01] == 0 &&
2019-01-27 18:47:55 +01:00
fileName.EndsWith(Extension, StringComparison.OrdinalIgnoreCase))
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
return true;
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
}
}
catch
{
return false;
}
}
return false;
}
public override string ToText(Subtitle subtitle, string title)
{
return "Not supported!";
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
subtitle.Paragraphs.Clear();
byte[] buffer = FileUtil.ReadAllBytesShared(fileName);
int index = buffer[0]; // go to first subtitle paragraph
while (index < buffer.Length)
{
Paragraph p = GetSptParagraph(ref index, buffer);
if (p != null)
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
subtitle.Paragraphs.Add(p);
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
}
subtitle.Renumber();
}
private Paragraph GetSptParagraph(ref int index, byte[] buffer)
{
if (index + 16 + 20 + 4 >= buffer.Length)
{
index = index + 16 + 20 + 4;
return null;
}
int textLengthFirstLine = buffer[index + 16 + 20];
int textLengthSecondLine = buffer[index + 16 + 20 + 4];
var allItalic = buffer[index + 16 + 4] == 1;
2016-02-08 21:11:03 +01:00
if (textLengthFirstLine == 0 && textLengthSecondLine == 0)
{
2019-01-27 18:47:55 +01:00
index += 16 + 20 + 16;
2016-02-08 21:11:03 +01:00
_errorCount++;
return null;
}
try
{
var p = new Paragraph
{
2019-01-27 18:47:55 +01:00
StartTime = Sptx.GetTimeCode(Encoding.Default.GetString(buffer, index, 8)),
EndTime = Sptx.GetTimeCode(Encoding.Default.GetString(buffer, index + 8, 8)),
Text = Sptx.FixItalics(Encoding.Default.GetString(buffer, index + 16 + 20 + 16, textLengthFirstLine))
};
2016-02-08 21:11:03 +01:00
if (textLengthSecondLine > 0)
2019-01-19 14:40:37 +01:00
{
2016-02-08 21:11:03 +01:00
p.Text += Environment.NewLine + Encoding.Default.GetString(buffer, index + 16 + 20 + 16 + textLengthFirstLine, textLengthSecondLine);
2019-01-19 14:40:37 +01:00
}
2016-02-08 21:11:03 +01:00
if (allItalic)
{
p.Text = "<i>" + p.Text.Trim() + "</i>";
}
index += 16 + 20 + 16 + textLengthFirstLine + textLengthSecondLine;
2016-02-08 21:11:03 +01:00
return p;
}
catch
{
index += 16 + 20 + 16 + textLengthFirstLine + textLengthSecondLine;
2016-02-08 21:11:03 +01:00
_errorCount++;
return null;
}
}
}
}