Пример #1
0
        public void WriteStyle()
        {
            StringBuilder sb = new StringBuilder();

            using (var writer = new StringWriter(sb))
            {
                WebVttSerializer.SerializeAsync(
                    new BaseBlock[]
                {
                    Style.Create(@"::cue {
  background-image: linear-gradient(to bottom, dimgray, lightgray);
  color: papayawhip;
}")
                },
                    writer)
                .ConfigureAwait(false).GetAwaiter().GetResult();

                string expected = @"WEBVTT

STYLE
::cue {
  background-image: linear-gradient(to bottom, dimgray, lightgray);
  color: papayawhip;
}
";

                Assert.AreEqual(expected, sb.ToString());
            }
        }
Пример #2
0
        public void WriteMultiLineCue()
        {
            StringBuilder sb = new StringBuilder();

            using (var writer = new StringWriter(sb))
            {
                WebVttSerializer.SerializeAsync(
                    new BaseBlock[]
                {
                    new Cue()
                    {
                        Start   = TimeSpan.FromSeconds(10),
                        End     = TimeSpan.FromSeconds(20),
                        Content = new Span[]
                        {
                            new Span()
                            {
                                Type = SpanType.Text, Text = "First line."
                            },
                            new Span()
                            {
                                Type = SpanType.Text, Text = "Second line."
                            },
                            new Span()
                            {
                                Type = SpanType.Voice, Annotation = "Voice", Children = new Span[] { new Span()
                                                                                                     {
                                                                                                         Type = SpanType.Text, Text = "Voice text."
                                                                                                     } }
                            },
                            new Span()
                            {
                                Type = SpanType.Ruby, Children = new Span[] { new Span()
                                                                              {
                                                                                  Type = SpanType.RubyText, Children = new Span[] { new Span()
                                                                                                                                    {
                                                                                                                                        Type = SpanType.Text, Text = "Ruby text"
                                                                                                                                    } }
                                                                              } }
                            },
                        }
                    }
                },
                    writer)
                .ConfigureAwait(false).GetAwaiter().GetResult();

                string expected = @"WEBVTT

00:00:10.000 --> 00:00:20.000
First line.
Second line.
<v Voice>Voice text.</v>
<ruby><rt>Ruby text</rt></ruby>
";

                Assert.AreEqual(expected, sb.ToString());
            }
        }
Пример #3
0
        public void IfNullBlock_Throws()
        {
            StringBuilder sb = new StringBuilder();

            using (var writer = new StringWriter(sb))
            {
                WebVttSerializer.SerializeAsync(new BaseBlock[] { null }, writer)
                .ConfigureAwait(false).GetAwaiter().GetResult();
            }
        }
Пример #4
0
        public void WriteEmptyContent()
        {
            StringBuilder sb = new StringBuilder();

            using (var writer = new StringWriter(sb))
            {
                WebVttSerializer.SerializeAsync(new BaseBlock[0], writer).ConfigureAwait(false).GetAwaiter().GetResult();

                string expected = @"WEBVTT
";

                Assert.AreEqual(expected, sb.ToString());
            }
        }
Пример #5
0
        public void IfStyleWithArrow_Throws()
        {
            StringBuilder sb = new StringBuilder();

            using (var writer = new StringWriter(sb))
            {
                WebVttSerializer.SerializeAsync(
                    new BaseBlock[]
                {
                    Style.Create("Styles with --> are not allowed.")
                },
                    writer)
                .ConfigureAwait(false).GetAwaiter().GetResult();
            }
        }
Пример #6
0
        public void IfCueRegionNameHasWhiteSpace_Throws()
        {
            StringBuilder sb = new StringBuilder();

            using (var writer = new StringWriter(sb))
            {
                WebVttSerializer.SerializeAsync(
                    new BaseBlock[]
                {
                    new Cue()
                    {
                        Start = TimeSpan.FromSeconds(1), End = TimeSpan.FromSeconds(2), Region = "Region with space"
                    },
                },
                    writer)
                .ConfigureAwait(false).GetAwaiter().GetResult();
            }
        }
Пример #7
0
        public void IfCueEndTimeLessThanStartTime_Throws()
        {
            StringBuilder sb = new StringBuilder();

            using (var writer = new StringWriter(sb))
            {
                WebVttSerializer.SerializeAsync(
                    new BaseBlock[]
                {
                    new Cue()
                    {
                        Start = TimeSpan.FromSeconds(10), End = TimeSpan.FromSeconds(2)
                    },
                },
                    writer)
                .ConfigureAwait(false).GetAwaiter().GetResult();
            }
        }
Пример #8
0
        public void IfRegionAfterCue_Throws()
        {
            StringBuilder sb = new StringBuilder();

            using (var writer = new StringWriter(sb))
            {
                WebVttSerializer.SerializeAsync(
                    new BaseBlock[]
                {
                    new Cue()
                    {
                        Start = TimeSpan.FromSeconds(0), End = TimeSpan.FromSeconds(1)
                    },
                    new RegionDefinition()
                },
                    writer)
                .ConfigureAwait(false).GetAwaiter().GetResult();
            }
        }
Пример #9
0
        public void WriteInlineComment()
        {
            StringBuilder sb = new StringBuilder();

            using (var writer = new StringWriter(sb))
            {
                WebVttSerializer.SerializeAsync(
                    new BaseBlock[] { Comment.Create("Test inline comment.") },
                    writer)
                .ConfigureAwait(false).GetAwaiter().GetResult();

                string expected = @"WEBVTT

NOTE Test inline comment.
";

                Assert.AreEqual(expected, sb.ToString());
            }
        }
Пример #10
0
        public void WriteRegion()
        {
            StringBuilder sb = new StringBuilder();

            using (var writer = new StringWriter(sb))
            {
                WebVttSerializer.SerializeAsync(
                    new BaseBlock[]
                {
                    new RegionDefinition()
                    {
                        Id           = "Id",
                        Lines        = 5,
                        RegionAnchor = new Anchor()
                        {
                            XPercent = 1.2, YPercent = 15.375
                        },
                        ViewPortAnchor = new Anchor()
                        {
                            XPercent = 99.99, YPercent = 0.01
                        },
                        WidthPercent = 98.76,
                        Scroll       = true
                    }
                },
                    writer)
                .ConfigureAwait(false).GetAwaiter().GetResult();

                string expected = @"WEBVTT

REGION
id:Id
lines:5
width:98.76%
regionanchor:1.2%,15.375%
viewportanchor:99.99%,0.01%
scroll:up
";

                Assert.AreEqual(expected, sb.ToString());
            }
        }
Пример #11
0
        public void IfCueVerticalAlignmentInvalid_Throws()
        {
            StringBuilder sb = new StringBuilder();

            using (var writer = new StringWriter(sb))
            {
                WebVttSerializer.SerializeAsync(
                    new BaseBlock[]
                {
                    new Cue()
                    {
                        Start    = TimeSpan.FromSeconds(1),
                        End      = TimeSpan.FromSeconds(2),
                        Vertical = (VerticalTextLayout)5678
                    },
                },
                    writer)
                .ConfigureAwait(false).GetAwaiter().GetResult();
            }
        }
Пример #12
0
        public void IfCueWithNullSpan_Throws()
        {
            StringBuilder sb = new StringBuilder();

            using (var writer = new StringWriter(sb))
            {
                WebVttSerializer.SerializeAsync(
                    new BaseBlock[]
                {
                    new Cue()
                    {
                        Start   = TimeSpan.FromSeconds(1),
                        End     = TimeSpan.FromSeconds(2),
                        Content = new Span[] { null }
                    },
                },
                    writer)
                .ConfigureAwait(false).GetAwaiter().GetResult();
            }
        }
Пример #13
0
        public void IfCueWithInvalidLine_Throws()
        {
            StringBuilder sb = new StringBuilder();

            using (var writer = new StringWriter(sb))
            {
                WebVttSerializer.SerializeAsync(
                    new BaseBlock[]
                {
                    new Cue()
                    {
                        Start = TimeSpan.FromSeconds(1),
                        End   = TimeSpan.FromSeconds(2),
                        Line  = new LineSettings()
                    },
                },
                    writer)
                .ConfigureAwait(false).GetAwaiter().GetResult();
            }
        }
Пример #14
0
        public void WriteChapters()
        {
            using (var reader = new StreamReader(new MemoryStream(Properties.Resources.SampleCaption, false)))
            {
                var captions = WebVttParser.ReadMediaCaptionsAsync(reader).ConfigureAwait(false).GetAwaiter().GetResult();

                StringBuilder sb = new StringBuilder();
                using (var writer = new StringWriter(sb))
                {
                    WebVttSerializer.SerializeAsync(captions, writer).ConfigureAwait(false).GetAwaiter().GetResult();
                }

                using (var reader2 = new StringReader(sb.ToString()))
                {
                    var captions2 = WebVttParser.ReadMediaCaptionsAsync(reader2).ConfigureAwait(false).GetAwaiter().GetResult();

                    Assert.IsNotNull(captions2.Cues, "Cues are null after serialization.");
                    Assert.AreEqual(captions.Cues.Length, captions2.Cues.Length, "Cue counts do not match.");

                    Assert.IsNull(captions2.Regions, "Regions are not null after serialization.");
                }
            }
        }
Пример #15
0
        public void IfCuePositionAlignmentInvalid_Throws()
        {
            StringBuilder sb = new StringBuilder();

            using (var writer = new StringWriter(sb))
            {
                WebVttSerializer.SerializeAsync(
                    new BaseBlock[]
                {
                    new Cue()
                    {
                        Start    = TimeSpan.FromSeconds(1),
                        End      = TimeSpan.FromSeconds(2),
                        Position = new PositionSettings()
                        {
                            Alignment = (PositionAlignment)4321
                        }
                    },
                },
                    writer)
                .ConfigureAwait(false).GetAwaiter().GetResult();
            }
        }
Пример #16
0
        public void IfCueSpanClassHasWhiteSpace_Throws()
        {
            StringBuilder sb = new StringBuilder();

            using (var writer = new StringWriter(sb))
            {
                WebVttSerializer.SerializeAsync(
                    new BaseBlock[]
                {
                    new Cue()
                    {
                        Start   = TimeSpan.FromSeconds(1),
                        End     = TimeSpan.FromSeconds(2),
                        Content = new Span[] { new Span()
                                               {
                                                   Type = SpanType.Italics, Classes = new string[] { "White\tspace not\rallowed\n" }
                                               } }
                    },
                },
                    writer)
                .ConfigureAwait(false).GetAwaiter().GetResult();
            }
        }
Пример #17
0
        public void IfCueTextHasArrow_Throws()
        {
            StringBuilder sb = new StringBuilder();

            using (var writer = new StringWriter(sb))
            {
                WebVttSerializer.SerializeAsync(
                    new BaseBlock[]
                {
                    new Cue()
                    {
                        Start   = TimeSpan.FromSeconds(1),
                        End     = TimeSpan.FromSeconds(2),
                        Content = new Span[] { new Span()
                                               {
                                                   Type = SpanType.Text, Text = "Text with --> is not allowed."
                                               } }
                    },
                },
                    writer)
                .ConfigureAwait(false).GetAwaiter().GetResult();
            }
        }
Пример #18
0
        static int Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("Please enter a translate.it file path");
                return(1);
            }

            string[] lines;
            try
            {
                lines = System.IO.File.ReadAllLines(args[0]);
            }
            catch (Exception)
            {
                Console.WriteLine("File not found");
                return(1);
            }

            MediaCaptions transcript  = new MediaCaptions();
            List <Cue>    cueList     = new List <Cue>();
            string        header      = "Presenter @ ";
            string        translation = "Translation";
            TimeSpan      origin      = new TimeSpan();
            TimeSpan      currentCaption;
            bool          firstCaption = true;
            Cue           phrase       = new Cue();

            foreach (string line in lines)
            {
                // Assumption: the translation always have Presenter / Translation / Recognition lines with the format:

                /*
                 *  Presenter @ 10:28:04
                 *
                 *  Translation (English): It's in English, too.
                 *
                 *  Recognition (Spanish): (Está en inglés, también.)
                 */

                // get time from Caption
                if (line.Contains(header) && firstCaption == false)
                {
                    currentCaption = TimeSpan.Parse(line.Substring(translation.Length));
                    TimeSpan diff = currentCaption.Subtract(origin);
                    phrase.Start = TimeSpan.FromSeconds(diff.TotalSeconds);
                    phrase.End   = phrase.Start.Add(TimeSpan.FromSeconds(5));
                }

                // get baseline Time from first Caption
                if (line.Contains(header) && firstCaption == true)
                {
                    origin = TimeSpan.Parse(line.Substring(header.Length));
                    TimeSpan diff = origin.Subtract(origin);
                    phrase.Start = TimeSpan.FromSeconds(diff.TotalSeconds);
                    phrase.End   = phrase.Start.Add(TimeSpan.FromSeconds(5));
                    firstCaption = false;
                }

                // get text from Caption
                if (line.StartsWith(translation))
                {
                    Span[] translatedCaption = new Span[]
                    {
                        new Span()
                        {
                            Type = SpanType.Text, Text = line.Substring((line.IndexOf("): ") + 3))
                        },
                    };

                    phrase.Content = translatedCaption;

                    cueList.Add(phrase);

                    phrase = new Cue();
                }
            }

            // Set a default end time, and update in a second loop
            // for clarity
            for (int item = 0; item < cueList.Count - 1; item++)
            {
                cueList[item].End = cueList[item + 1].Start;
            }

            Cue[] Cues = cueList.ToArray();
            transcript.Cues = Cues;

            StringBuilder sb = new StringBuilder();

            using var writer = new StringWriter(sb);
            WebVttSerializer.SerializeAsync(transcript, writer).ConfigureAwait(false).GetAwaiter().GetResult();

            File.WriteAllText(String.Concat(args[0], ".vtt"), sb.ToString());

            Console.WriteLine("Press any key to exit. {0} File saved", String.Concat(args[0], ".vtt"));
            System.Console.ReadKey();

            return(0);
        }
Пример #19
0
 public void IfNullWriter_Throws()
 {
     WebVttSerializer.SerializeAsync((MediaCaptions)null, null)
     .ConfigureAwait(false).GetAwaiter().GetResult();
 }
Пример #20
0
        public void WriteMediaCaptionsWithOutOfOrderCues()
        {
            StringBuilder sb = new StringBuilder();

            using (var writer = new StringWriter(sb))
            {
                WebVttSerializer.SerializeAsync(
                    new MediaCaptions
                {
                    Cues = new Cue[] {
                        new Cue()
                        {
                            Start   = TimeSpan.FromSeconds(30),
                            End     = TimeSpan.FromSeconds(40),
                            Content = new Span[]
                            {
                                new Span()
                                {
                                    Type = SpanType.Text, Text = "Second cue."
                                },
                            }
                        },
                        new Cue()
                        {
                            Start   = TimeSpan.FromSeconds(10),
                            End     = TimeSpan.FromSeconds(20),
                            Content = new Span[]
                            {
                                new Span()
                                {
                                    Type = SpanType.Text, Text = "First cue."
                                },
                            }
                        },
                        new Cue()
                        {
                            Start   = TimeSpan.FromSeconds(30),
                            End     = TimeSpan.FromSeconds(50),
                            Content = new Span[]
                            {
                                new Span()
                                {
                                    Type = SpanType.Text, Text = "Third cue."
                                },
                            }
                        }
                    }
                },
                    writer)
                .ConfigureAwait(false).GetAwaiter().GetResult();

                string expected = @"WEBVTT

00:00:10.000 --> 00:00:20.000
First cue.

00:00:30.000 --> 00:00:40.000
Second cue.

00:00:30.000 --> 00:00:50.000
Third cue.
";

                Assert.AreEqual(expected, sb.ToString());
            }
        }
Пример #21
0
        public void WriteSingleLineCue()
        {
            StringBuilder sb = new StringBuilder();

            using (var writer = new StringWriter(sb))
            {
                WebVttSerializer.SerializeAsync(
                    new BaseBlock[]
                {
                    new Cue()
                    {
                        Alignment = TextAlignment.Start,
                        Start     = TimeSpan.FromSeconds(9.25),
                        End       = new TimeSpan(1, 2, 3, 4, 56),
                        Id        = "Cue Id",
                        Line      = new LineSettings()
                        {
                            Alignment = LineAlignment.Center, Percent = 12.34
                        },
                        Position = new PositionSettings()
                        {
                            Alignment = PositionAlignment.LineRight, PositionPercent = 43.21
                        },
                        Region      = "Region",
                        SizePercent = 100.0,
                        Vertical    = VerticalTextLayout.LeftToRight,
                        Content     = new Span[]
                        {
                            new Span()
                            {
                                Type       = SpanType.Voice,
                                Classes    = new string[] { "one", "two", "three" },
                                Annotation = "Cue voice",
                                Children   = new Span[]
                                {
                                    new Span()
                                    {
                                        Type = SpanType.Text, Text = "Prefix"
                                    },
                                    new Span()
                                    {
                                        Type = SpanType.Italics, Children = new Span[] { new Span()
                                                                                         {
                                                                                             Type = SpanType.Text, Text = "Italic"
                                                                                         } }
                                    },
                                    new Span()
                                    {
                                        Type = SpanType.Underline, Children = new Span[] { new Span()
                                                                                           {
                                                                                               Type = SpanType.Text, Text = "Underline"
                                                                                           } }
                                    },
                                    new Span()
                                    {
                                        Type = SpanType.Bold, Children = new Span[] { new Span()
                                                                                      {
                                                                                          Type = SpanType.Text, Text = "Bold"
                                                                                      } }
                                    },
                                    new Span()
                                    {
                                        Type = SpanType.Class, Children = new Span[] { new Span()
                                                                                       {
                                                                                           Type = SpanType.Text, Text = "Class"
                                                                                       } }
                                    },
                                    new Span()
                                    {
                                        Type = SpanType.Language, Annotation = "en-us", Children = new Span[] { new Span()
                                                                                                                {
                                                                                                                    Type = SpanType.Text, Text = "Language"
                                                                                                                } }
                                    },
                                }
                            }
                        }
                    }
                },
                    writer)
                .ConfigureAwait(false).GetAwaiter().GetResult();

                string expected = @"WEBVTT

Cue Id
00:00:09.250 --> 1:02:03:04.056 region:Region align:start line:12.34%,center position:43.21%,line-right size:100% vertical:lr
<v.one.two.three Cue voice>Prefix<i>Italic</i><u>Underline</u><b>Bold</b><c>Class</c><lang en-us>Language</lang></v>
";

                Assert.AreEqual(expected, sb.ToString());
            }
        }