From f8fcbc88fca8086052d1b3ef37dbd71d85e7dee7 Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Sun, 12 Dec 2021 02:22:30 +0100 Subject: [PATCH 1/2] Add tests for ProbeResultNormalizer.GetFrameRate --- .../Probing/ProbeResultNormalizer.cs | 27 +++++++++++-------- .../Probing/ProbeResultNormalizerTests.cs | 13 +++++++++ 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs b/MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs index 770881149f..ed0c8a2e10 100644 --- a/MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs +++ b/MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs @@ -1027,27 +1027,32 @@ namespace MediaBrowser.MediaEncoding.Probing /// /// The value. /// System.Nullable{System.Single}. - private float? GetFrameRate(string value) + internal static float? GetFrameRate(ReadOnlySpan value) { - if (string.IsNullOrEmpty(value)) + if (value.IsEmpty) { return null; } - var parts = value.Split('/'); - - float result; - - if (parts.Length == 2) + int index = value.IndexOf('/'); + if (index == -1) { - result = float.Parse(parts[0], CultureInfo.InvariantCulture) / float.Parse(parts[1], CultureInfo.InvariantCulture); + // REVIEW: is this branch actually required? (i.e. does ffprobe ever output something other than a fraction?) + if (float.TryParse(value, NumberStyles.AllowThousands | NumberStyles.Float, CultureInfo.InvariantCulture, out var result)) + { + return result; + } + + return null; } - else + + if (!float.TryParse(value[..index], NumberStyles.Integer, CultureInfo.InvariantCulture, out var dividend) + || !float.TryParse(value[(index + 1)..], NumberStyles.Integer, CultureInfo.InvariantCulture, out var divisor)) { - result = float.Parse(parts[0], CultureInfo.InvariantCulture); + return null; } - return float.IsNaN(result) ? null : result; + return divisor == 0f ? 0f : dividend / divisor; } private void SetAudioRuntimeTicks(InternalMediaInfoResult result, MediaInfo data) diff --git a/tests/Jellyfin.MediaEncoding.Tests/Probing/ProbeResultNormalizerTests.cs b/tests/Jellyfin.MediaEncoding.Tests/Probing/ProbeResultNormalizerTests.cs index 4504924cbf..cc1ec495a0 100644 --- a/tests/Jellyfin.MediaEncoding.Tests/Probing/ProbeResultNormalizerTests.cs +++ b/tests/Jellyfin.MediaEncoding.Tests/Probing/ProbeResultNormalizerTests.cs @@ -18,6 +18,19 @@ namespace Jellyfin.MediaEncoding.Tests.Probing private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options; private readonly ProbeResultNormalizer _probeResultNormalizer = new ProbeResultNormalizer(new NullLogger(), null); + [Theory] + [InlineData("2997/125", 23.976f)] + [InlineData("1/50", 0.02f)] + [InlineData("25/1", 25f)] + [InlineData("120/1", 120f)] + [InlineData("1704753000/71073479", 23.98578237601117f)] + [InlineData("0/0", 0f)] + [InlineData("1/1000", 0.001f)] + [InlineData("1/90000", 1.1111111E-05f)] + [InlineData("1/48000", 2.0833333E-05f)] + public void GetFrameRate_Success(string value, float? expected) + => Assert.Equal(expected, ProbeResultNormalizer.GetFrameRate(value)); + [Fact] public void GetMediaInfo_MetaData_Success() { From 968c534864efbd979feedb859b4b2afeb279ae52 Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Wed, 15 Dec 2021 18:33:27 +0100 Subject: [PATCH 2/2] Return null on division by zero --- MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs | 2 +- .../Probing/ProbeResultNormalizerTests.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs b/MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs index ed0c8a2e10..da76ff0f98 100644 --- a/MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs +++ b/MediaBrowser.MediaEncoding/Probing/ProbeResultNormalizer.cs @@ -1052,7 +1052,7 @@ namespace MediaBrowser.MediaEncoding.Probing return null; } - return divisor == 0f ? 0f : dividend / divisor; + return divisor == 0f ? null : dividend / divisor; } private void SetAudioRuntimeTicks(InternalMediaInfoResult result, MediaInfo data) diff --git a/tests/Jellyfin.MediaEncoding.Tests/Probing/ProbeResultNormalizerTests.cs b/tests/Jellyfin.MediaEncoding.Tests/Probing/ProbeResultNormalizerTests.cs index cc1ec495a0..0fc8724b6a 100644 --- a/tests/Jellyfin.MediaEncoding.Tests/Probing/ProbeResultNormalizerTests.cs +++ b/tests/Jellyfin.MediaEncoding.Tests/Probing/ProbeResultNormalizerTests.cs @@ -24,7 +24,7 @@ namespace Jellyfin.MediaEncoding.Tests.Probing [InlineData("25/1", 25f)] [InlineData("120/1", 120f)] [InlineData("1704753000/71073479", 23.98578237601117f)] - [InlineData("0/0", 0f)] + [InlineData("0/0", null)] [InlineData("1/1000", 0.001f)] [InlineData("1/90000", 1.1111111E-05f)] [InlineData("1/48000", 2.0833333E-05f)]