diff --git a/src/Forms/Main.cs b/src/Forms/Main.cs index cf719f59b..704282749 100644 --- a/src/Forms/Main.cs +++ b/src/Forms/Main.cs @@ -757,7 +757,7 @@ namespace Nikse.SubtitleEdit.Forms var fi = new FileInfo(fileName); if (fi.Length < 1024 * 1024 && !done) // max 1 mb { - format = sub.LoadSubtitle(fileName, out encoding, null); + format = sub.LoadSubtitle(fileName, out encoding, null, true); if (format == null) { @@ -773,6 +773,7 @@ namespace Nikse.SubtitleEdit.Forms var pac = new Pac(); if (pac.IsMine(null, fileName)) { + pac.BatchMode = true; pac.LoadSubtitle(sub, null, fileName); format = pac; } @@ -931,12 +932,13 @@ namespace Nikse.SubtitleEdit.Forms if (sf.Name.ToLower().Replace(" ", string.Empty) == toFormat.ToLower() || sf.Name.ToLower().Replace(" ", string.Empty) == toFormat.Replace(" ", string.Empty).ToLower()) { targetFormatFound = true; + sf.BatchMode = true; outputFileName = FormatOutputFileNameForBatchConvert(fileName, sf.Extension, outputFolder, overwrite); Console.Write(string.Format("{0}: {1} -> {2}...", count, Path.GetFileName(fileName), outputFileName)); if (sf.IsFrameBased && !sub.WasLoadedWithFrameNumbers) sub.CalculateFrameNumbersFromTimeCodesNoCheck(Configuration.Settings.General.CurrentFrameRate); else if (sf.IsTimeBased && sub.WasLoadedWithFrameNumbers) - sub.CalculateTimeCodesFromFrameNumbers(Configuration.Settings.General.CurrentFrameRate); + sub.CalculateTimeCodesFromFrameNumbers(Configuration.Settings.General.CurrentFrameRate); File.WriteAllText(outputFileName, sub.ToText(sf), targetEncoding); if (format.GetType() == typeof(Sami) || format.GetType() == typeof(SamiModern)) { diff --git a/src/Logic/Subtitle.cs b/src/Logic/Subtitle.cs index 24f00ff16..9d15eec0a 100644 --- a/src/Logic/Subtitle.cs +++ b/src/Logic/Subtitle.cs @@ -106,6 +106,11 @@ namespace Nikse.SubtitleEdit.Logic } public SubtitleFormat LoadSubtitle(string fileName, out Encoding encoding, Encoding useThisEncoding) + { + return LoadSubtitle(fileName, out encoding, useThisEncoding, false); + } + + public SubtitleFormat LoadSubtitle(string fileName, out Encoding encoding, Encoding useThisEncoding, bool batchMode) { FileName = fileName; @@ -149,6 +154,7 @@ namespace Nikse.SubtitleEdit.Logic if (subtitleFormat.IsMine(lines, fileName)) { Header = null; + subtitleFormat.BatchMode = batchMode; subtitleFormat.LoadSubtitle(this, lines, fileName); _format = subtitleFormat; _wasLoadedWithFrameNumbers = _format.IsFrameBased; diff --git a/src/Logic/SubtitleFormats/Pac.cs b/src/Logic/SubtitleFormats/Pac.cs index 1699a6bdc..628035a3c 100644 --- a/src/Logic/SubtitleFormats/Pac.cs +++ b/src/Logic/SubtitleFormats/Pac.cs @@ -1095,6 +1095,12 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats private void GetCodePage( byte[] buffer, int index, int endDelimiter) { + if (BatchMode) + { + _codePage = -2; + return; + } + byte[] previewBuffer = null; if (buffer != null) diff --git a/src/Logic/SubtitleFormats/SubtitleFormat.cs b/src/Logic/SubtitleFormats/SubtitleFormat.cs index 7d9960c1f..103a033a0 100644 --- a/src/Logic/SubtitleFormats/SubtitleFormat.cs +++ b/src/Logic/SubtitleFormats/SubtitleFormat.cs @@ -185,6 +185,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats new UnknownSubtitle58(), new UnknownSubtitle59(), new UnknownSubtitle60(), + new UnknownSubtitle61(), }; string path = Configuration.PluginsDirectory; @@ -332,6 +333,8 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats } } + public bool BatchMode { get; set; } + public static string ToUtf8XmlString(XmlDocument xml, bool omitXmlDeclaration) { XmlWriterSettings settings = new XmlWriterSettings(); diff --git a/src/Logic/SubtitleFormats/UnknownSubtitle61.cs b/src/Logic/SubtitleFormats/UnknownSubtitle61.cs new file mode 100644 index 000000000..a0b14170b --- /dev/null +++ b/src/Logic/SubtitleFormats/UnknownSubtitle61.cs @@ -0,0 +1,152 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Text.RegularExpressions; + +namespace Nikse.SubtitleEdit.Logic.SubtitleFormats +{ + public class UnknownSubtitle61 : SubtitleFormat + { + //00:02:23.59 + //קרוליין: פשוט תשימי את זה בפייסבוק או משהו. + + //00:02:25.78 + //הם בטוח יאהבו את זה. + + //00:02:27.78 + //ליזי: אוקיי. אז אני מניחה שזה זמן הנתינה + static Regex regexTimeCodes1 = new Regex(@"^\d\d:\d\d:\d\d\.\d\d$", RegexOptions.Compiled); + + public override string Extension + { + get { return ".txt"; } + } + + public override string Name + { + get { return "Unknown 61"; } + } + + public override bool IsTimeBased + { + get { return true; } + } + + public override bool IsMine(List lines, string fileName) + { + var subtitle = new Subtitle(); + + var sb = new StringBuilder(); + foreach (string line in lines) + sb.AppendLine(line); + + LoadSubtitle(subtitle, lines, fileName); + return subtitle.Paragraphs.Count > _errorCount; + } + + public override string ToText(Subtitle subtitle, string title) + { + var sb = new StringBuilder(); + foreach (Paragraph p in subtitle.Paragraphs) + { + sb.AppendLine(EncodeTimeCode(p.StartTime)); + sb.AppendLine(EncodeTimeCode(p.EndTime)); + sb.AppendLine(Utilities.RemoveHtmlTags(p.Text)); + sb.AppendLine(); + } + return sb.ToString(); + } + + private string EncodeTimeCode(TimeCode time) + { + return string.Format("{0:00}:{1:00}:{2:00}.{3:00}", time.Hours, time.Minutes, time.Seconds, time.Milliseconds / 10); + } + + public override void LoadSubtitle(Subtitle subtitle, List lines, string fileName) + { + _errorCount = 0; + bool expectStartTime = true; + var p = new Paragraph(); + subtitle.Paragraphs.Clear(); + foreach (string line in lines) + { + string s = line.Trim(); + var match = regexTimeCodes1.Match(s); + if (match.Success && s.Length == 11) + { + if (!expectStartTime) + _errorCount++; + + if (p != null && p.StartTime.TotalMilliseconds > 0) + { + subtitle.Paragraphs.Add(p); + if (string.IsNullOrEmpty(p.Text)) + _errorCount++; + } + + p = new Paragraph(); + string[] parts = s.Split(":.".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); + if (parts.Length == 4) + { + try + { + p.StartTime = DecodeTimeCode(parts); + expectStartTime = false; + } + catch (Exception exception) + { + _errorCount++; + System.Diagnostics.Debug.WriteLine(exception.Message); + expectStartTime = true; + } + } + } + else if (line.Trim().Length > 0 && !expectStartTime) + { + p.Text = (p.Text + Environment.NewLine + line).Trim(); + if (p.Text.Length > 5000) + { + _errorCount += 10; + return; + } + } + else if (line.Trim().Length == 0) + { + expectStartTime = true; + } + } + if (p != null && p.StartTime.TotalMilliseconds > 0) + subtitle.Paragraphs.Add(p); + + bool allNullEndTime = true; + for (int i = 0; i < subtitle.Paragraphs.Count; i++) + { + p = subtitle.Paragraphs[i]; + if (p.EndTime.TotalMilliseconds != 0) + allNullEndTime = false; + + p.EndTime.TotalMilliseconds = p.StartTime.TotalMilliseconds + Utilities.GetOptimalDisplayMilliseconds(p.Text); + if (i < subtitle.Paragraphs.Count - 2 &&p.EndTime.TotalMilliseconds >= subtitle.Paragraphs[i + 1].StartTime.TotalMilliseconds) + p.EndTime.TotalMilliseconds = subtitle.Paragraphs[i + 1].StartTime.TotalMilliseconds - Configuration.Settings.General.MininumMillisecondsBetweenLines; + } + if (!allNullEndTime) + subtitle.Paragraphs.Clear(); + + subtitle.RemoveEmptyLines(); + subtitle.Renumber(1); + } + + private TimeCode DecodeTimeCode(string[] parts) + { + //00:00:07:12 + string hour = parts[0]; + string minutes = parts[1]; + string seconds = parts[2]; + string msDiv10 = parts[3]; + + return new TimeCode(int.Parse(hour), int.Parse(minutes), int.Parse(seconds), int.Parse(msDiv10) * 10); + } + + } +} + diff --git a/src/SubtitleEdit.csproj b/src/SubtitleEdit.csproj index 5c3188d24..57e2f4c92 100644 --- a/src/SubtitleEdit.csproj +++ b/src/SubtitleEdit.csproj @@ -809,6 +809,7 @@ + @@ -1426,6 +1427,7 @@ Always + Designer Always