using UnityEngine; using System.Collections.Generic; //----------------------------------------------------------------------------- // Copyright 2015-2021 RenderHeads Ltd. All rights reserved. //----------------------------------------------------------------------------- namespace RenderHeads.Media.AVProVideo { public class Subtitle { public int index; // Rich string can contain etc public string text; public double timeStart, timeEnd; public bool IsBefore(double time) { return (time > timeStart && time > timeEnd); } public bool IsTime(double time) { return (time >= timeStart && time < timeEnd); } } public class SubtitlePlayer { // min time, max time // set time // event for change(subs added, subs removed) // list of subs on } public class SubtitleUtils { /// /// Parse time in format: 00:00:48,924 and convert to seconds /// private static double ParseTimeToSeconds(string text) { double result = 0.0; string[] digits = text.Split(new char[] { ':', ',' }); if (digits.Length == 4) { int hours = int.Parse(digits[0]); int minutes = int.Parse(digits[1]); int seconds = int.Parse(digits[2]); int milliseconds = int.Parse(digits[3]); result = (milliseconds / 1000.0) + (seconds + (minutes + (hours * 60)) * 60); } return result; } /// /// Parse subtitles in the SRT format and convert to a list of ordered Subtitle objects /// public static List ParseSubtitlesSRT(string data) { List result = null; if (!string.IsNullOrEmpty(data)) { data = data.Trim(); var rx = new System.Text.RegularExpressions.Regex("\n\r|\r\n|\n|\r"); string[] lines = rx.Split(data); if (lines.Length >= 3) { result = new List(256); int count = 0; int index = 0; Subtitle subtitle = null; for (int i = 0; i < lines.Length; i++) { if (index == 0) { subtitle = new Subtitle(); subtitle.index = count;// int.Parse(lines[i]); } else if (index == 1) { string[] times = lines[i].Split(new string[] { " --> " }, System.StringSplitOptions.RemoveEmptyEntries); if (times.Length == 2) { subtitle.timeStart = ParseTimeToSeconds(times[0]); subtitle.timeEnd = ParseTimeToSeconds(times[1]); } else { throw new System.FormatException("SRT format doesn't appear to be valid"); } } else { if (!string.IsNullOrEmpty(lines[i])) { if (index == 2) { subtitle.text = lines[i]; } else { subtitle.text += "\n" + lines[i]; } } } if (string.IsNullOrEmpty(lines[i]) && index > 1) { result.Add(subtitle); index = 0; count++; subtitle = null; } else { index++; } } // Handle the last one if (subtitle != null) { result.Add(subtitle); subtitle = null; } } else { Debug.LogWarning("[AVProVideo] SRT format doesn't appear to be valid"); } } return result; } } }