Beispiel #1
0
        public static bool TryParse(string line, out SubtitleTiming?value)
        {
            value = null;

            var tokens = line.Replace("-->", "|").Replace(',', '.').Split('|');

            if (tokens.Length != 2)
            {
                return(false);
            }

            if (!TimeSpan.TryParse(tokens[0], out var start))
            {
                return(false);
            }

            if (!TimeSpan.TryParse(tokens[1], out var end))
            {
                return(false);
            }

            if (end < start)
            {
                return(false);
            }

            value = new SubtitleTiming
            {
                Start = start,
                End   = end,
            };

            return(true);
        }
Beispiel #2
0
        private bool Read(string srtPath)
        {
            var lines = File.ReadAllLines(srtPath);

            for (int n = 0; n < lines.Length; ++n)
            {
                var line = lines[n];
                if (string.IsNullOrEmpty(line))
                {
                    continue;
                }

                // first line in section is the subtitle number (1, 2, 3 etc)...
                if (!int.TryParse(line, out var number))
                {
                    return(false);
                }

                if (n >= lines.Length - 1)
                {
                    return(false);
                }

                line = lines[++n];

                // second line is the start and end timing of the subtitle...
                if (!SubtitleTiming.TryParse(line, out var timing))
                {
                    return(false);
                }

                if (n >= lines.Length - 1)
                {
                    return(false);
                }

                line = lines[++n];

                var text = new List <string>();
                while (!string.IsNullOrEmpty(line) && n < lines.Length - 1)
                {
                    text.Add(line.Trim());
                    line = lines[++n];
                }

                var entry = new SubtitleEntry
                {
                    Number = number,
                    Timing = timing,
                    Text   = StripHtml(text)
                };

                _subtitles.Add(entry);
            }

            return(true);
        }