mirror of
				https://github.com/jellyfin/jellyfin.git
				synced 2025-11-03 19:17:24 -05:00 
			
		
		
		
	add VTT writer implementation
This commit is contained in:
		
							parent
							
								
									62ffe2c59d
								
							
						
					
					
						commit
						8f56b74d63
					
				@ -9,8 +9,18 @@ namespace MediaBrowser.MediaEncoding.Subtitles
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
    public class VttWriter : ISubtitleWriter
 | 
					    public class VttWriter : ISubtitleWriter
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        public void Write(SubtitleTrackInfo info, Stream stream)
 | 
					        public void Write(SubtitleTrackInfo info, Stream stream) {
 | 
				
			||||||
 | 
					            using (var writer = new StreamWriter(stream))
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
 | 
					                writer.WriteLine("WEBVTT");
 | 
				
			||||||
 | 
					                writer.WriteLine(string.Empty);
 | 
				
			||||||
 | 
					                foreach (var trackEvent in info.TrackEvents)
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    writer.WriteLine(@"{0:hh\:mm\:ss\.fff} --> {1:hh\:mm\:ss\.fff}", TimeSpan.FromTicks(trackEvent.StartPositionTicks), TimeSpan.FromTicks(trackEvent.EndPositionTicks));
 | 
				
			||||||
 | 
					                    writer.WriteLine(trackEvent.Text);
 | 
				
			||||||
 | 
					                    writer.WriteLine(string.Empty);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -52,6 +52,7 @@
 | 
				
			|||||||
  <ItemGroup>
 | 
					  <ItemGroup>
 | 
				
			||||||
    <Compile Include="MediaEncoding\Subtitles\SsaParserTests.cs" />
 | 
					    <Compile Include="MediaEncoding\Subtitles\SsaParserTests.cs" />
 | 
				
			||||||
    <Compile Include="MediaEncoding\Subtitles\SrtParserTests.cs" />
 | 
					    <Compile Include="MediaEncoding\Subtitles\SrtParserTests.cs" />
 | 
				
			||||||
 | 
					    <Compile Include="MediaEncoding\Subtitles\VttWriterTest.cs" />
 | 
				
			||||||
    <Compile Include="Providers\MovieDbProviderTests.cs" />
 | 
					    <Compile Include="Providers\MovieDbProviderTests.cs" />
 | 
				
			||||||
    <Compile Include="Resolvers\MovieResolverTests.cs" />
 | 
					    <Compile Include="Resolvers\MovieResolverTests.cs" />
 | 
				
			||||||
    <Compile Include="Resolvers\TvUtilTests.cs" />
 | 
					    <Compile Include="Resolvers\TvUtilTests.cs" />
 | 
				
			||||||
@ -84,6 +85,9 @@
 | 
				
			|||||||
    <None Include="app.config" />
 | 
					    <None Include="app.config" />
 | 
				
			||||||
  </ItemGroup>
 | 
					  </ItemGroup>
 | 
				
			||||||
  <ItemGroup>
 | 
					  <ItemGroup>
 | 
				
			||||||
 | 
					    <None Include="MediaEncoding\Subtitles\TestSubtitles\expected.vtt">
 | 
				
			||||||
 | 
					      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
 | 
				
			||||||
 | 
					    </None>
 | 
				
			||||||
    <None Include="MediaEncoding\Subtitles\TestSubtitles\data.ssa">
 | 
					    <None Include="MediaEncoding\Subtitles\TestSubtitles\data.ssa">
 | 
				
			||||||
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
 | 
					      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
 | 
				
			||||||
    </None>
 | 
					    </None>
 | 
				
			||||||
 | 
				
			|||||||
@ -0,0 +1,32 @@
 | 
				
			|||||||
 | 
					WEBVTT
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					00:00:02.400 --> 00:00:05.200
 | 
				
			||||||
 | 
					[Background Music Playing]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					00:00:15.712 --> 00:00:17.399
 | 
				
			||||||
 | 
					Oh my god, Watch out!<br />It's coming!!
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					00:00:25.712 --> 00:00:30.399
 | 
				
			||||||
 | 
					[Bird noises]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					00:00:31.000 --> 00:00:31.999
 | 
				
			||||||
 | 
					This text is <font color="red">RED</font> and has not been positioned.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					00:00:32.000 --> 00:00:32.999
 | 
				
			||||||
 | 
					This is a<br />new line, as is<br />this
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					00:00:33.000 --> 00:00:33.999
 | 
				
			||||||
 | 
					This contains nested <b>bold, <i>italic, <u>underline</u> and <s>strike-through</s></u></i></b> HTML tags
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					00:00:34.000 --> 00:00:34.999
 | 
				
			||||||
 | 
					Unclosed but <b>supported HTML tags are left in,  SSA italics aren't
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					00:00:35.000 --> 00:00:35.999
 | 
				
			||||||
 | 
					<ggg>Unsupported</ggg> HTML tags are escaped and left in, even if <hhh>not closed.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					00:00:36.000 --> 00:00:36.999
 | 
				
			||||||
 | 
					Multiple SSA tags are stripped
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					00:00:37.000 --> 00:00:37.999
 | 
				
			||||||
 | 
					Greater than (<) and less than (>) are shown
 | 
				
			||||||
 | 
					
 | 
				
			||||||
							
								
								
									
										103
									
								
								MediaBrowser.Tests/MediaEncoding/Subtitles/VttWriterTest.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										103
									
								
								MediaBrowser.Tests/MediaEncoding/Subtitles/VttWriterTest.cs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,103 @@
 | 
				
			|||||||
 | 
					using System.Collections.Generic;
 | 
				
			||||||
 | 
					using System.IO;
 | 
				
			||||||
 | 
					using MediaBrowser.MediaEncoding.Subtitles;
 | 
				
			||||||
 | 
					using Microsoft.VisualStudio.TestTools.UnitTesting;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace MediaBrowser.Tests.MediaEncoding.Subtitles {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    [TestClass]
 | 
				
			||||||
 | 
					    public class VttWriterTest {
 | 
				
			||||||
 | 
					        [TestMethod]
 | 
				
			||||||
 | 
					        public void TestWrite() {
 | 
				
			||||||
 | 
					            var infoSubs =
 | 
				
			||||||
 | 
					                new SubtitleTrackInfo
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    TrackEvents = new List<SubtitleTrackEvent> {
 | 
				
			||||||
 | 
					                                                                                         new SubtitleTrackEvent {
 | 
				
			||||||
 | 
					                                                                                                                    Id = "1",
 | 
				
			||||||
 | 
					                                                                                                                    StartPositionTicks = 24000000,
 | 
				
			||||||
 | 
					                                                                                                                    EndPositionTicks = 52000000,
 | 
				
			||||||
 | 
					                                                                                                                    Text =
 | 
				
			||||||
 | 
					                                                                                                                        "[Background Music Playing]"
 | 
				
			||||||
 | 
					                                                                                                                },
 | 
				
			||||||
 | 
					                                                                                         new SubtitleTrackEvent {
 | 
				
			||||||
 | 
					                                                                                                                    Id = "2",
 | 
				
			||||||
 | 
					                                                                                                                    StartPositionTicks = 157120000,
 | 
				
			||||||
 | 
					                                                                                                                    EndPositionTicks = 173990000,
 | 
				
			||||||
 | 
					                                                                                                                    Text =
 | 
				
			||||||
 | 
					                                                                                                                        "Oh my god, Watch out!<br />It's coming!!"
 | 
				
			||||||
 | 
					                                                                                                                },
 | 
				
			||||||
 | 
					                                                                                         new SubtitleTrackEvent {
 | 
				
			||||||
 | 
					                                                                                                                    Id = "3",
 | 
				
			||||||
 | 
					                                                                                                                    StartPositionTicks = 257120000,
 | 
				
			||||||
 | 
					                                                                                                                    EndPositionTicks = 303990000,
 | 
				
			||||||
 | 
					                                                                                                                    Text = "[Bird noises]"
 | 
				
			||||||
 | 
					                                                                                                                },
 | 
				
			||||||
 | 
					                                                                                         new SubtitleTrackEvent {
 | 
				
			||||||
 | 
					                                                                                                                    Id = "4",
 | 
				
			||||||
 | 
					                                                                                                                    StartPositionTicks = 310000000,
 | 
				
			||||||
 | 
					                                                                                                                    EndPositionTicks = 319990000,
 | 
				
			||||||
 | 
					                                                                                                                    Text =
 | 
				
			||||||
 | 
					                                                                                                                        "This text is <font color=\"red\">RED</font> and has not been positioned."
 | 
				
			||||||
 | 
					                                                                                                                },
 | 
				
			||||||
 | 
					                                                                                         new SubtitleTrackEvent {
 | 
				
			||||||
 | 
					                                                                                                                    Id = "5",
 | 
				
			||||||
 | 
					                                                                                                                    StartPositionTicks = 320000000,
 | 
				
			||||||
 | 
					                                                                                                                    EndPositionTicks = 329990000,
 | 
				
			||||||
 | 
					                                                                                                                    Text =
 | 
				
			||||||
 | 
					                                                                                                                        "This is a<br />new line, as is<br />this"
 | 
				
			||||||
 | 
					                                                                                                                },
 | 
				
			||||||
 | 
					                                                                                         new SubtitleTrackEvent {
 | 
				
			||||||
 | 
					                                                                                                                    Id = "6",
 | 
				
			||||||
 | 
					                                                                                                                    StartPositionTicks = 330000000,
 | 
				
			||||||
 | 
					                                                                                                                    EndPositionTicks = 339990000,
 | 
				
			||||||
 | 
					                                                                                                                    Text =
 | 
				
			||||||
 | 
					                                                                                                                        "This contains nested <b>bold, <i>italic, <u>underline</u> and <s>strike-through</s></u></i></b> HTML tags"
 | 
				
			||||||
 | 
					                                                                                                                },
 | 
				
			||||||
 | 
					                                                                                         new SubtitleTrackEvent {
 | 
				
			||||||
 | 
					                                                                                                                    Id = "7",
 | 
				
			||||||
 | 
					                                                                                                                    StartPositionTicks = 340000000,
 | 
				
			||||||
 | 
					                                                                                                                    EndPositionTicks = 349990000,
 | 
				
			||||||
 | 
					                                                                                                                    Text =
 | 
				
			||||||
 | 
					                                                                                                                        "Unclosed but <b>supported HTML tags are left in,  SSA italics aren't"
 | 
				
			||||||
 | 
					                                                                                                                },
 | 
				
			||||||
 | 
					                                                                                         new SubtitleTrackEvent {
 | 
				
			||||||
 | 
					                                                                                                                    Id = "8",
 | 
				
			||||||
 | 
					                                                                                                                    StartPositionTicks = 350000000,
 | 
				
			||||||
 | 
					                                                                                                                    EndPositionTicks = 359990000,
 | 
				
			||||||
 | 
					                                                                                                                    Text =
 | 
				
			||||||
 | 
					                                                                                                                        "<ggg>Unsupported</ggg> HTML tags are escaped and left in, even if <hhh>not closed."
 | 
				
			||||||
 | 
					                                                                                                                },
 | 
				
			||||||
 | 
					                                                                                         new SubtitleTrackEvent {
 | 
				
			||||||
 | 
					                                                                                                                    Id = "9",
 | 
				
			||||||
 | 
					                                                                                                                    StartPositionTicks = 360000000,
 | 
				
			||||||
 | 
					                                                                                                                    EndPositionTicks = 369990000,
 | 
				
			||||||
 | 
					                                                                                                                    Text =
 | 
				
			||||||
 | 
					                                                                                                                        "Multiple SSA tags are stripped"
 | 
				
			||||||
 | 
					                                                                                                                },
 | 
				
			||||||
 | 
					                                                                                         new SubtitleTrackEvent {
 | 
				
			||||||
 | 
					                                                                                                                    Id = "10",
 | 
				
			||||||
 | 
					                                                                                                                    StartPositionTicks = 370000000,
 | 
				
			||||||
 | 
					                                                                                                                    EndPositionTicks = 379990000,
 | 
				
			||||||
 | 
					                                                                                                                    Text =
 | 
				
			||||||
 | 
					                                                                                                                        "Greater than (<) and less than (>) are shown"
 | 
				
			||||||
 | 
					                                                                                                                }
 | 
				
			||||||
 | 
					                                                                                     }
 | 
				
			||||||
 | 
					                };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            var sut = new VttWriter();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if(File.Exists("testVTT.vtt"))
 | 
				
			||||||
 | 
					                File.Delete("testVTT.vtt");
 | 
				
			||||||
 | 
					            using (var file = File.OpenWrite("testVTT.vtt"))
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                sut.Write(infoSubs,file);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            var result = File.ReadAllText("testVTT.vtt");
 | 
				
			||||||
 | 
					            var expectedText = File.ReadAllText(@"MediaEncoding\Subtitles\TestSubtitles\expected.vtt");
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					            Assert.AreEqual(expectedText, result);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user