Пример #1
0
        public void FramesToMilliseconds1()
        {
            Configuration.Settings.General.CurrentFrameRate = 25.0;
            var ms = SubtitleFormat.FramesToMilliseconds(1);

            Assert.AreEqual(ms, 40);
        }
        public void FramesToMilliseconds()
        {
            Configuration.Settings.General.CurrentFrameRate = 30;
            var fr = SubtitleFormat.FramesToMilliseconds(1);

            Assert.AreEqual(33, fr);
            fr = SubtitleFormat.FramesToMilliseconds(30);
            Assert.AreEqual(1000, fr);

            fr = SubtitleFormat.FramesToMillisecondsMax999(30);
            Assert.AreEqual(999, fr);
        }
Пример #3
0
 internal static double ParseHHMMSSFFToMilliseconds(string text)
 {
     string[] parts = text.Split(":,.".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
     if (parts.Length == 4)
     {
         int hours;
         int minutes;
         int seconds;
         int frames;
         if (int.TryParse(parts[0], out hours) && int.TryParse(parts[1], out minutes) && int.TryParse(parts[2], out seconds) && int.TryParse(parts[3], out frames))
         {
             TimeSpan ts = new TimeSpan(0, hours, minutes, seconds, SubtitleFormat.FramesToMilliseconds(frames));
             return(ts.TotalMilliseconds);
         }
     }
     return(0);
 }
Пример #4
0
        public void JacoSubSubtitleTest()
        {
            var          jacobSub = new JacoSub();
            var          subtitle = new Subtitle();
            const string text     = @"1:55:52.16 1:55:53.20 D [Billy] That might have been my fault.
1:55:53.20 1:55:55.13 D That might have been my fault,\nI'm so sorry.";

            // Test text.
            jacobSub.LoadSubtitle(subtitle, new List <string>(text.SplitToLines()), null);
            Assert.AreEqual("[Billy] That might have been my fault.", subtitle.Paragraphs[0].Text);
            Assert.AreEqual("That might have been my fault," + Environment.NewLine + "I'm so sorry.", subtitle.Paragraphs[1].Text);

            // Test time code.
            double expectedTotalMilliseconds = new TimeCode(1, 55, 52, SubtitleFormat.FramesToMilliseconds(16)).TotalMilliseconds;

            Assert.AreEqual(expectedTotalMilliseconds, subtitle.Paragraphs[0].StartTime.TotalMilliseconds);

            // Test total lines.
            Assert.AreEqual(2, subtitle.Paragraphs[1].NumberOfLines);
        }
Пример #5
0
        public void PhoenixSubtitleTest()
        {
            var          phxSub   = new PhoenixSubtitle();
            var          subtitle = new Subtitle();
            const string text     = @"2447,   2513, ""You should come to the Drama Club, too.""
2513,   2594, ""Yeah. The Drama Club is worried|that you haven't been coming.""
2603,   2675, """"I see. Sorry, ""I'll drop"" by next time.""""";

            // Test text.
            phxSub.LoadSubtitle(subtitle, new List <string>(text.SplitToLines()), null);
            Assert.AreEqual("You should come to the Drama Club, too.", subtitle.Paragraphs[0].Text);
            Assert.AreEqual("Yeah. The Drama Club is worried\r\nthat you haven't been coming.", subtitle.Paragraphs[1].Text);

            // Test frames.
            Assert.AreEqual(SubtitleFormat.FramesToMilliseconds(2447), SubtitleFormat.FramesToMilliseconds(subtitle.Paragraphs[0].StartFrame));
            Assert.AreEqual(SubtitleFormat.FramesToMilliseconds(2513), SubtitleFormat.FramesToMilliseconds(subtitle.Paragraphs[0].EndFrame));

            // Test total lines.
            Assert.AreEqual(2, subtitle.Paragraphs[1].NumberOfLines);

            // Test quote inside/beginning of text.
            Assert.AreEqual("\"I see. Sorry, \"I'll drop\" by next time.\"", subtitle.Paragraphs[2].Text);
        }
Пример #6
0
        private Subtitle ImportTimeCodesInFramesOnSameSeparateLine(List <string> lines)
        {
            Paragraph p        = null;
            var       subtitle = new Subtitle();
            var       sb       = new StringBuilder();

            for (int idx = 0; idx < lines.Count; idx++)
            {
                string line = lines[idx];
                string lineWithPerhapsOnlyNumbers = GetLineWithPerhapsOnlyNumbers(line);
                bool   allNumbers = lineWithPerhapsOnlyNumbers.Length > 0;
                foreach (char c in lineWithPerhapsOnlyNumbers)
                {
                    if (!char.IsDigit(c))
                    {
                        allNumbers = false;
                    }
                }
                if (allNumbers && lineWithPerhapsOnlyNumbers.Length > 2)
                {
                    string[] arr = line.Replace('-', ' ').Replace('>', ' ').Replace('{', ' ').Replace('}', ' ').Replace('[', ' ').Replace(']', ' ').Trim().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    if (arr.Length == 2)
                    {
                        string[] start = arr[0].Trim().Split(ExpectedSplitChars, StringSplitOptions.RemoveEmptyEntries);
                        string[] end   = arr[0].Trim().Split(ExpectedSplitChars, StringSplitOptions.RemoveEmptyEntries);
                        if (start.Length == 1 && end.Length == 1)
                        {
                            if (p != null)
                            {
                                p.Text = sb.ToString().Trim();
                                subtitle.Paragraphs.Add(p);
                            }
                            p = new Paragraph();
                            sb.Clear();
                            try
                            {
                                if (UseFrames)
                                {
                                    p.StartTime.TotalMilliseconds = SubtitleFormat.FramesToMilliseconds(int.Parse(start[0]));
                                    p.EndTime.TotalMilliseconds   = SubtitleFormat.FramesToMilliseconds(int.Parse(end[0]));
                                }
                                else
                                {
                                    p.StartTime.TotalMilliseconds = double.Parse(start[0]);
                                    p.EndTime.TotalMilliseconds   = double.Parse(end[0]);
                                }
                            }
                            catch
                            {
                                p = null;
                            }
                        }
                    }
                    else if (arr.Length == 3)
                    {
                        string[] start    = arr[0].Trim().Split(ExpectedSplitChars, StringSplitOptions.RemoveEmptyEntries);
                        string[] end      = arr[0].Trim().Split(ExpectedSplitChars, StringSplitOptions.RemoveEmptyEntries);
                        string[] duration = arr[0].Trim().Split(ExpectedSplitChars, StringSplitOptions.RemoveEmptyEntries);

                        if (end.Length == 1 && duration.Length == 1)
                        {
                            start = end;
                            end   = duration;
                        }

                        if (start.Length == 1 && end.Length == 1)
                        {
                            if (p != null)
                            {
                                p.Text = sb.ToString().Trim();
                                subtitle.Paragraphs.Add(p);
                            }
                            p = new Paragraph();
                            sb.Clear();
                            try
                            {
                                if (UseFrames)
                                {
                                    p.StartTime.TotalMilliseconds = SubtitleFormat.FramesToMilliseconds(int.Parse(start[0]));
                                    p.EndTime.TotalMilliseconds   = SubtitleFormat.FramesToMilliseconds(int.Parse(end[0]));
                                }
                                else
                                {
                                    p.StartTime.TotalMilliseconds = double.Parse(start[0]);
                                    p.EndTime.TotalMilliseconds   = double.Parse(end[0]);
                                }
                            }
                            catch
                            {
                                p = null;
                            }
                        }
                    }
                }
                if (p != null && !allNumbers && line.Length > 1)
                {
                    line = line.Trim();
                    if (line.StartsWith("}{}", StringComparison.Ordinal) || line.StartsWith("][]", StringComparison.Ordinal))
                    {
                        line = line.Remove(0, 3);
                    }

                    sb.AppendLine(line.Trim());
                }
            }
            if (p != null)
            {
                p.Text = sb.ToString().Trim();
                subtitle.Paragraphs.Add(p);
            }
            subtitle.Renumber();
            return(subtitle);
        }
Пример #7
0
        private Subtitle ImportTimeCodesInFramesAndTextOnSameLine(List <string> lines)
        {
            var       regexTimeCodes1 = new Regex(@"\d+", RegexOptions.Compiled);
            Paragraph p        = null;
            var       subtitle = new Subtitle();
            var       sb       = new StringBuilder();

            for (int idx = 0; idx < lines.Count; idx++)
            {
                string line = lines[idx];

                var matches = regexTimeCodes1.Matches(line);
                if (matches.Count >= 2)
                {
                    string start = matches[0].ToString();
                    string end   = matches[1].ToString();

                    if (p != null)
                    {
                        p.Text = sb.ToString().Trim();
                        subtitle.Paragraphs.Add(p);
                    }
                    p = new Paragraph();
                    sb.Clear();
                    try
                    {
                        if (UseFrames)
                        {
                            p.StartTime.TotalMilliseconds = SubtitleFormat.FramesToMilliseconds(int.Parse(start));
                            p.EndTime.TotalMilliseconds   = SubtitleFormat.FramesToMilliseconds(int.Parse(end));
                        }
                        else
                        {
                            p.StartTime.TotalMilliseconds = double.Parse(start);
                            p.EndTime.TotalMilliseconds   = double.Parse(end);
                        }
                    }
                    catch
                    {
                        p = null;
                    }

                    if (matches[0].Index < 9)
                    {
                        line = line.Remove(0, matches[0].Index);
                    }

                    line = line.Replace(matches[0].ToString(), string.Empty);
                    line = line.Replace(matches[1].ToString(), string.Empty);
                    line = line.Trim();
                    if (line.StartsWith("}{}", StringComparison.Ordinal) || line.StartsWith("][]", StringComparison.Ordinal))
                    {
                        line = line.Remove(0, 3);
                    }

                    line = line.Trim();
                }
                if (p != null && line.Length > 1)
                {
                    sb.AppendLine(line.Trim());
                    if (sb.Length > 200)
                    {
                        return(new Subtitle());
                    }
                }
            }
            if (p != null)
            {
                p.Text = sb.ToString().Trim();
                subtitle.Paragraphs.Add(p);
            }
            subtitle.Renumber();
            return(subtitle);
        }