Subtitles.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. //-----------------------------------------------------------------------------
  4. // Copyright 2015-2021 RenderHeads Ltd. All rights reserved.
  5. //-----------------------------------------------------------------------------
  6. namespace RenderHeads.Media.AVProVideo
  7. {
  8. public class Subtitle
  9. {
  10. public int index;
  11. // Rich string can contain <font color=""> <u> etc
  12. public string text;
  13. public double timeStart, timeEnd;
  14. public bool IsBefore(double time)
  15. {
  16. return (time > timeStart && time > timeEnd);
  17. }
  18. public bool IsTime(double time)
  19. {
  20. return (time >= timeStart && time < timeEnd);
  21. }
  22. }
  23. public class SubtitlePlayer
  24. {
  25. // min time, max time
  26. // set time
  27. // event for change(subs added, subs removed)
  28. // list of subs on
  29. }
  30. public class SubtitleUtils
  31. {
  32. /// <summary>
  33. /// Parse time in format: 00:00:48,924 and convert to seconds
  34. /// </summary>
  35. private static double ParseTimeToSeconds(string text)
  36. {
  37. double result = 0.0;
  38. string[] digits = text.Split(new char[] { ':', ',' });
  39. if (digits.Length == 4)
  40. {
  41. int hours = int.Parse(digits[0]);
  42. int minutes = int.Parse(digits[1]);
  43. int seconds = int.Parse(digits[2]);
  44. int milliseconds = int.Parse(digits[3]);
  45. result = (milliseconds / 1000.0) + (seconds + (minutes + (hours * 60)) * 60);
  46. }
  47. return result;
  48. }
  49. /// <summary>
  50. /// Parse subtitles in the SRT format and convert to a list of ordered Subtitle objects
  51. /// </summary>
  52. public static List<Subtitle> ParseSubtitlesSRT(string data)
  53. {
  54. List<Subtitle> result = null;
  55. if (!string.IsNullOrEmpty(data))
  56. {
  57. data = data.Trim();
  58. var rx = new System.Text.RegularExpressions.Regex("\n\r|\r\n|\n|\r");
  59. string[] lines = rx.Split(data);
  60. if (lines.Length >= 3)
  61. {
  62. result = new List<Subtitle>(256);
  63. int count = 0;
  64. int index = 0;
  65. Subtitle subtitle = null;
  66. for (int i = 0; i < lines.Length; i++)
  67. {
  68. if (index == 0)
  69. {
  70. subtitle = new Subtitle();
  71. subtitle.index = count;// int.Parse(lines[i]);
  72. }
  73. else if (index == 1)
  74. {
  75. string[] times = lines[i].Split(new string[] { " --> " }, System.StringSplitOptions.RemoveEmptyEntries);
  76. if (times.Length == 2)
  77. {
  78. subtitle.timeStart = ParseTimeToSeconds(times[0]);
  79. subtitle.timeEnd = ParseTimeToSeconds(times[1]);
  80. }
  81. else
  82. {
  83. throw new System.FormatException("SRT format doesn't appear to be valid");
  84. }
  85. }
  86. else
  87. {
  88. if (!string.IsNullOrEmpty(lines[i]))
  89. {
  90. if (index == 2)
  91. {
  92. subtitle.text = lines[i];
  93. }
  94. else
  95. {
  96. subtitle.text += "\n" + lines[i];
  97. }
  98. }
  99. }
  100. if (string.IsNullOrEmpty(lines[i]) && index > 1)
  101. {
  102. result.Add(subtitle);
  103. index = 0;
  104. count++;
  105. subtitle = null;
  106. }
  107. else
  108. {
  109. index++;
  110. }
  111. }
  112. // Handle the last one
  113. if (subtitle != null)
  114. {
  115. result.Add(subtitle);
  116. subtitle = null;
  117. }
  118. }
  119. else
  120. {
  121. Debug.LogWarning("[AVProVideo] SRT format doesn't appear to be valid");
  122. }
  123. }
  124. return result;
  125. }
  126. }
  127. }