/src/Logic/SubtitleFormats/UnknownSubtitle45.cs

https://gitlab.com/minaz922/subtitleedit · C# · 141 lines · 113 code · 17 blank · 11 comment · 23 complexity · 98d178d0da67fd2714356fc1fb000d34 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 UnknownSubtitle45 : SubtitleFormat
  8. {
  9. //* 00001.00-00003.00 02.01 00.0 1 0001 00 16-090-090
  10. //* 00138.10-00143.05 00.00 00.0 1 0003 00 16-090-090
  11. static Regex regexTimeCodes = new Regex(@"^\*\s+\d\d\d\d\d\.\d\d-\d\d\d\d\d\.\d\d \d\d.\d\d \d\d.\d\ \d \d\d\d\d \d\d \d\d-\d\d\d-\d\d\d$", RegexOptions.Compiled);
  12. public override string Extension
  13. {
  14. get { return ".rtf"; }
  15. }
  16. public override string Name
  17. {
  18. get { return "Unknown 45"; }
  19. }
  20. public override bool IsTimeBased
  21. {
  22. get { return true; }
  23. }
  24. public override bool IsMine(List<string> lines, string fileName)
  25. {
  26. if (fileName != null && !fileName.ToLower().EndsWith(Extension))
  27. return false;
  28. var subtitle = new Subtitle();
  29. LoadSubtitle(subtitle, lines, fileName);
  30. return subtitle.Paragraphs.Count > _errorCount;
  31. }
  32. public override string ToText(Subtitle subtitle, string title)
  33. {
  34. var sb = new StringBuilder();
  35. sb.AppendLine(@"0 2 1.0 1.0 3.0 048 0400 0040 0500 100 100 0 100 0 6600 6600 01
  36. CRULIC R1
  37. ST 0 EB 3.10
  38. @");
  39. int index = 0;
  40. foreach (Paragraph p in subtitle.Paragraphs)
  41. {
  42. //1 00:50:34:22 00:50:39:13
  43. //Ich muss dafür sorgen,
  44. //dass die Epsteins weiterleben
  45. index++;
  46. sb.AppendLine(string.Format("* {0}-{1} 00.00 00.0 1 {2} 00 16-090-090{3}{4}{3}@", EncodeTimeCode(p.StartTime), EncodeTimeCode(p.EndTime), index.ToString().PadLeft(4, '0'), Environment.NewLine, Utilities.RemoveHtmlTags(p.Text)));
  47. }
  48. System.Windows.Forms.RichTextBox rtBox = new System.Windows.Forms.RichTextBox();
  49. rtBox.Text = sb.ToString();
  50. return rtBox.Rtf;
  51. }
  52. private string EncodeTimeCode(TimeCode time)
  53. {
  54. return string.Format("{0:00000}.{1:00}", time.TotalSeconds, MillisecondsToFramesMaxFrameRate(time.Milliseconds));
  55. }
  56. public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
  57. {
  58. //* 00001.00-00003.00 02.01 00.0 1 0001 00 16-090-090
  59. //CRULIC R1
  60. //pour Bobi
  61. //@
  62. _errorCount = 0;
  63. var sb = new StringBuilder();
  64. foreach (string line in lines)
  65. sb.AppendLine(line);
  66. string rtf = sb.ToString().Trim();
  67. if (!rtf.StartsWith("{\\rtf"))
  68. return;
  69. var rtBox = new System.Windows.Forms.RichTextBox();
  70. try
  71. {
  72. rtBox.Rtf = rtf;
  73. }
  74. catch (Exception exception)
  75. {
  76. System.Diagnostics.Debug.WriteLine(exception.Message);
  77. return;
  78. }
  79. Paragraph p = null;
  80. subtitle.Paragraphs.Clear();
  81. foreach (string line in rtBox.Text.Replace("\r\n", "\n").Split('\n'))
  82. {
  83. if (regexTimeCodes.IsMatch(line.Trim()))
  84. {
  85. string[] temp = line.Substring(1).Trim().Substring(0, 17).Split('-');
  86. if (temp.Length == 2)
  87. {
  88. string start = temp[0];
  89. string end = temp[1];
  90. string[] startParts = start.Split(".".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  91. string[] endParts = end.Split(".".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  92. if (startParts.Length == 2 && endParts.Length == 2)
  93. {
  94. p = new Paragraph(DecodeTimeCode(startParts), DecodeTimeCode(endParts), string.Empty);
  95. subtitle.Paragraphs.Add(p);
  96. }
  97. }
  98. }
  99. else if (line.Trim().Length == 0 || line.Trim() == "@")
  100. {
  101. // skip these lines
  102. }
  103. else if (line.Trim().Length > 0 && p != null)
  104. {
  105. if (p.Text.Length > 2000)
  106. return; // wrong format
  107. else if (string.IsNullOrEmpty(p.Text))
  108. p.Text = line;
  109. else
  110. p.Text = p.Text + Environment.NewLine + line;
  111. }
  112. }
  113. subtitle.Renumber(1);
  114. }
  115. private TimeCode DecodeTimeCode(string[] parts)
  116. {
  117. //00119.12
  118. string seconds = parts[0];
  119. string frames = parts[1];
  120. TimeCode tc = new TimeCode(0, 0, int.Parse(seconds), FramesToMillisecondsMax999(int.Parse(frames)));
  121. return tc;
  122. }
  123. }
  124. }