From 1d6517796f5cd2732befaa552a7266cb98e803f1 Mon Sep 17 00:00:00 2001 From: Malte Rosenbjerg Date: Mon, 15 Mar 2021 23:43:22 +0100 Subject: [PATCH] Fix tests --- FFMpegCore.Test/FFProbeTests.cs | 24 +++++++++++++----------- FFMpegCore/FFMpeg/FFMpeg.cs | 4 ++-- FFMpegCore/FFProbe/MediaAnalysis.cs | 10 ++-------- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/FFMpegCore.Test/FFProbeTests.cs b/FFMpegCore.Test/FFProbeTests.cs index a056fd9..cba5c52 100644 --- a/FFMpegCore.Test/FFProbeTests.cs +++ b/FFMpegCore.Test/FFProbeTests.cs @@ -25,20 +25,22 @@ public async Task Audio_FromStream_Duration() Assert.IsTrue(fileAnalysis.Duration == streamAnalysis.Duration); } - [TestMethod] - public void MediaAnalysis_ParseDuration() + [DataTestMethod] + [DataRow("0:00:03.008000", 0, 0, 0, 3, 8)] + [DataRow("05:12:59.177", 0, 5, 12, 59, 177)] + [DataRow("149:07:50.911750", 6, 5, 7, 50, 911750)] + [DataRow("00:00:00.83", 0, 0, 0, 0, 830)] + public void MediaAnalysis_ParseDuration(string duration, int expectedDays, int expectedHours, int expectedMinutes, int expectedSeconds, int expectedMilliseconds) { - var durationHHMMSS = new FFProbeStream { Duration = "05:12:59.177" }; - var longDuration = new FFProbeStream { Duration = "149:07:50.911750" }; - var shortDuration = new FFProbeStream { Duration = "00:00:00.83" }; + var ffprobeStream = new FFProbeStream { Duration = duration }; - var testdurationHHMMSS = MediaAnalysis.ParseDuration(durationHHMMSS); - var testlongDuration = MediaAnalysis.ParseDuration(longDuration); - var testshortDuration = MediaAnalysis.ParseDuration(shortDuration); + var parsedDuration = MediaAnalysisUtils.ParseDuration(ffprobeStream); - Assert.IsTrue(testdurationHHMMSS.Days == 0 && testdurationHHMMSS.Hours == 5 && testdurationHHMMSS.Minutes == 12 && testdurationHHMMSS.Seconds == 59 && testdurationHHMMSS.Milliseconds == 177); - Assert.IsTrue(testlongDuration.Days == 6 && testlongDuration.Hours == 5 && testlongDuration.Minutes == 7 && testlongDuration.Seconds == 50 && testlongDuration.Milliseconds == 911); - Assert.IsTrue(testdurationHHMMSS.Days == 0 && testshortDuration.Hours == 0 && testshortDuration.Minutes == 0 && testshortDuration.Seconds == 0 && testshortDuration.Milliseconds == 830); + Assert.AreEqual(parsedDuration.Days, expectedDays); + Assert.AreEqual(parsedDuration.Hours, expectedHours); + Assert.AreEqual(parsedDuration.Minutes, expectedMinutes); + Assert.AreEqual(parsedDuration.Seconds, expectedSeconds); + Assert.AreEqual(parsedDuration.Milliseconds, expectedMilliseconds); } [TestMethod] diff --git a/FFMpegCore/FFMpeg/FFMpeg.cs b/FFMpegCore/FFMpeg/FFMpeg.cs index 3f3d162..42c344b 100644 --- a/FFMpegCore/FFMpeg/FFMpeg.cs +++ b/FFMpegCore/FFMpeg/FFMpeg.cs @@ -163,8 +163,8 @@ public static bool Convert( var source = FFProbe.Analyse(input); FFMpegHelper.ConversionSizeExceptionCheck(source); - var scale = VideoSize.Original == size ? 1 : (double)source.PrimaryVideoStream.Height / (int)size; - var outputSize = new Size((int)(source.PrimaryVideoStream.Width / scale), (int)(source.PrimaryVideoStream.Height / scale)); + var scale = VideoSize.Original == size ? 1 : (double)source.PrimaryVideoStream!.Height / (int)size; + var outputSize = new Size((int)(source.PrimaryVideoStream!.Width / scale), (int)(source.PrimaryVideoStream.Height / scale)); if (outputSize.Width % 2 != 0) outputSize.Width += 1; diff --git a/FFMpegCore/FFProbe/MediaAnalysis.cs b/FFMpegCore/FFProbe/MediaAnalysis.cs index 772997a..2602f86 100644 --- a/FFMpegCore/FFProbe/MediaAnalysis.cs +++ b/FFMpegCore/FFProbe/MediaAnalysis.cs @@ -18,7 +18,7 @@ private MediaFormat ParseFormat(Format analysisFormat) { return new MediaFormat { - Duration = ParseDuration(analysisFormat.Duration), + Duration = MediaAnalysisUtils.ParseDuration(analysisFormat.Duration), FormatName = analysisFormat.FormatName, FormatLongName = analysisFormat.FormatLongName, StreamCount = analysisFormat.NbStreams, @@ -89,7 +89,7 @@ private AudioStream ParseAudioStream(FFProbeStream stream) public static class MediaAnalysisUtils { - private static readonly Regex DurationRegex = new Regex("^(\\d{1,5}:\\d{1,2}:\\d{1,2}(.\\d{1,7})?)", RegexOptions.Compiled); + private static readonly Regex DurationRegex = new Regex(@"^(\d+):(\d{1,2}):(\d{1,2})\.(\d{1,3})", RegexOptions.Compiled); public static double DivideRatio((double, double) ratio) => ratio.Item1 / ratio.Item2; @@ -150,11 +150,5 @@ public static TimeSpan ParseDuration(FFProbeStream ffProbeStream) { return ParseDuration(ffProbeStream.Duration); } - - private static string? TrimTimeSpan(string? durationTag) - { - var durationMatch = DurationRegex.Match(durationTag ?? ""); - return durationMatch.Success ? durationMatch.Groups[1].Value : null; - } } } \ No newline at end of file