private static void CleanUp(Subtitle subtitle) { foreach (Paragraph p in subtitle.Paragraphs) { p.Text = p.Text.Replace("<html>", string.Empty); p.Text = p.Text.Replace("</html>", string.Empty); p.Text = p.Text.Replace("<div>", string.Empty); p.Text = p.Text.Replace("</div>", string.Empty); p.Text = p.Text.Replace("<body>", string.Empty); p.Text = p.Text.Replace("</body>", string.Empty); p.Text = p.Text.Replace("<tt>", string.Empty); p.Text = p.Text.Replace("</tt>", string.Empty); p.Text = p.Text.Replace("<tr>", string.Empty); p.Text = p.Text.Replace("</tr>", string.Empty); p.Text = p.Text.Replace("<td>", string.Empty); p.Text = p.Text.Replace("</td>", string.Empty); p.Text = p.Text.Replace("<table>", string.Empty); p.Text = p.Text.Replace("</table>", string.Empty); p.Text = p.Text.Replace("<br>", Environment.NewLine); p.Text = p.Text.Replace("<br/>", Environment.NewLine); p.Text = p.Text.Replace("<br />", Environment.NewLine); p.Text = p.Text.Replace("<", "<"); p.Text = p.Text.Replace(">", ">"); p.Text = p.Text.Replace(" ", " "); p.Text = p.Text.Replace(" ", " "); p.Text = p.Text.Replace(" ", " "); p.Text = p.Text.Replace("|", Environment.NewLine).Replace("<p>", Environment.NewLine).Replace("</p>", Environment.NewLine).Trim(); p.Text = p.Text.Replace(Environment.NewLine + Environment.NewLine, Environment.NewLine).Trim(); p.Text = p.Text.Replace(Environment.NewLine + Environment.NewLine, Environment.NewLine).Trim(); } subtitle.RemoveEmptyLines(); }
public static string AutoDetectGoogleLanguageOrNull(Subtitle subtitle) { var s = new Subtitle(subtitle); s.RemoveEmptyLines(); string languageId = AutoDetectGoogleLanguage(s.GetAllTexts(), s.Paragraphs.Count / 14); if (string.IsNullOrEmpty(languageId)) { languageId = null; } return(languageId); }
public void OkPressed(Subtitle subtitle) { WasOkPressed = true; FixedSubtitle = new Subtitle(_subtitle, false); foreach (var fix in Window.GetFixes()) { var p = FixedSubtitle.GetParagraphOrDefaultById(fix.Id); if (p != null) { p.Text = fix.After; } } FixedSubtitle.RemoveEmptyLines(); }
public void TestRemoveEmptyLines() { var sub = new Subtitle(); var p1 = new Paragraph("1", 0, 1000); var p2 = new Paragraph(" ", 1000, 2000); var p3 = new Paragraph("3", 2000, 3000); sub.Paragraphs.Add(p1); sub.Paragraphs.Add(p2); sub.Paragraphs.Add(p3); int removedCount = sub.RemoveEmptyLines(); Assert.AreEqual(removedCount, 1); Assert.AreEqual(sub.Paragraphs.Count, 2); Assert.AreEqual(sub.Paragraphs[0], p1); Assert.AreEqual(sub.Paragraphs[1], p3); }
/// <summary> /// The load subtitle. /// </summary> /// <param name="subtitle"> /// The subtitle. /// </param> /// <param name="lines"> /// The lines. /// </param> /// <param name="fileName"> /// The file name. /// </param> public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName) { bool expectStartTime = true; Paragraph p = new Paragraph(); subtitle.Paragraphs.Clear(); this._errorCount = 0; foreach (string line in lines) { string s = line.Trim(); Match match = regexTimeCodes1.Match(s); if (match.Success) { string[] parts = s.Split(':'); if (parts.Length == 4) { try { if (expectStartTime) { p.StartTime = DecodeTimeCode(parts); expectStartTime = false; } else { if (p.EndTime.TotalMilliseconds < 0.01) { this._errorCount++; } p.EndTime = DecodeTimeCode(parts); } } catch (Exception exception) { this._errorCount++; Debug.WriteLine(exception.Message); } } } else if (string.IsNullOrWhiteSpace(line)) { if (p.StartTime.TotalMilliseconds == 0 && p.EndTime.TotalMilliseconds == 0) { this._errorCount++; } else { subtitle.Paragraphs.Add(p); } p = new Paragraph(); } else { expectStartTime = true; p.Text = (p.Text + Environment.NewLine + line).Trim(); if (p.Text.Length > 500) { this._errorCount += 10; return; } } } if (p.EndTime.TotalMilliseconds > 0) { subtitle.Paragraphs.Add(p); } bool allNullEndTime = true; for (int i = 0; i < subtitle.Paragraphs.Count; i++) { if (subtitle.Paragraphs[i].EndTime.TotalMilliseconds != 0) { allNullEndTime = false; } } if (allNullEndTime) { subtitle.Paragraphs.Clear(); } subtitle.RemoveEmptyLines(); subtitle.Renumber(); }
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName) { _errorCount = 0; var sb = new StringBuilder(); foreach (string line in lines) sb.AppendLine(line); string rtf = sb.ToString().Trim(); if (!rtf.StartsWith("{\\rtf")) return; string[] arr = null; var rtBox = new System.Windows.Forms.RichTextBox(); try { rtBox.Rtf = rtf; arr = rtBox.Text.Replace("\r", "").Split('\n'); } catch (Exception exception) { System.Diagnostics.Debug.WriteLine(exception.Message); return; } finally { rtBox.Dispose(); } var p = new Paragraph(); subtitle.Paragraphs.Clear(); foreach (string line in arr) { string s = line.Trim(); if (s.StartsWith('[') && s.EndsWith('>') && s.Length > 13 && s[12] == ']') s = s.Substring(0, 13); var match = regexTimeCodes.Match(s); if (match.Success) { string[] parts = s.Replace("[", string.Empty).Replace("]", string.Empty).Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (parts.Length == 1) { try { if (!string.IsNullOrEmpty(p.Text)) { p.EndTime = DecodeTimeCode(parts[0]); subtitle.Paragraphs.Add(p); p = new Paragraph(); } else { p.StartTime = DecodeTimeCode(parts[0]); } } catch (Exception exception) { _errorCount++; System.Diagnostics.Debug.WriteLine(exception.Message); } } } else if (string.IsNullOrWhiteSpace(line)) { } else { p.Text = (p.Text + Environment.NewLine + line).Trim(); if (p.Text.Length > 500) { _errorCount += 10; return; } while (p.Text.Contains(Environment.NewLine + " ")) p.Text = p.Text.Replace(Environment.NewLine + " ", Environment.NewLine); } } if (!string.IsNullOrEmpty(p.Text)) subtitle.Paragraphs.Add(p); subtitle.RemoveEmptyLines(); subtitle.Renumber(); }
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName) { _errorCount = 0; subtitle.Paragraphs.Clear(); if (string.IsNullOrEmpty(fileName)) { return; } byte[] array; try { array = FileUtil.ReadAllBytesShared(fileName); } catch { _errorCount++; return; } if (array.Length < 100 || array[0] != 84 || array[1] != 83 || array[2] != 66 || array[3] != 52) { return; } for (int i = 0; i < array.Length - 20; i++) { if (array[i] == 84 && array[i + 1] == 73 && array[i + 2] == 84 && array[i + 3] == 76 && array[i + 8] == 84 && array[i + 9] == 73 && array[i + 10] == 77 && array[i + 11] == 69) // TITL + TIME { int endOfText = array[i + 4]; int start = array[i + 16] + array[i + 17] * 256; if (array[i + 18] != 32) start += array[i + 18] * 256 * 256; int end = array[i + 20] + array[i + 21] * 256; if (array[i + 22] != 32) end += array[i + 22] * 256 * 256; int textStart = i; while (textStart < i + endOfText && !(array[textStart] == 0x4C && array[textStart + 1] == 0x49 && array[textStart + 2] == 0x4E && array[textStart + 3] == 0x45)) // LINE { textStart++; } int length = i + endOfText - textStart - 2; textStart += 8; string text = Encoding.Default.GetString(array, textStart, length); // text = Encoding.Default.GetString(array, i + 53, endOfText - 47); text = text.Trim('\0').Replace("\0", " ").Trim(); var item = new Paragraph(text, FramesToMilliseconds(start), FramesToMilliseconds(end)); subtitle.Paragraphs.Add(item); i += endOfText + 5; } } subtitle.RemoveEmptyLines(); subtitle.Renumber(); }
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName) { this._errorCount = 0; subtitle.Paragraphs.Clear(); subtitle.Header = null; byte[] buffer = FileUtil.ReadAllBytesShared(fileName); string title = Encoding.ASCII.GetString(buffer, 82, 66); int i = 128; Encoding encoding = Encoding.GetEncoding(1252); while (i < buffer.Length - 66) { if (buffer[i] == 0xff && buffer[i + 1] == 0xff && buffer[i + 3] != 0xff && buffer[i - 1] != 0xff && buffer[i + 64] == 0xff && buffer[i + 65] == 0xff) { Paragraph p = new Paragraph(); StringBuilder sb = new StringBuilder(); int j = i + 4; while (j < i + 64) { if (buffer[j] == 0) { break; } if (buffer[j] == 0xd) { sb.AppendLine(); j += 3; } else if (buffer[j] == 0x87) { sb.Append('♪'); j++; } else { sb.Append(encoding.GetString(buffer, j, 1)); j++; } } p.Text = sb.ToString(); subtitle.Paragraphs.Add(p); i += 62; } else { i++; } } subtitle.Renumber(); if (buffer[0] == 0x43 && // CAPT.1.2 buffer[1] == 0x41 && buffer[2] == 0x50 && buffer[3] == 0x54 && buffer[4] == 0x00 && buffer[5] == 0x31 && buffer[6] == 0x2e && buffer[7] == 0x32) { i = 396; int start = i; int number = 0; while (i < buffer.Length - 66) { if (buffer[i] == 0xff && buffer[i + 1] == 0xff && buffer[i + 2] != 0xff && buffer[i + 28] == 0xff && buffer[i + 29] == 0xff && buffer[i + 30] != 0xff) { if (buffer[i + 14] == number + 1) { Paragraph p = subtitle.GetParagraphOrDefault(number); if (p != null) { p.StartTime = DecodeTimeCode(buffer, i + 18); p.EndTime = DecodeTimeCode(buffer, i + 22); number++; } i += 25; } } i++; } } else { i = 230; int countTimecodes = 0; int start = i; int lastNumber = -1; while (i < buffer.Length - 66) { if (buffer[i] == 0xff && buffer[i + 1] == 0xff && buffer[i + 2] == 0xff && buffer[i + 3] == 0xff) { int length = i - start - 2; if (length >= 10) { int count = length / 14; if (length % 14 == 10) { count++; } else { // System.Windows.Forms.MessageBox.Show("Problem at with a length of " + length.ToString() + " at file position " + (i + 2) + " which gives remainer: " + (length % 14)); if (length % 14 == 8) { count++; } } for (int k = 0; k < count; k++) { int index = start + 2 + (14 * k); int number = buffer[index] + buffer[index + 1] * 256; if (number != lastNumber + 1) { int tempNumber = buffer[index - 2] + buffer[index - 1] * 256; if (tempNumber == lastNumber + 1) { index -= 2; number = tempNumber; } } if (number > lastNumber) { lastNumber = number; Paragraph p = subtitle.GetParagraphOrDefault(number); if (p != null) { if (k < count - 1) { p.StartTime = DecodeTimeCode(buffer, index + 6); p.EndTime = DecodeTimeCode(buffer, index + 10); } else { p.StartTime = DecodeTimeCode(buffer, index + 6); } countTimecodes++; } } } } start = i + 2; i += 5; } i++; } } for (i = 0; i < subtitle.Paragraphs.Count; i++) { Paragraph p = subtitle.GetParagraphOrDefault(i); Paragraph next = subtitle.GetParagraphOrDefault(i + 1); if (next != null && p.EndTime.TotalMilliseconds == 0) { p.EndTime.TotalMilliseconds = next.StartTime.TotalMilliseconds - 1; } } for (i = 0; i < subtitle.Paragraphs.Count; i++) { Paragraph p = subtitle.GetParagraphOrDefault(i); Paragraph next = subtitle.GetParagraphOrDefault(i + 1); if (p.Duration.TotalMilliseconds <= 0 && next != null) { p.EndTime.TotalMilliseconds = next.StartTime.TotalMilliseconds - 1; } } subtitle.RemoveEmptyLines(); Paragraph last = subtitle.GetParagraphOrDefault(subtitle.Paragraphs.Count - 1); if (last != null) { last.EndTime.TotalMilliseconds = last.StartTime.TotalMilliseconds + Utilities.GetOptimalDisplayMilliseconds(last.Text); } subtitle.Renumber(); }
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName) { this._errorCount = 0; StringBuilder sb = new StringBuilder(); lines.ForEach(line => sb.AppendLine(line)); XmlDocument xml = new XmlDocument { XmlResolver = null }; xml.LoadXml(sb.ToString().Trim()); XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable); nsmgr.AddNamespace("ttaf1", xml.DocumentElement.NamespaceURI); XmlNode div = xml.DocumentElement.SelectSingleNode("//ttaf1:body", nsmgr).SelectSingleNode("ttaf1:div", nsmgr); if (div == null) { div = xml.DocumentElement.SelectSingleNode("//ttaf1:body", nsmgr).FirstChild; } Dictionary<string, string> styleDic = new Dictionary<string, string>(); foreach (XmlNode node in xml.DocumentElement.SelectNodes("//ttaf1:style", nsmgr)) { if (node.Attributes["tts:fontStyle"] != null && node.Attributes["xml:id"] != null) { styleDic.Add(node.Attributes["xml:id"].Value, node.Attributes["tts:fontStyle"].Value); } } foreach (XmlNode node in div.ChildNodes) { try { StringBuilder pText = new StringBuilder(); foreach (XmlNode innerNode in node.ChildNodes) { switch (innerNode.Name) { case "br": pText.AppendLine(); break; case "span": bool italic = false; if (innerNode.Attributes["style"] != null && styleDic.ContainsKey(innerNode.Attributes["style"].Value)) { if (styleDic[innerNode.Attributes["style"].Value].Contains("italic")) { italic = true; pText.Append("<i>"); } } if (!italic && innerNode.Attributes != null) { XmlNode fs = innerNode.Attributes.GetNamedItem("tts:fontStyle"); if (fs != null && fs.Value == "italic") { italic = true; pText.Append("<i>"); } } if (innerNode.HasChildNodes) { foreach (XmlNode innerInnerNode in innerNode.ChildNodes) { if (innerInnerNode.Name == "br") { pText.AppendLine(); } else { pText.Append(innerInnerNode.InnerText); } } } else { pText.Append(innerNode.InnerText); } if (italic) { pText.Append("</i>"); } break; case "i": pText.Append("<i>" + innerNode.InnerText + "</i>"); break; case "b": pText.Append("<b>" + innerNode.InnerText + "</b>"); break; default: pText.Append(innerNode.InnerText); break; } } string start = null; // = node.Attributes["begin"].InnerText; string end = null; // = node.Attributes["begin"].InnerText; string dur = null; // = node.Attributes["begin"].InnerText; foreach (XmlAttribute attr in node.Attributes) { if (attr.Name.EndsWith("begin", StringComparison.Ordinal)) { start = attr.InnerText; } else if (attr.Name.EndsWith("end", StringComparison.Ordinal)) { end = attr.InnerText; } else if (attr.Name.EndsWith("duration", StringComparison.Ordinal)) { dur = attr.InnerText; } } // string start = node.Attributes["begin"].InnerText; string text = pText.ToString(); text = text.Replace(Environment.NewLine + "</i>", "</i>" + Environment.NewLine); text = text.Replace("<i></i>", string.Empty).Trim(); if (end != null) { // string end = node.Attributes["end"].InnerText; double dBegin, dEnd; if (!start.Contains(':') && Utilities.CountTagInText(start, '.') == 1 && !end.Contains(':') && Utilities.CountTagInText(end, '.') == 1 && double.TryParse(start, out dBegin) && double.TryParse(end, out dEnd)) { subtitle.Paragraphs.Add(new Paragraph(text, dBegin * TimeCode.BaseUnit, dEnd * TimeCode.BaseUnit)); } else { if (start.Length == 8 && start[2] == ':' && start[5] == ':' && end.Length == 8 && end[2] == ':' && end[5] == ':') { Paragraph p = new Paragraph(); string[] parts = start.Split(':'); p.StartTime = new TimeCode(int.Parse(parts[0]), int.Parse(parts[1]), int.Parse(parts[2]), 0); parts = end.Split(':'); p.EndTime = new TimeCode(int.Parse(parts[0]), int.Parse(parts[1]), int.Parse(parts[2]), 0); p.Text = text; subtitle.Paragraphs.Add(p); } else { subtitle.Paragraphs.Add(new Paragraph(TimedText10.GetTimeCode(start, false), TimedText10.GetTimeCode(end, false), text)); } } } else if (dur != null) { TimeCode duration = TimedText10.GetTimeCode(dur, false); TimeCode startTime = TimedText10.GetTimeCode(start, false); TimeCode endTime = new TimeCode(startTime.TotalMilliseconds + duration.TotalMilliseconds); subtitle.Paragraphs.Add(new Paragraph(startTime, endTime, text)); } } catch (Exception ex) { Debug.WriteLine(ex.Message); this._errorCount++; } } subtitle.RemoveEmptyLines(); subtitle.Renumber(); }
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName) { this._errorCount = 0; Paragraph p = null; foreach (string line in lines) { if (line.StartsWith("# SUBTITLE")) { if (p != null) { subtitle.Paragraphs.Add(p); } p = new Paragraph(); } else if (p != null && line.StartsWith("# TIMEIN")) { string timeCode = line.Remove(0, 8).Trim(); if (timeCode != "--:--:--:--" && !GetTimeCode(p.StartTime, timeCode)) { this._errorCount++; } } else if (p != null && line.StartsWith("# DURATION")) { // # DURATION 01:17 AUTO string timecode = line.Remove(0, 10).Replace("AUTO", string.Empty).Trim(); if (timecode != "--:--") { string[] arr = timecode.Split(':', ' '); if (arr.Length > 1) { int sec; int frame; if (int.TryParse(arr[0], out sec) && int.TryParse(arr[1], out frame)) { p.EndTime.TotalMilliseconds = p.StartTime.TotalMilliseconds + FramesToMillisecondsMax999(frame); p.EndTime.TotalSeconds += sec; } } } } else if (p != null && line.StartsWith("# TIMEOUT")) { string timeCode = line.Remove(0, 9).Trim(); if (timeCode != "--:--:--:--" && !GetTimeCode(p.EndTime, timeCode)) { this._errorCount++; } } else if (p != null && !line.StartsWith('#')) { if (p.Text.Length > 500) { this._errorCount += 10; return; } p.Text = (p.Text + Environment.NewLine + line).Trim(); } } if (p != null) { subtitle.Paragraphs.Add(p); } subtitle.RemoveEmptyLines(); subtitle.Renumber(); }
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName) { //[01:05.99]I've been walking in the same way as I do _errorCount = 0; var header = new StringBuilder(); foreach (string line in lines) { if (line.StartsWith('[') && _timeCode.Match(line).Success) { string s = line; s = line.Substring(1, 8); string[] parts = s.Split(new[] { ':', '.' }, StringSplitOptions.RemoveEmptyEntries); if (parts.Length == 3) { try { int minutes = int.Parse(parts[0]); int seconds = int.Parse(parts[1]); int milliseconds = int.Parse(parts[2]) * 10; string text = line.Remove(0, 9).Trim().TrimStart(']').Trim(); var start = new TimeCode(0, minutes, seconds, milliseconds); var p = new Paragraph(start, new TimeCode(0, 0, 0, 0), text); subtitle.Paragraphs.Add(p); } catch { _errorCount++; } } else { _errorCount++; } } else if (line.StartsWith("[ar:")) // [ar:Lyrics artist] { if (subtitle.Paragraphs.Count < 1) header.AppendLine(line); } else if (line.StartsWith("[al:")) // [al:Album where the song is from] { if (subtitle.Paragraphs.Count < 1) header.AppendLine(line); } else if (line.StartsWith("[ti:")) // [ti:Lyrics (song) title] { if (subtitle.Paragraphs.Count < 1) header.AppendLine(line); } else if (line.StartsWith("[au:")) // [au:Creator of the Songtext] { if (subtitle.Paragraphs.Count < 1) header.AppendLine(line); } else if (line.StartsWith("[length:")) // [length:How long the song is] { if (subtitle.Paragraphs.Count < 1) header.AppendLine(line); } else if (line.StartsWith("[by:")) // [by:Creator of the LRC file] { if (subtitle.Paragraphs.Count < 1) header.AppendLine(line); } else if (!string.IsNullOrWhiteSpace(line)) { if (subtitle.Paragraphs.Count < 1) header.AppendLine(line); _errorCount++; } else if (subtitle.Paragraphs.Count < 1) { header.AppendLine(line); } } subtitle.Header = header.ToString(); int max = subtitle.Paragraphs.Count; for (int i = 0; i < max; i++) { Paragraph p = subtitle.Paragraphs[i]; while (_timeCode.Match(p.Text).Success) { string s = p.Text.Substring(1, 8); p.Text = p.Text.Remove(0, 10).Trim(); string[] parts = s.Split(new[] { ':', '.' }, StringSplitOptions.RemoveEmptyEntries); try { int minutes = int.Parse(parts[0]); int seconds = int.Parse(parts[1]); int milliseconds = int.Parse(parts[2]) * 10; string text = GetTextAfterTimeCodes(p.Text); var start = new TimeCode(0, minutes, seconds, milliseconds); var newParagraph = new Paragraph(start, new TimeCode(0, 0, 0, 0), text); subtitle.Paragraphs.Add(newParagraph); } catch { _errorCount++; } } } subtitle.Sort(Enums.SubtitleSortCriteria.StartTime); int index = 0; foreach (Paragraph p in subtitle.Paragraphs) { p.Text = Utilities.AutoBreakLine(p.Text); Paragraph next = subtitle.GetParagraphOrDefault(index + 1); if (next != null) { if (string.IsNullOrEmpty(next.Text)) { p.EndTime = new TimeCode(next.StartTime.TotalMilliseconds); } else { p.EndTime.TotalMilliseconds = next.StartTime.TotalMilliseconds - Configuration.Settings.General.MinimumMillisecondsBetweenLines; } if (p.Duration.TotalMilliseconds > Configuration.Settings.General.SubtitleMaximumDisplayMilliseconds) { double duration = Configuration.Settings.General.SubtitleMaximumDisplayMilliseconds; p.EndTime = new TimeCode(p.StartTime.TotalMilliseconds + duration); } } else { double duration = Utilities.GetOptimalDisplayMilliseconds(p.Text, 16) + 1500; p.EndTime = new TimeCode(p.StartTime.TotalMilliseconds + duration); } index++; } subtitle.RemoveEmptyLines(); subtitle.Renumber(); }
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName) { _errorCount = 0; Paragraph p = null; foreach (string line in lines) { string s = line.Trim(); if (regexTimeCodes.Match(s).Success && !UnknownSubtitle59.RegexTimeCodes.IsMatch(s)) { if (p != null && !string.IsNullOrEmpty(p.Text)) subtitle.Paragraphs.Add(p); p = new Paragraph(); try { string[] arr = s.Substring(0, 8).Split(':'); if (arr.Length == 3) { int hours = int.Parse(arr[0]); int minutes = int.Parse(arr[1]); int seconds = int.Parse(arr[2]); p.StartTime = new TimeCode(hours, minutes, seconds, 0); string text = s.Remove(0, 8).Trim(); p.Text = text; if (text.Length > 1 && Utilities.IsInteger(text.Substring(0, 2))) _errorCount++; } } catch { _errorCount++; } } else if (line.StartsWith("\t") && p != null) { if (p.Text.Length > 1000) { _errorCount += 100; return; } p.Text = (p.Text + Environment.NewLine + s).Trim(); } else if (s.Length > 0 && !Utilities.IsInteger(s)) { _errorCount++; } } if (p != null && !string.IsNullOrEmpty(p.Text)) subtitle.Paragraphs.Add(p); int index = 1; foreach (Paragraph paragraph in subtitle.Paragraphs) { Paragraph next = subtitle.GetParagraphOrDefault(index); if (next != null) { paragraph.EndTime.TotalMilliseconds = next.StartTime.TotalMilliseconds - 1; } else { paragraph.EndTime.TotalMilliseconds = paragraph.StartTime.TotalMilliseconds + Utilities.GetOptimalDisplayMilliseconds(paragraph.Text); } index++; } subtitle.RemoveEmptyLines(); }
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName) { _errorCount = 0; bool expectStartTime = true; bool expectActor = false; 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 (p.StartTime.TotalMilliseconds > 0) { subtitle.Paragraphs.Add(p); if (string.IsNullOrEmpty(p.Text)) _errorCount++; } p = new Paragraph(); string[] parts = s.Split(':'); if (parts.Length == 4) { try { p.StartTime = DecodeTimeCode(parts); expectActor = true; expectStartTime = false; } catch (Exception exception) { _errorCount++; System.Diagnostics.Debug.WriteLine(exception.Message); expectStartTime = true; } } } else if (!string.IsNullOrWhiteSpace(line) && expectActor) { if (line == line.ToUpper()) p.Actor = line; else _errorCount++; expectActor = false; } else if (!string.IsNullOrWhiteSpace(line) && !expectActor && !expectStartTime) { p.Text = (p.Text + Environment.NewLine + line).Trim(); if (p.Text.Length > 5000) { _errorCount += 10; return; } } } if (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.MinimumMillisecondsBetweenLines; } if (!allNullEndTime) subtitle.Paragraphs.Clear(); subtitle.RemoveEmptyLines(); subtitle.Renumber(); }
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName) { _errorCount = 0; Paragraph p = null; bool started = false; var header = new StringBuilder(); var text = new StringBuilder(); foreach (string line in lines) { try { if (RegexTimeCodes.Match(line).Success) { started = true; if (p != null) p.Text = text.ToString().Trim(); text = new StringBuilder(); string start = line.Substring(7, 11); string end = line.Substring(19, 11); p = new Paragraph(GetTimeCode(start), GetTimeCode(end), string.Empty); subtitle.Paragraphs.Add(p); } else if (!started) { header.AppendLine(line); } else if (p != null && p.Text.Length < 200) { text.AppendLine(line); } else { _errorCount++; } } catch { _errorCount++; } } if (p != null) p.Text = text.ToString().Trim(); subtitle.Header = header.ToString(); subtitle.RemoveEmptyLines(); subtitle.Renumber(); }
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName) { _errorCount = 0; var sb = new StringBuilder(); foreach (string s in lines) sb.Append(s); string allText = sb.ToString(); if (!allText.Contains("text_tees")) return; var times = Json.ReadArray(allText, "text_tees"); var texts = Json.ReadArray(allText, "text_content"); for (int i = 0; i < Math.Min(times.Count, texts.Count); i++) { try { string text = texts[i]; if (text.StartsWith('[')) { var textLines = Json.ReadArray("{\"text\":" + texts[i] + "}", "text"); var textSb = new StringBuilder(); foreach (string line in textLines) { string t = Json.DecodeJsonText(line); if (t.StartsWith("[\"", StringComparison.Ordinal) && t.EndsWith("\"]", StringComparison.Ordinal)) { var innerSb = new StringBuilder(); var innerTextLines = Json.ReadArray("{\"text\":" + t + "}", "text"); foreach (string innerLine in innerTextLines) { innerSb.Append(' '); innerSb.Append(innerLine); } textSb.AppendLine(innerSb.ToString().Trim()); } else { textSb.AppendLine(t); } } text = textSb.ToString().Trim(); text = text.Replace(Environment.NewLine + Environment.NewLine, Environment.NewLine); } var p = new Paragraph(text, int.Parse(times[i]), 0); if (i + 1 < times.Count) p.EndTime.TotalMilliseconds = int.Parse(times[i + 1]); subtitle.Paragraphs.Add(p); } catch { _errorCount++; } } subtitle.RemoveEmptyLines(); subtitle.Renumber(); }
/// <summary> /// The load subtitle. /// </summary> /// <param name="subtitle"> /// The subtitle. /// </param> /// <param name="lines"> /// The lines. /// </param> /// <param name="fileName"> /// The file name. /// </param> public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName) { this._errorCount = 0; StringBuilder sb = new StringBuilder(); foreach (string s in lines) { sb.Append(s); } string allText = sb.ToString(); if (!allText.Contains("\"words\"") && !allText.Contains("'words'")) { return; } List<string> words = Json.ReadArray(allText, "words"); foreach (string word in words) { List<string> elements = Json.ReadArray(word); if (elements.Count == 2) { string milliseconds = elements[0].Trim('"').Trim(); string text = elements[1].Trim(); if (text.StartsWith('"')) { text = text.Remove(0, 1); } if (text.EndsWith('"')) { text = text.Remove(text.Length - 1, 1); } long number; if (long.TryParse(milliseconds, out number)) { subtitle.Paragraphs.Add(new Paragraph(text, number, number)); } else { this._errorCount++; } } } sb = new StringBuilder(); Subtitle sub = new Subtitle(); double startMilliseconds = 0; if (subtitle.Paragraphs.Count > 0) { startMilliseconds = subtitle.Paragraphs[0].StartTime.TotalMilliseconds; } for (int i = 0; i < subtitle.Paragraphs.Count - 1; i++) { Paragraph p = subtitle.Paragraphs[i]; Paragraph next = subtitle.Paragraphs[i + 1]; Paragraph prev = subtitle.GetParagraphOrDefault(i - 1); if (sb.Length + p.Text.Length > (Configuration.Settings.General.SubtitleLineMaximumLength * 2) - 15) { // text too big Paragraph newParagraph = new Paragraph(sb.ToString(), startMilliseconds, prev.EndTime.TotalMilliseconds); sub.Paragraphs.Add(newParagraph); sb = new StringBuilder(); if (!string.IsNullOrWhiteSpace(p.Text)) { sb.Append(p.Text); startMilliseconds = p.StartTime.TotalMilliseconds; } } else if (next.StartTime.TotalMilliseconds - p.EndTime.TotalMilliseconds > 2000) { // long time to next sub if (!string.IsNullOrWhiteSpace(p.Text)) { sb.Append(' '); sb.Append(p.Text); } Paragraph newParagraph = new Paragraph(sb.ToString(), startMilliseconds, next.StartTime.TotalMilliseconds); sub.Paragraphs.Add(newParagraph); sb = new StringBuilder(); startMilliseconds = next.StartTime.TotalMilliseconds; } else if (string.IsNullOrWhiteSpace(p.Text)) { // empty text line if (string.IsNullOrWhiteSpace(next.Text) && sb.Length > 0) { Paragraph newParagraph = new Paragraph(sb.ToString(), startMilliseconds, next.StartTime.TotalMilliseconds); sub.Paragraphs.Add(newParagraph); sb = new StringBuilder(); } } else { // just add word to current sub if (sb.Length == 0) { startMilliseconds = p.StartTime.TotalMilliseconds; } if (!string.IsNullOrWhiteSpace(p.Text)) { sb.Append(' '); sb.Append(p.Text); } } } if (sb.Length > 0) { Paragraph newParagraph = new Paragraph(sb.ToString().Trim(), startMilliseconds, Utilities.GetOptimalDisplayMilliseconds(sb.ToString())); sub.Paragraphs.Add(newParagraph); } subtitle.Paragraphs.Clear(); foreach (Paragraph p in sub.Paragraphs) { p.Text = Utilities.AutoBreakLine(p.Text); subtitle.Paragraphs.Add(new Paragraph(p)); } subtitle.RemoveEmptyLines(); subtitle.Renumber(); }
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName) { _errorCount = 0; Paragraph p = null; subtitle.Paragraphs.Clear(); foreach (string line in lines) { string s = line.Trim(); var match = regexTimeCodes2.Match(s); if (match.Success) { s = s.Substring(0, match.Index + 13).Trim(); } match = regexTimeCodes1.Match(s); if (match.Success && match.Index > 13) { string text = s.Substring(0, match.Index).Trim(); string timeCode = s.Substring(match.Index).Trim(); string[] startParts = timeCode.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries); if (startParts.Length == 4) { try { p = new Paragraph(DecodeTimeCode(startParts), new TimeCode(0, 0, 0, 0), text); subtitle.Paragraphs.Add(p); } catch (Exception exception) { _errorCount++; System.Diagnostics.Debug.WriteLine(exception.Message); } } } else if (string.IsNullOrWhiteSpace(line) || regexTimeCodes1.IsMatch(" " + s)) { // skip empty lines } else if (!string.IsNullOrWhiteSpace(line) && p != null) { _errorCount++; } } for (int i = 0; i < subtitle.Paragraphs.Count; i++) { Paragraph current = subtitle.Paragraphs[i]; Paragraph next = subtitle.GetParagraphOrDefault(i + 1); if (next != null) current.EndTime.TotalMilliseconds = next.StartTime.TotalMilliseconds - Configuration.Settings.General.MinimumMillisecondsBetweenLines; else current.EndTime.TotalMilliseconds = current.StartTime.TotalMilliseconds + Utilities.GetOptimalDisplayMilliseconds(current.Text); if (current.Duration.TotalMilliseconds > Configuration.Settings.General.SubtitleMaximumDisplayMilliseconds) current.EndTime.TotalMilliseconds = current.StartTime.TotalMilliseconds + Configuration.Settings.General.SubtitleMaximumDisplayMilliseconds; } subtitle.RemoveEmptyLines(); subtitle.Renumber(); }
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName) { this._errorCount = 0; subtitle.Paragraphs.Clear(); foreach (string line in lines) { string s = line.Trim(); Match match = regexTimeCodes1.Match(s); if (match.Success) { Paragraph p = new Paragraph(); try { p.StartTime = DecodeTimeCode(s.Substring(0, 11)); p.Text = GetText(line.Remove(0, 11)); subtitle.Paragraphs.Add(p); } catch (Exception exception) { this._errorCount++; Debug.WriteLine(exception.Message); } } else { this._errorCount++; } for (int i = 0; i < subtitle.Paragraphs.Count - 1; i++) { Paragraph p2 = subtitle.Paragraphs[i]; Paragraph next = subtitle.Paragraphs[i + 1]; p2.EndTime.TotalMilliseconds = next.StartTime.TotalMilliseconds - Configuration.Settings.General.MinimumMillisecondsBetweenLines; } if (subtitle.Paragraphs.Count > 0) { subtitle.Paragraphs[subtitle.Paragraphs.Count - 1].EndTime.TotalMilliseconds = subtitle.Paragraphs[subtitle.Paragraphs.Count - 1].StartTime.TotalMilliseconds + Utilities.GetOptimalDisplayMilliseconds(subtitle.Paragraphs[subtitle.Paragraphs.Count - 1].Text); } } subtitle.RemoveEmptyLines(); subtitle.Renumber(); }
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName) { _errorCount = 0; var sb = new StringBuilder(); foreach (string line in lines) sb.AppendLine(line); string rtf = sb.ToString().Trim(); if (!rtf.StartsWith("{\\rtf")) return; System.Windows.Forms.RichTextBox rtBox = null; try { rtBox = new System.Windows.Forms.RichTextBox { Rtf = rtf }; lines = new List<string>(rtBox.Text.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None)); } catch (ArgumentException) { // Invalid format return; } finally { if (rtBox != null) { rtBox.Dispose(); } } _errorCount = 0; Paragraph p = null; foreach (string line in lines) { string s = line.TrimEnd(); if (RegexTimeCode1.IsMatch(s)) { try { if (p != null) subtitle.Paragraphs.Add(p); string[] arr = s.Split('\t'); if (arr.Length > 2) p = new Paragraph(DecodeTimeCode(arr[1]), new TimeCode(0, 0, 0, 0), arr[2].Trim()); else p = new Paragraph(DecodeTimeCode(arr[1]), new TimeCode(0, 0, 0, 0), string.Empty); } catch { _errorCount++; p = null; } } else if (s.StartsWith("\t\t")) { if (p != null) p.Text = p.Text + Environment.NewLine + s.Trim(); } else if (!string.IsNullOrWhiteSpace(s)) { _errorCount++; } } if (p != null) subtitle.Paragraphs.Add(p); for (int j = 0; j < subtitle.Paragraphs.Count - 1; j++) { p = subtitle.Paragraphs[j]; Paragraph next = subtitle.Paragraphs[j + 1]; p.EndTime.TotalMilliseconds = next.StartTime.TotalMilliseconds - Configuration.Settings.General.MinimumMillisecondsBetweenLines; } if (subtitle.Paragraphs.Count > 0) { p = subtitle.Paragraphs[subtitle.Paragraphs.Count - 1]; p.EndTime.TotalMilliseconds = p.StartTime.TotalMilliseconds + Utilities.GetOptimalDisplayMilliseconds(p.Text); } subtitle.RemoveEmptyLines(); subtitle.Renumber(); }
public override void LoadSubtitle(Subtitle subtitle, List<string> 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) { string[] parts = s.Split('\t'); if (parts.Length == 3) { try { if (!string.IsNullOrEmpty(p.Text)) { subtitle.Paragraphs.Add(p); p = new Paragraph(); } p.StartTime = DecodeTimeCode(parts[1]); p.EndTime = DecodeTimeCode(parts[2]); expectStartTime = false; } catch (Exception exception) { _errorCount++; System.Diagnostics.Debug.WriteLine(exception.Message); } } } else if (string.IsNullOrWhiteSpace(line)) { if (p.StartTime.TotalMilliseconds == 0 && p.EndTime.TotalMilliseconds == 0) _errorCount++; else subtitle.Paragraphs.Add(p); p = new Paragraph(); } else if (!expectStartTime) { p.Text = (p.Text + Environment.NewLine + line).Trim(); if (p.Text.Length > 500) { _errorCount += 10; return; } while (p.Text.Contains(Environment.NewLine + " ")) p.Text = p.Text.Replace(Environment.NewLine + " ", Environment.NewLine); } } if (p.EndTime.TotalMilliseconds > 0) subtitle.Paragraphs.Add(p); foreach (Paragraph temp in subtitle.Paragraphs) temp.Text = temp.Text.Replace("<", "@ITALIC_START").Replace(">", "</i>").Replace("@ITALIC_START", "<i>"); subtitle.RemoveEmptyLines(); subtitle.Renumber(); }
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName) { this._errorCount = 0; Paragraph p = null; foreach (string line in lines) { string s = line.Trim(); if (s.Length > 4 && s[2] == ':' && regexTimeCodes.Match(s).Success) { if (p != null && !string.IsNullOrEmpty(p.Text)) { subtitle.Paragraphs.Add(p); } p = new Paragraph(); try { string[] arr = s.Substring(0, 8).Split(':'); if (arr.Length == 3) { int hours = int.Parse(arr[0]); int minutes = int.Parse(arr[1]); int seconds = int.Parse(arr[2]); p.StartTime = new TimeCode(hours, minutes, seconds, 0); string text = s.Remove(0, 12).Trim(); p.Text = text; } } catch { this._errorCount++; } } else if (p != null && regexNumberAndText.Match(s).Success) { if (p.Text.Length > 1000) { this._errorCount += 100; return; } string text = s.Remove(0, 2).Trim(); p.Text = (p.Text + Environment.NewLine + text).Trim(); } else if (s.Length > 0 && !Utilities.IsInteger(s)) { this._errorCount++; } } if (p != null && !string.IsNullOrEmpty(p.Text)) { subtitle.Paragraphs.Add(p); } int index = 1; foreach (Paragraph paragraph in subtitle.Paragraphs) { Paragraph next = subtitle.GetParagraphOrDefault(index); if (next != null) { paragraph.EndTime.TotalMilliseconds = next.StartTime.TotalMilliseconds - 1; } index++; } subtitle.RemoveEmptyLines(); subtitle.Renumber(); }
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName) { _errorCount = 0; var sb = new StringBuilder(); lines.ForEach(line => sb.AppendLine(line)); var xml = new XmlDocument(); xml.XmlResolver = null; xml.LoadXml(sb.ToString().Trim()); var nsmgr = new XmlNamespaceManager(xml.NameTable); nsmgr.AddNamespace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main"); foreach (XmlNode node in xml.DocumentElement.SelectNodes("//w:tr", nsmgr)) { try { Paragraph p = new Paragraph(); XmlNode t = node.SelectSingleNode("w:tc/w:p/w:r/w:t", nsmgr); if (t != null) { p.StartTime = GetTimeCode(t.InnerText); sb = new StringBuilder(); foreach (XmlNode wrNode in node.SelectNodes("w:tc/w:p/w:r", nsmgr)) { foreach (XmlNode child in wrNode.ChildNodes) { if (child.Name == "w:t") { bool isTimeCode = child.InnerText.Length == 11 && child.InnerText.Replace(":", string.Empty).Length == 8; if (!isTimeCode) sb.Append(child.InnerText); } else if (child.Name == "w:br") { sb.AppendLine(); } } } p.Text = sb.ToString(); subtitle.Paragraphs.Add(p); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); _errorCount++; } } for (int i = 0; i < subtitle.Paragraphs.Count - 1; i++) { subtitle.Paragraphs[i].EndTime.TotalMilliseconds = subtitle.Paragraphs[i + 1].StartTime.TotalMilliseconds; } subtitle.Paragraphs[subtitle.Paragraphs.Count - 1].EndTime.TotalMilliseconds = 2500; subtitle.RemoveEmptyLines(); for (int i = 0; i < subtitle.Paragraphs.Count - 1; i++) { if (subtitle.Paragraphs[i].EndTime.TotalMilliseconds == subtitle.Paragraphs[i + 1].StartTime.TotalMilliseconds) subtitle.Paragraphs[i].EndTime.TotalMilliseconds = subtitle.Paragraphs[i + 1].StartTime.TotalMilliseconds - 1; } subtitle.Renumber(); }
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName) { this._errorCount = 0; StringBuilder sb = new StringBuilder(); foreach (string line in lines) { sb.AppendLine(line); } string rtf = sb.ToString().Trim(); if (!rtf.StartsWith("{\\rtf")) { return; } string text = string.Empty; RichTextBox rtBox = new RichTextBox(); try { rtBox.Rtf = rtf; text = rtBox.Text.Replace("\r\n", "\n"); } catch (Exception exception) { Debug.WriteLine(exception.Message); return; } finally { rtBox.Dispose(); } lines = new List<string>(); foreach (string line in text.Split('\n')) { lines.Add(line); } this._errorCount = 0; Paragraph p = null; foreach (string line in lines) { string s = line.TrimEnd(); if (RegexTimeCode1.IsMatch(s)) { try { if (p != null) { subtitle.Paragraphs.Add(p); } string[] arr = s.Split('\t'); if (arr.Length > 2) { p = new Paragraph(DecodeTimeCode(arr[1]), new TimeCode(0, 0, 0, 0), arr[2].Trim()); } else { p = new Paragraph(DecodeTimeCode(arr[1]), new TimeCode(0, 0, 0, 0), string.Empty); } } catch { this._errorCount++; p = null; } } else if (s.StartsWith("\t\t")) { if (p != null) { p.Text = p.Text + Environment.NewLine + s.Trim(); } } else if (!string.IsNullOrWhiteSpace(s)) { this._errorCount++; } } if (p != null) { subtitle.Paragraphs.Add(p); } for (int j = 0; j < subtitle.Paragraphs.Count - 1; j++) { p = subtitle.Paragraphs[j]; Paragraph next = subtitle.Paragraphs[j + 1]; p.EndTime.TotalMilliseconds = next.StartTime.TotalMilliseconds - Configuration.Settings.General.MinimumMillisecondsBetweenLines; } if (subtitle.Paragraphs.Count > 0) { p = subtitle.Paragraphs[subtitle.Paragraphs.Count - 1]; p.EndTime.TotalMilliseconds = p.StartTime.TotalMilliseconds + Utilities.GetOptimalDisplayMilliseconds(p.Text); } subtitle.RemoveEmptyLines(); subtitle.Renumber(); }
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName) { this._errorCount = 0; Paragraph p = null; foreach (string line in lines) { string s = line.Trim(); if (regexTimeCodes.Match(s).Success) { if (p != null && !string.IsNullOrEmpty(p.Text)) { subtitle.Paragraphs.Add(p); } p = new Paragraph(); try { string[] arr = s.Substring(0, 11).Split(':'); if (arr.Length == 4) { int hours = int.Parse(arr[0]); int minutes = int.Parse(arr[1]); int seconds = int.Parse(arr[2]); int frames = int.Parse(arr[3]); p.StartTime = new TimeCode(hours, minutes, seconds, FramesToMillisecondsMax999(frames)); string text = s.Remove(0, 11).Trim(); p.Text = text; if (text.Length > 1 && Utilities.IsInteger(text.Substring(0, 2))) { this._errorCount++; } } } catch { this._errorCount++; } } else if (s.Length > 0) { this._errorCount++; } } if (p != null && !string.IsNullOrEmpty(p.Text)) { subtitle.Paragraphs.Add(p); } int index = 1; foreach (Paragraph paragraph in subtitle.Paragraphs) { paragraph.Text = paragraph.Text.Replace("\\M", "♪"); Paragraph next = subtitle.GetParagraphOrDefault(index); if (next != null) { paragraph.EndTime.TotalMilliseconds = next.StartTime.TotalMilliseconds - 1; } else { paragraph.EndTime.TotalMilliseconds = paragraph.StartTime.TotalMilliseconds + Utilities.GetOptimalDisplayMilliseconds(paragraph.Text); } index++; } subtitle.RemoveEmptyLines(); }