diff --git a/src/Forms/Main.cs b/src/Forms/Main.cs index 2f28ea9a5..b4f81186b 100644 --- a/src/Forms/Main.cs +++ b/src/Forms/Main.cs @@ -387,6 +387,15 @@ namespace Nikse.SubtitleEdit.Forms format = cavena890; } } + if (format == null) + { + var spt = new Spt(); + if (spt.IsMine(null, fileName)) + { + spt.LoadSubtitle(sub, null, fileName); + format = spt; + } + } if (format == null) { @@ -1318,6 +1327,19 @@ namespace Nikse.SubtitleEdit.Forms } } + if (format == null) + { + var spt = new Spt(); + if (spt.IsMine(null, fileName)) + { + spt.LoadSubtitle(_subtitle, null, fileName); + _oldSubtitleFormat = spt; + SetFormatToSubRip(); + justConverted = true; + format = GetCurrentSubtitleFormat(); + } + } + if (format == null) { var bdnXml = new BdnXml(); diff --git a/src/Forms/Main.resx b/src/Forms/Main.resx index 9f847b8d4..e6c0d71f9 100644 --- a/src/Forms/Main.resx +++ b/src/Forms/Main.resx @@ -684,7 +684,7 @@ AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w LCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkFAQAAACZTeXN0 ZW0uV2luZG93cy5Gb3Jtcy5JbWFnZUxpc3RTdHJlYW1lcgEAAAAERGF0YQcCAgAAAAkDAAAADwMAAAD2 - CAAAAk1TRnQBSQFMAgEBAgEAAVgBBwFYAQcBEAEAARABAAT/AQkBAAj/AUIBTQE2AQQGAAE2AQQCAAEo + CAAAAk1TRnQBSQFMAgEBAgEAAWABBwFgAQcBEAEAARABAAT/AQkBAAj/AUIBTQE2AQQGAAE2AQQCAAEo AwABQAMAARADAAEBAQABCAYAAQQYAAGAAgABgAMAAoABAAGAAwABgAEAAYABAAKAAgADwAEAAcAB3AHA AQAB8AHKAaYBAAEzBQABMwEAATMBAAEzAQACMwIAAxYBAAMcAQADIgEAAykBAANVAQADTQEAA0IBAAM5 AQABgAF8Af8BAAJQAf8BAAGTAQAB1gEAAf8B7AHMAQABxgHWAe8BAAHWAucBAAGQAakBrQIAAf8BMwMA diff --git a/src/Logic/SubtitleFormats/Spt.cs b/src/Logic/SubtitleFormats/Spt.cs new file mode 100644 index 000000000..ee2123aec --- /dev/null +++ b/src/Logic/SubtitleFormats/Spt.cs @@ -0,0 +1,168 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace Nikse.SubtitleEdit.Logic.SubtitleFormats +{ + class Spt : SubtitleFormat + { + public override string Extension + { + get { return ".spt"; } + } + + public override string Name + { + get { return "spt"; } + } + + public override bool HasLineNumber + { + get { return false; } + } + + public override bool IsTimeBased + { + get { return true; } + } + + public void Save(string fileName, Subtitle subtitle) + { + FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write); + + // header + fs.WriteByte(1); + for (int i = 1; i < 23; i++) + fs.WriteByte(0); + fs.WriteByte(0x60); + + // paragraphs + int number = 0; + foreach (Paragraph p in subtitle.Paragraphs) + { + WriteParagraph(fs, p, number); + number++; + } + + // footer + fs.WriteByte(0xff); + for (int i = 0; i < 11; i++) + fs.WriteByte(0); + fs.WriteByte(0x11); + byte[] footerBuffer = System.Text.Encoding.ASCII.GetBytes("dummy end of file"); + fs.Write(footerBuffer, 0, footerBuffer.Length); + + fs.Close(); + } + + private void WriteParagraph(FileStream fs, Paragraph p, int number) + { + WriteTimeCode(fs, p.StartTime); + WriteTimeCode(fs, p.EndTime); + + string text = p.Text; + if (Utilities.CountTagInText(text, Environment.NewLine)> 1) + text = Utilities.AutoBreakLine(p.Text); + + string[] lines = text.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); + int textLengthFirstLine = 0; + int textLengthSecondLine = 0; + if (lines.Length > 0) + { + textLengthFirstLine = lines[0].Length; + if (lines.Length > 1) + textLengthSecondLine = lines[1].Length; + } + } + + private void WriteTimeCode(FileStream fs, TimeCode timeCode) + { + // write 8 bytes time code + } + + public override bool IsMine(List lines, string fileName) + { + if (!string.IsNullOrEmpty(fileName) && File.Exists(fileName)) + { + FileInfo fi = new FileInfo(fileName); + if (fi.Length > 100 && fi.Length < 1024000) // not too small or too big + { + byte[] buffer = File.ReadAllBytes(fileName); + + if (buffer[00] > 10 && + buffer[01] == 0 && + fileName.ToLower().EndsWith(".spt")) + return true; + } + } + return false; + } + + public override string ToText(Subtitle subtitle, string title) + { + return "Not supported!"; + } + + public override void LoadSubtitle(Subtitle subtitle, List lines, string fileName) + { + subtitle.Paragraphs.Clear(); + byte[] buffer = File.ReadAllBytes(fileName); + + int index = buffer[0]; // go to first subtitle paragraph + while (index < buffer.Length) + { + Paragraph p = GetPacParagraph(ref index, buffer); + if (p != null) + subtitle.Paragraphs.Add(p); + } + subtitle.Renumber(1); + } + + private Paragraph GetPacParagraph(ref int index, byte[] buffer) + { + if (index + 16 + 20 + 4 >= buffer.Length) + return null; + + int textLengthFirstLine = buffer[index + 16 + 20]; + int textLengthSecondLine = buffer[index + 16 + 20 + 4]; + + try + { + + var p = new Paragraph(); + p.StartTime = GetTimeCode(Encoding.Default.GetString(buffer, index, 8)); + p.EndTime = GetTimeCode(Encoding.Default.GetString(buffer, index + 8, 8)); + + p.Text = Encoding.Default.GetString(buffer, index + 16 + 20 + 16, textLengthFirstLine); + + if (textLengthSecondLine > 0) + p.Text += Environment.NewLine + Encoding.Default.GetString(buffer, index + 16 + 20 + 16 + textLengthFirstLine, textLengthSecondLine); + + index += (16 + 20 + 16 + textLengthFirstLine + textLengthSecondLine); + return p; + } + catch + { + index += (16 + 20 + 16 + textLengthFirstLine + textLengthSecondLine); + _errorCount++; + return null; + } + } + + private TimeCode GetTimeCode(string timeCode) + { + int hour = int.Parse(timeCode.Substring(0, 2)); + int minute = int.Parse(timeCode.Substring(2, 2)); + int second = int.Parse(timeCode.Substring(4, 2)); + int frames = int.Parse(timeCode.Substring(6, 2)); + + int milliseconds = (int)((1000 / Configuration.Settings.General.CurrentFrameRate) * frames); + if (milliseconds > 999) + milliseconds = 999; + + return new TimeCode(hour, minute, second, milliseconds); + } + + } +} \ No newline at end of file diff --git a/src/Logic/Utilities.cs b/src/Logic/Utilities.cs index 2a375bcbc..6c9bdf2a0 100644 --- a/src/Logic/Utilities.cs +++ b/src/Logic/Utilities.cs @@ -1089,6 +1089,7 @@ namespace Nikse.SubtitleEdit.Logic } sb.Append("*" + new Pac().Extension + ";"); sb.Append("*" + new Cavena890().Extension + ";"); + sb.Append("*" + new Spt().Extension + ";"); sb.Append("*.sup"); sb.Append("|" + Configuration.Settings.Language.General.AllFiles + "|*.*"); return sb.ToString(); diff --git a/src/SubtitleEdit.csproj b/src/SubtitleEdit.csproj index 8e3aea621..7ec6faf79 100644 --- a/src/SubtitleEdit.csproj +++ b/src/SubtitleEdit.csproj @@ -559,6 +559,7 @@ +