ProbeResultNormalizerTests.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. using System;
  2. using System.Globalization;
  3. using System.IO;
  4. using System.Text.Json;
  5. using Jellyfin.Extensions.Json;
  6. using MediaBrowser.MediaEncoding.Probing;
  7. using MediaBrowser.Model.Entities;
  8. using MediaBrowser.Model.Globalization;
  9. using MediaBrowser.Model.MediaInfo;
  10. using Microsoft.Extensions.Logging.Abstractions;
  11. using Moq;
  12. using Xunit;
  13. namespace Jellyfin.MediaEncoding.Tests.Probing
  14. {
  15. public class ProbeResultNormalizerTests
  16. {
  17. private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
  18. private readonly ProbeResultNormalizer _probeResultNormalizer = new ProbeResultNormalizer(new NullLogger<EncoderValidatorTests>(), null);
  19. [Theory]
  20. [InlineData("2997/125", 23.976f)]
  21. [InlineData("1/50", 0.02f)]
  22. [InlineData("25/1", 25f)]
  23. [InlineData("120/1", 120f)]
  24. [InlineData("1704753000/71073479", 23.98578237601117f)]
  25. [InlineData("0/0", null)]
  26. [InlineData("1/1000", 0.001f)]
  27. [InlineData("1/90000", 1.1111111E-05f)]
  28. [InlineData("1/48000", 2.0833333E-05f)]
  29. public void GetFrameRate_Success(string value, float? expected)
  30. => Assert.Equal(expected, ProbeResultNormalizer.GetFrameRate(value));
  31. [Fact]
  32. public void GetMediaInfo_MetaData_Success()
  33. {
  34. var bytes = File.ReadAllBytes("Test Data/Probing/video_metadata.json");
  35. var internalMediaInfoResult = JsonSerializer.Deserialize<InternalMediaInfoResult>(bytes, _jsonOptions);
  36. MediaInfo res = _probeResultNormalizer.GetMediaInfo(internalMediaInfoResult, VideoType.VideoFile, false, "Test Data/Probing/video_metadata.mkv", MediaProtocol.File);
  37. Assert.Single(res.MediaStreams);
  38. Assert.NotNull(res.VideoStream);
  39. Assert.Equal("4:3", res.VideoStream.AspectRatio);
  40. Assert.Equal(25f, res.VideoStream.AverageFrameRate);
  41. Assert.Equal(8, res.VideoStream.BitDepth);
  42. Assert.Equal(69432, res.VideoStream.BitRate);
  43. Assert.Equal("h264", res.VideoStream.Codec);
  44. Assert.Equal("1/50", res.VideoStream.CodecTimeBase);
  45. Assert.Equal(240, res.VideoStream.Height);
  46. Assert.Equal(320, res.VideoStream.Width);
  47. Assert.Equal(0, res.VideoStream.Index);
  48. Assert.False(res.VideoStream.IsAnamorphic);
  49. Assert.True(res.VideoStream.IsAVC);
  50. Assert.True(res.VideoStream.IsDefault);
  51. Assert.False(res.VideoStream.IsExternal);
  52. Assert.False(res.VideoStream.IsForced);
  53. Assert.False(res.VideoStream.IsHearingImpaired);
  54. Assert.False(res.VideoStream.IsInterlaced);
  55. Assert.False(res.VideoStream.IsTextSubtitleStream);
  56. Assert.Equal(13d, res.VideoStream.Level);
  57. Assert.Equal("4", res.VideoStream.NalLengthSize);
  58. Assert.Equal("yuv444p", res.VideoStream.PixelFormat);
  59. Assert.Equal("High 4:4:4 Predictive", res.VideoStream.Profile);
  60. Assert.Equal(25f, res.VideoStream.RealFrameRate);
  61. Assert.Equal(1, res.VideoStream.RefFrames);
  62. Assert.Equal("1/1000", res.VideoStream.TimeBase);
  63. Assert.Equal(MediaStreamType.Video, res.VideoStream.Type);
  64. Assert.Equal(1, res.VideoStream.DvVersionMajor);
  65. Assert.Equal(0, res.VideoStream.DvVersionMinor);
  66. Assert.Equal(5, res.VideoStream.DvProfile);
  67. Assert.Equal(6, res.VideoStream.DvLevel);
  68. Assert.Equal(1, res.VideoStream.RpuPresentFlag);
  69. Assert.Equal(0, res.VideoStream.ElPresentFlag);
  70. Assert.Equal(1, res.VideoStream.BlPresentFlag);
  71. Assert.Equal(0, res.VideoStream.DvBlSignalCompatibilityId);
  72. Assert.Empty(res.Chapters);
  73. Assert.Equal("Just color bars", res.Overview);
  74. }
  75. [Fact]
  76. public void GetMediaInfo_Mp4MetaData_Success()
  77. {
  78. var bytes = File.ReadAllBytes("Test Data/Probing/video_mp4_metadata.json");
  79. var internalMediaInfoResult = JsonSerializer.Deserialize<InternalMediaInfoResult>(bytes, _jsonOptions);
  80. // subtitle handling requires a localization object, set a mock to return the input string
  81. var mockLocalization = new Mock<ILocalizationManager>();
  82. mockLocalization.Setup(x => x.GetLocalizedString(It.IsAny<string>())).Returns<string>(x => x);
  83. ProbeResultNormalizer localizedProbeResultNormalizer = new ProbeResultNormalizer(new NullLogger<EncoderValidatorTests>(), mockLocalization.Object);
  84. MediaInfo res = localizedProbeResultNormalizer.GetMediaInfo(internalMediaInfoResult, VideoType.VideoFile, false, "Test Data/Probing/video_mp4_metadata.mkv", MediaProtocol.File);
  85. // [Video, Audio (Main), Audio (Commentary), Subtitle (Main, Spanish), Subtitle (Main, English), Subtitle (Commentary)
  86. Assert.Equal(6, res.MediaStreams.Count);
  87. Assert.NotNull(res.VideoStream);
  88. Assert.Equal(res.MediaStreams[0], res.VideoStream);
  89. Assert.Equal(0, res.VideoStream.Index);
  90. Assert.Equal("h264", res.VideoStream.Codec);
  91. Assert.Equal("High", res.VideoStream.Profile);
  92. Assert.Equal(MediaStreamType.Video, res.VideoStream.Type);
  93. Assert.Equal(358, res.VideoStream.Height);
  94. Assert.Equal(720, res.VideoStream.Width);
  95. Assert.Equal("2.40:1", res.VideoStream.AspectRatio);
  96. Assert.Equal("yuv420p", res.VideoStream.PixelFormat);
  97. Assert.Equal(31d, res.VideoStream.Level);
  98. Assert.Equal(1, res.VideoStream.RefFrames);
  99. Assert.True(res.VideoStream.IsAVC);
  100. Assert.Equal(120f, res.VideoStream.RealFrameRate);
  101. Assert.Equal("1/90000", res.VideoStream.TimeBase);
  102. Assert.Equal(1147365, res.VideoStream.BitRate);
  103. Assert.Equal(8, res.VideoStream.BitDepth);
  104. Assert.True(res.VideoStream.IsDefault);
  105. Assert.Equal("und", res.VideoStream.Language);
  106. Assert.Equal(MediaStreamType.Audio, res.MediaStreams[1].Type);
  107. Assert.Equal("aac", res.MediaStreams[1].Codec);
  108. Assert.Equal(7, res.MediaStreams[1].Channels);
  109. Assert.True(res.MediaStreams[1].IsDefault);
  110. Assert.Equal("eng", res.MediaStreams[1].Language);
  111. Assert.Equal("Surround 6.1", res.MediaStreams[1].Title);
  112. Assert.Equal(MediaStreamType.Audio, res.MediaStreams[2].Type);
  113. Assert.Equal("aac", res.MediaStreams[2].Codec);
  114. Assert.Equal(2, res.MediaStreams[2].Channels);
  115. Assert.False(res.MediaStreams[2].IsDefault);
  116. Assert.Equal("eng", res.MediaStreams[2].Language);
  117. Assert.Equal("Commentary", res.MediaStreams[2].Title);
  118. Assert.Equal("spa", res.MediaStreams[3].Language);
  119. Assert.Equal(MediaStreamType.Subtitle, res.MediaStreams[3].Type);
  120. Assert.Equal("DVDSUB", res.MediaStreams[3].Codec);
  121. Assert.Null(res.MediaStreams[3].Title);
  122. Assert.False(res.MediaStreams[3].IsHearingImpaired);
  123. Assert.Equal("eng", res.MediaStreams[4].Language);
  124. Assert.Equal(MediaStreamType.Subtitle, res.MediaStreams[4].Type);
  125. Assert.Equal("mov_text", res.MediaStreams[4].Codec);
  126. Assert.Null(res.MediaStreams[4].Title);
  127. Assert.True(res.MediaStreams[4].IsHearingImpaired);
  128. Assert.Equal("eng", res.MediaStreams[5].Language);
  129. Assert.Equal(MediaStreamType.Subtitle, res.MediaStreams[5].Type);
  130. Assert.Equal("mov_text", res.MediaStreams[5].Codec);
  131. Assert.Equal("Commentary", res.MediaStreams[5].Title);
  132. Assert.False(res.MediaStreams[5].IsHearingImpaired);
  133. }
  134. [Fact]
  135. public void GetMediaInfo_ProgressiveVideoNoFieldOrder_Success()
  136. {
  137. var bytes = File.ReadAllBytes("Test Data/Probing/video_progressive_no_field_order.json");
  138. var internalMediaInfoResult = JsonSerializer.Deserialize<InternalMediaInfoResult>(bytes, _jsonOptions);
  139. MediaInfo res = _probeResultNormalizer.GetMediaInfo(internalMediaInfoResult, VideoType.VideoFile, false, "Test Data/Probing/video_progressive_no_field_order.mp4", MediaProtocol.File);
  140. Assert.Equal(2, res.MediaStreams.Count);
  141. Assert.NotNull(res.VideoStream);
  142. Assert.Equal(res.MediaStreams[0], res.VideoStream);
  143. Assert.Equal(0, res.VideoStream.Index);
  144. Assert.Equal("h264", res.VideoStream.Codec);
  145. Assert.Equal("Main", res.VideoStream.Profile);
  146. Assert.Equal(MediaStreamType.Video, res.VideoStream.Type);
  147. Assert.Equal(1080, res.VideoStream.Height);
  148. Assert.Equal(1920, res.VideoStream.Width);
  149. Assert.False(res.VideoStream.IsInterlaced);
  150. Assert.Equal("16:9", res.VideoStream.AspectRatio);
  151. Assert.Equal("yuv420p", res.VideoStream.PixelFormat);
  152. Assert.Equal(41d, res.VideoStream.Level);
  153. Assert.Equal(1, res.VideoStream.RefFrames);
  154. Assert.True(res.VideoStream.IsAVC);
  155. Assert.Equal(23.9760246f, res.VideoStream.RealFrameRate);
  156. Assert.Equal("1/24000", res.VideoStream.TimeBase);
  157. Assert.Equal(3948341, res.VideoStream.BitRate);
  158. Assert.Equal(8, res.VideoStream.BitDepth);
  159. Assert.True(res.VideoStream.IsDefault);
  160. }
  161. [Fact]
  162. public void GetMediaInfo_ProgressiveVideoNoFieldOrder2_Success()
  163. {
  164. var bytes = File.ReadAllBytes("Test Data/Probing/video_progressive_no_field_order2.json");
  165. var internalMediaInfoResult = JsonSerializer.Deserialize<InternalMediaInfoResult>(bytes, _jsonOptions);
  166. MediaInfo res = _probeResultNormalizer.GetMediaInfo(internalMediaInfoResult, VideoType.VideoFile, false, "Test Data/Probing/video_progressive_no_field_order2.mp4", MediaProtocol.File);
  167. Assert.Single(res.MediaStreams);
  168. Assert.NotNull(res.VideoStream);
  169. Assert.Equal(res.MediaStreams[0], res.VideoStream);
  170. Assert.Equal(0, res.VideoStream.Index);
  171. Assert.Equal("h264", res.VideoStream.Codec);
  172. Assert.Equal("High", res.VideoStream.Profile);
  173. Assert.Equal(MediaStreamType.Video, res.VideoStream.Type);
  174. Assert.Equal(720, res.VideoStream.Height);
  175. Assert.Equal(1280, res.VideoStream.Width);
  176. Assert.False(res.VideoStream.IsInterlaced);
  177. Assert.Equal("16:9", res.VideoStream.AspectRatio);
  178. Assert.Equal("yuv420p", res.VideoStream.PixelFormat);
  179. Assert.Equal(31d, res.VideoStream.Level);
  180. Assert.Equal(1, res.VideoStream.RefFrames);
  181. Assert.True(res.VideoStream.IsAVC);
  182. Assert.Equal(25f, res.VideoStream.RealFrameRate);
  183. Assert.Equal("1/12800", res.VideoStream.TimeBase);
  184. Assert.Equal(53288, res.VideoStream.BitRate);
  185. Assert.Equal(8, res.VideoStream.BitDepth);
  186. Assert.True(res.VideoStream.IsDefault);
  187. }
  188. [Fact]
  189. public void GetMediaInfo_InterlacedVideo_Success()
  190. {
  191. var bytes = File.ReadAllBytes("Test Data/Probing/video_interlaced.json");
  192. var internalMediaInfoResult = JsonSerializer.Deserialize<InternalMediaInfoResult>(bytes, _jsonOptions);
  193. MediaInfo res = _probeResultNormalizer.GetMediaInfo(internalMediaInfoResult, VideoType.VideoFile, false, "Test Data/Probing/video_interlaced.mp4", MediaProtocol.File);
  194. Assert.Single(res.MediaStreams);
  195. Assert.NotNull(res.VideoStream);
  196. Assert.Equal(res.MediaStreams[0], res.VideoStream);
  197. Assert.Equal(0, res.VideoStream.Index);
  198. Assert.Equal("h264", res.VideoStream.Codec);
  199. Assert.Equal("High", res.VideoStream.Profile);
  200. Assert.Equal(MediaStreamType.Video, res.VideoStream.Type);
  201. Assert.Equal(720, res.VideoStream.Height);
  202. Assert.Equal(1280, res.VideoStream.Width);
  203. Assert.True(res.VideoStream.IsInterlaced);
  204. Assert.Equal("16:9", res.VideoStream.AspectRatio);
  205. Assert.Equal("yuv420p", res.VideoStream.PixelFormat);
  206. Assert.Equal(40d, res.VideoStream.Level);
  207. Assert.Equal(1, res.VideoStream.RefFrames);
  208. Assert.True(res.VideoStream.IsAVC);
  209. Assert.Equal(25f, res.VideoStream.RealFrameRate);
  210. Assert.Equal("1/12800", res.VideoStream.TimeBase);
  211. Assert.Equal(56945, res.VideoStream.BitRate);
  212. Assert.Equal(8, res.VideoStream.BitDepth);
  213. Assert.True(res.VideoStream.IsDefault);
  214. }
  215. [Fact]
  216. public void GetMediaInfo_MusicVideo_Success()
  217. {
  218. var bytes = File.ReadAllBytes("Test Data/Probing/music_video_metadata.json");
  219. var internalMediaInfoResult = JsonSerializer.Deserialize<InternalMediaInfoResult>(bytes, _jsonOptions);
  220. MediaInfo res = _probeResultNormalizer.GetMediaInfo(internalMediaInfoResult, VideoType.VideoFile, false, "Test Data/Probing/music_video.mkv", MediaProtocol.File);
  221. Assert.Equal("The Title", res.Name);
  222. Assert.Equal("Title, The", res.ForcedSortName);
  223. Assert.Single(res.Artists);
  224. Assert.Equal("The Artist", res.Artists[0]);
  225. Assert.Equal("Album", res.Album);
  226. Assert.Equal(2021, res.ProductionYear);
  227. Assert.True(res.PremiereDate.HasValue);
  228. Assert.Equal(DateTime.Parse("2021-01-01T00:00Z", DateTimeFormatInfo.CurrentInfo, DateTimeStyles.AdjustToUniversal), res.PremiereDate);
  229. }
  230. [Fact]
  231. public void GetMediaInfo_GivenOriginalDateContainsOnlyYear_Success()
  232. {
  233. var bytes = File.ReadAllBytes("Test Data/Probing/music_year_only_metadata.json");
  234. var internalMediaInfoResult = JsonSerializer.Deserialize<InternalMediaInfoResult>(bytes, _jsonOptions);
  235. MediaInfo res = _probeResultNormalizer.GetMediaInfo(internalMediaInfoResult, null, true, "Test Data/Probing/music.flac", MediaProtocol.File);
  236. Assert.Equal("Baker Street", res.Name);
  237. Assert.Single(res.Artists);
  238. Assert.Equal("Gerry Rafferty", res.Artists[0]);
  239. Assert.Equal("City to City", res.Album);
  240. Assert.Equal(1978, res.ProductionYear);
  241. Assert.True(res.PremiereDate.HasValue);
  242. Assert.Equal(DateTime.Parse("1978-01-01T00:00Z", DateTimeFormatInfo.CurrentInfo, DateTimeStyles.AdjustToUniversal), res.PremiereDate);
  243. Assert.Contains("Electronic", res.Genres);
  244. Assert.Contains("Ambient", res.Genres);
  245. Assert.Contains("Pop", res.Genres);
  246. Assert.Contains("Jazz", res.Genres);
  247. }
  248. [Fact]
  249. public void GetMediaInfo_Music_Success()
  250. {
  251. var bytes = File.ReadAllBytes("Test Data/Probing/music_metadata.json");
  252. var internalMediaInfoResult = JsonSerializer.Deserialize<InternalMediaInfoResult>(bytes, _jsonOptions);
  253. MediaInfo res = _probeResultNormalizer.GetMediaInfo(internalMediaInfoResult, null, true, "Test Data/Probing/music.flac", MediaProtocol.File);
  254. Assert.Equal("UP NO MORE", res.Name);
  255. Assert.Single(res.Artists);
  256. Assert.Equal("TWICE", res.Artists[0]);
  257. Assert.Equal("Eyes wide open", res.Album);
  258. Assert.Equal(2020, res.ProductionYear);
  259. Assert.True(res.PremiereDate.HasValue);
  260. Assert.Equal(DateTime.Parse("2020-10-26T00:00Z", DateTimeFormatInfo.CurrentInfo, DateTimeStyles.AdjustToUniversal), res.PremiereDate);
  261. Assert.Equal(22, res.People.Length);
  262. Assert.Equal("Krysta Youngs", res.People[0].Name);
  263. Assert.Equal(PersonType.Composer, res.People[0].Type);
  264. Assert.Equal("Julia Ross", res.People[1].Name);
  265. Assert.Equal(PersonType.Composer, res.People[1].Type);
  266. Assert.Equal("Yiwoomin", res.People[2].Name);
  267. Assert.Equal(PersonType.Composer, res.People[2].Type);
  268. Assert.Equal("Ji-hyo Park", res.People[3].Name);
  269. Assert.Equal(PersonType.Lyricist, res.People[3].Type);
  270. Assert.Equal("Yiwoomin", res.People[4].Name);
  271. Assert.Equal(PersonType.Actor, res.People[4].Type);
  272. Assert.Equal("Electric Piano", res.People[4].Role);
  273. Assert.Equal(4, res.Genres.Length);
  274. Assert.Contains("Electronic", res.Genres);
  275. Assert.Contains("Trance", res.Genres);
  276. Assert.Contains("Dance", res.Genres);
  277. Assert.Contains("Jazz", res.Genres);
  278. }
  279. }
  280. }