PageRenderTime 75ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Logic/SubtitleFormats/UnknownSubtitle55.cs

https://gitlab.com/minaz922/subtitleedit
C# | 162 lines | 138 code | 22 blank | 2 comment | 20 complexity | 52117e07996be8d760e4238b2206f020 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
  6. {
  7. public class UnknownSubtitle55 : SubtitleFormat
  8. {
  9. // 338: 00:24:34.00 00:24:37.10 [51]
  10. static Regex regexTimeCodes = new Regex(@"^\d+:\s+\d\d:\d\d:\d\d\.\d\d\s+\d\d:\d\d:\d\d\.\d\d\s+\[\d+\]$", RegexOptions.Compiled);
  11. public override string Extension
  12. {
  13. get { return ".rtf"; }
  14. }
  15. public override string Name
  16. {
  17. get { return "Unknown 55"; }
  18. }
  19. public override bool IsTimeBased
  20. {
  21. get { return true; }
  22. }
  23. public override bool IsMine(List<string> lines, string fileName)
  24. {
  25. var subtitle = new Subtitle();
  26. var sb = new StringBuilder();
  27. foreach (string line in lines)
  28. sb.AppendLine(line);
  29. LoadSubtitle(subtitle, lines, fileName);
  30. return subtitle.Paragraphs.Count > _errorCount;
  31. }
  32. public override string ToText(Subtitle subtitle, string title)
  33. {
  34. string format = "{0}: {1} {2} [{3}]";
  35. var sb = new StringBuilder();
  36. int count = 1;
  37. foreach (Paragraph p in subtitle.Paragraphs)
  38. {
  39. sb.AppendLine(string.Format(format, count, EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime), p.Text.Length));
  40. sb.AppendLine(p.Text);
  41. sb.AppendLine();
  42. count++;
  43. }
  44. System.Windows.Forms.RichTextBox rtBox = new System.Windows.Forms.RichTextBox();
  45. rtBox.Text = sb.ToString();
  46. return rtBox.Rtf;
  47. }
  48. private string EncodeTimeCode(TimeCode time)
  49. {
  50. return string.Format("{0:00}:{1:00}:{2:00}.{3:00}", time.Hours, time.Minutes, time.Seconds, MillisecondsToFramesMaxFrameRate(time.Milliseconds));
  51. }
  52. public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
  53. {
  54. _errorCount = 0;
  55. var sb = new StringBuilder();
  56. foreach (string line in lines)
  57. sb.AppendLine(line);
  58. string rtf = sb.ToString().Trim();
  59. if (!rtf.StartsWith("{\\rtf"))
  60. return;
  61. var rtBox = new System.Windows.Forms.RichTextBox();
  62. try
  63. {
  64. rtBox.Rtf = rtf;
  65. }
  66. catch (Exception exception)
  67. {
  68. System.Diagnostics.Debug.WriteLine(exception.Message);
  69. return;
  70. }
  71. bool expectStartTime = true;
  72. var p = new Paragraph();
  73. subtitle.Paragraphs.Clear();
  74. foreach (string line in rtBox.Text.Replace("\r", "").Split('\n'))
  75. {
  76. string s = line.Trim().Replace("*", string.Empty);
  77. var match = regexTimeCodes.Match(s);
  78. if (match.Success)
  79. {
  80. string[] parts = s.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  81. if (parts.Length == 4)
  82. {
  83. try
  84. {
  85. if (!string.IsNullOrEmpty(p.Text))
  86. {
  87. subtitle.Paragraphs.Add(p);
  88. p = new Paragraph();
  89. }
  90. p.StartTime = DecodeTimeCode(parts[1]);
  91. p.EndTime = DecodeTimeCode(parts[2]);
  92. expectStartTime = false;
  93. }
  94. catch (Exception exception)
  95. {
  96. _errorCount++;
  97. System.Diagnostics.Debug.WriteLine(exception.Message);
  98. }
  99. }
  100. }
  101. else if (line.Trim().Length == 0)
  102. {
  103. if (p != null)
  104. {
  105. if (p.StartTime.TotalMilliseconds == 0 && p.EndTime.TotalMilliseconds == 0)
  106. _errorCount++;
  107. else
  108. subtitle.Paragraphs.Add(p);
  109. p = new Paragraph();
  110. }
  111. }
  112. else if (line.Trim().Length > 0 && !expectStartTime)
  113. {
  114. p.Text = (p.Text + Environment.NewLine + line).Trim();
  115. if (p.Text.Length > 500)
  116. {
  117. _errorCount+=10;
  118. return;
  119. }
  120. while (p.Text.Contains(Environment.NewLine + " "))
  121. p.Text = p.Text.Replace(Environment.NewLine + " ", Environment.NewLine);
  122. }
  123. }
  124. if (!string.IsNullOrEmpty(p.Text))
  125. subtitle.Paragraphs.Add(p);
  126. subtitle.RemoveEmptyLines();
  127. subtitle.Renumber(1);
  128. }
  129. private TimeCode DecodeTimeCode(string part)
  130. {
  131. string[] parts = part.Split(".:".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  132. //00:00:07:12
  133. string hour = parts[0];
  134. string minutes = parts[1];
  135. string seconds = parts[2];
  136. string frames = parts[3];
  137. return new TimeCode(int.Parse(hour), int.Parse(minutes), int.Parse(seconds), FramesToMillisecondsMax999(int.Parse(frames)));
  138. }
  139. }
  140. }