2
0

SsaParserTests.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Text;
  4. using System.Threading;
  5. using MediaBrowser.MediaEncoding.Subtitles;
  6. using MediaBrowser.Model.MediaInfo;
  7. using Xunit;
  8. namespace Jellyfin.MediaEncoding.Tests
  9. {
  10. public class SsaParserTests
  11. {
  12. // commonly shared invariant value between tests, assumes default format order
  13. private const string InvariantDialoguePrefix = "[Events]\nDialogue: ,0:00:00.00,0:00:00.01,,,,,,,";
  14. private SsaParser parser = new SsaParser();
  15. [Theory]
  16. [InlineData("[EvEnTs]\nDialogue: ,0:00:00.00,0:00:00.01,,,,,,,text", "text")] // label casing insensitivity
  17. [InlineData("[Events]\n,0:00:00.00,0:00:00.01,,,,,,,labelless dialogue", "labelless dialogue")] // no "Dialogue:" label, it is optional
  18. [InlineData("[Events]\nFormat: Text, Start, End, Layer, Effect, Style\nDialogue: reordered text,0:00:00.00,0:00:00.01", "reordered text")] // reordered formats
  19. [InlineData(InvariantDialoguePrefix + "Cased TEXT", "Cased TEXT")] // preserve text casing
  20. [InlineData(InvariantDialoguePrefix + " text ", " text ")] // do not trim text
  21. [InlineData(InvariantDialoguePrefix + "text, more text", "text, more text")] // append excess dialogue values (> 10) to text
  22. [InlineData(InvariantDialoguePrefix + "start {\\fnFont Name}text{\\fn} end", "start <font face=\"Font Name\">text</font> end")] // font name
  23. [InlineData(InvariantDialoguePrefix + "start {\\fs10}text{\\fs} end", "start <font size=\"10\">text</font> end")] // font size
  24. [InlineData(InvariantDialoguePrefix + "start {\\c&H112233}text{\\c} end", "start <font color=\"#332211\">text</font> end")] // color
  25. [InlineData(InvariantDialoguePrefix + "start {\\1c&H112233}text{\\1c} end", "start <font color=\"#332211\">text</font> end")] // primay color
  26. [InlineData(InvariantDialoguePrefix + "start {\\fnFont Name}text1 {\\fs10}text2{\\fs}{\\fn} {\\1c&H112233}text3{\\1c} end", "start <font face=\"Font Name\">text1 <font size=\"10\">text2</font></font> <font color=\"#332211\">text3</font> end")] // nested formatting
  27. public void Parse(string ssa, string expectedText)
  28. {
  29. using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(ssa)))
  30. {
  31. SubtitleTrackInfo subtitleTrackInfo = parser.Parse(stream, CancellationToken.None);
  32. SubtitleTrackEvent actual = subtitleTrackInfo.TrackEvents[0];
  33. Assert.Equal(expectedText, actual.Text);
  34. }
  35. }
  36. [Theory]
  37. [MemberData(nameof(Parse_MultipleDialogues_TestData))]
  38. public void Parse_MultipleDialogues(string ssa, IReadOnlyList<SubtitleTrackEvent> expectedSubtitleTrackEvents)
  39. {
  40. using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(ssa)))
  41. {
  42. SubtitleTrackInfo subtitleTrackInfo = parser.Parse(stream, CancellationToken.None);
  43. Assert.Equal(expectedSubtitleTrackEvents.Count, subtitleTrackInfo.TrackEvents.Count);
  44. for (int i = 0; i < expectedSubtitleTrackEvents.Count; ++i)
  45. {
  46. SubtitleTrackEvent expected = expectedSubtitleTrackEvents[i];
  47. SubtitleTrackEvent actual = subtitleTrackInfo.TrackEvents[i];
  48. Assert.Equal(expected.StartPositionTicks, actual.StartPositionTicks);
  49. Assert.Equal(expected.EndPositionTicks, actual.EndPositionTicks);
  50. Assert.Equal(expected.Text, actual.Text);
  51. }
  52. }
  53. }
  54. public static IEnumerable<object[]> Parse_MultipleDialogues_TestData()
  55. {
  56. yield return new object[]
  57. {
  58. @"[Events]
  59. Format: Layer, Start, End, Text
  60. Dialogue: ,0:00:01.18,0:00:01.85,dialogue1
  61. Dialogue: ,0:00:02.18,0:00:02.85,dialogue2
  62. Dialogue: ,0:00:03.18,0:00:03.85,dialogue3
  63. ",
  64. new List<SubtitleTrackEvent>
  65. {
  66. new SubtitleTrackEvent
  67. {
  68. StartPositionTicks = 11800000,
  69. EndPositionTicks = 18500000,
  70. Text = "dialogue1"
  71. },
  72. new SubtitleTrackEvent
  73. {
  74. StartPositionTicks = 21800000,
  75. EndPositionTicks = 28500000,
  76. Text = "dialogue2"
  77. },
  78. new SubtitleTrackEvent
  79. {
  80. StartPositionTicks = 31800000,
  81. EndPositionTicks = 38500000,
  82. Text = "dialogue3"
  83. }
  84. }
  85. };
  86. }
  87. }
  88. }