From 37fc05b26b8823233e0f8e4b07e9d65bb4bed2fd Mon Sep 17 00:00:00 2001 From: Cry dsch Date: Wed, 12 Feb 2020 21:26:27 +0100 Subject: [PATCH 1/4] Fix duration parsing of .mkv and .webm files Former-commit-id: a4c7d87e62ef5dce7c88c92c51cb09b004424e8a --- FFMpegCore/FFMPEG/FFMpegStreamMetadata.cs | 10 ++++++++++ FFMpegCore/FFMPEG/FFProbe.cs | 17 ++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/FFMpegCore/FFMPEG/FFMpegStreamMetadata.cs b/FFMpegCore/FFMPEG/FFMpegStreamMetadata.cs index 68269cf..11fbf6f 100644 --- a/FFMpegCore/FFMPEG/FFMpegStreamMetadata.cs +++ b/FFMpegCore/FFMPEG/FFMpegStreamMetadata.cs @@ -31,8 +31,18 @@ internal class Stream [JsonProperty("r_frame_rate")] internal string FrameRate { get; set; } + + [JsonProperty("tags")] + internal Tags Tags { get; set; } } + internal class Tags + { + [JsonProperty("DURATION")] + internal string Duration { get; set; } + } + + internal class FFMpegStreamMetadata { [JsonProperty("streams")] diff --git a/FFMpegCore/FFMPEG/FFProbe.cs b/FFMpegCore/FFMPEG/FFProbe.cs index 095a7d2..f4299af 100644 --- a/FFMpegCore/FFMPEG/FFProbe.cs +++ b/FFMpegCore/FFMPEG/FFProbe.cs @@ -79,9 +79,20 @@ private VideoInfo ParseVideoInfoInternal(VideoInfo info, string probeOutput) double videoSize = 0d; double audioSize = 0d; - var duration = TimeSpan.FromSeconds(double.TryParse((video ?? audio).Duration, NumberStyles.Any, CultureInfo.InvariantCulture, out var output) ? output : 0); - info.Duration = duration.Subtract(TimeSpan.FromMilliseconds(duration.Milliseconds)); - + string sDuration = (video ?? audio).Duration; + TimeSpan duration; + if (sDuration != null) + { + duration = TimeSpan.FromSeconds(double.TryParse(sDuration, NumberStyles.Any, CultureInfo.InvariantCulture, out var output) ? output : 0); + } + else + { + sDuration = (video ?? audio).Tags.Duration; + TimeSpan.TryParse(sDuration.Remove(sDuration.LastIndexOf('.')), CultureInfo.InvariantCulture, out duration); + } + // Strip milliseconds and additional ticks + info.Duration = new TimeSpan(duration.Days, duration.Hours, duration.Minutes, duration.Seconds); + if (video != null) { var bitRate = Convert.ToDouble(video.BitRate, CultureInfo.InvariantCulture); From ed5fffd7a9ac83404d9653be85036a7ad818145b Mon Sep 17 00:00:00 2001 From: Cry dsch Date: Wed, 12 Feb 2020 21:37:10 +0100 Subject: [PATCH 2/4] Fix duration parsing of .mkv and .webm files Former-commit-id: fbd9f3fc893d28118e54f3963c9ee576929d2022 --- FFMpegCore/FFMPEG/FFMpegStreamMetadata.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/FFMpegCore/FFMPEG/FFMpegStreamMetadata.cs b/FFMpegCore/FFMPEG/FFMpegStreamMetadata.cs index 11fbf6f..3a85b7a 100644 --- a/FFMpegCore/FFMPEG/FFMpegStreamMetadata.cs +++ b/FFMpegCore/FFMPEG/FFMpegStreamMetadata.cs @@ -42,7 +42,6 @@ internal class Tags internal string Duration { get; set; } } - internal class FFMpegStreamMetadata { [JsonProperty("streams")] From 073cfee5ea275494769b31cc2c77e22d33835313 Mon Sep 17 00:00:00 2001 From: Crydsch Date: Thu, 13 Feb 2020 17:36:49 +0100 Subject: [PATCH 3/4] Lift restriction of duration to seconds Former-commit-id: 0b35935b5d1016c0779c840a4147c9df89fda7e5 --- FFMpegCore.Test/VideoTest.cs | 16 ++++++++++++---- FFMpegCore/FFMPEG/FFProbe.cs | 5 ++--- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/FFMpegCore.Test/VideoTest.cs b/FFMpegCore.Test/VideoTest.cs index 88510e6..ec781aa 100644 --- a/FFMpegCore.Test/VideoTest.cs +++ b/FFMpegCore.Test/VideoTest.cs @@ -260,7 +260,11 @@ public void Video_Join() var result = Encoder.Join(output, input, input2); Assert.IsTrue(File.Exists(output.FullName)); - Assert.AreEqual(input.Duration.TotalSeconds * 2, result.Duration.TotalSeconds); + TimeSpan expectedDuration = input.Duration * 2; + Assert.AreEqual(expectedDuration.Days, result.Duration.Days); + Assert.AreEqual(expectedDuration.Hours, result.Duration.Hours); + Assert.AreEqual(expectedDuration.Minutes, result.Duration.Minutes); + Assert.AreEqual(expectedDuration.Seconds, result.Duration.Seconds); Assert.AreEqual(input.Height, result.Height); Assert.AreEqual(input.Width, result.Width); } @@ -316,10 +320,10 @@ public void Video_With_Only_Audio_Should_Extract_Metadata() var video = VideoInfo.FromFileInfo(VideoLibrary.LocalVideoAudioOnly); Assert.AreEqual(video.VideoFormat, "none"); Assert.AreEqual(video.AudioFormat, "aac"); - Assert.AreEqual(video.Duration.TotalSeconds, 79); + Assert.AreEqual(video.Duration.TotalSeconds, 79.451); Assert.AreEqual(video.Size, 1.25); } - + [TestMethod] public void Video_Duration() { var video = VideoInfo.FromFileInfo(VideoLibrary.LocalVideo); @@ -335,7 +339,11 @@ public void Video_Duration() { Assert.IsTrue(File.Exists(output.FullName)); var outputVideo = new VideoInfo(output.FullName); - Assert.AreEqual(video.Duration.TotalSeconds - 5, outputVideo.Duration.TotalSeconds); + + Assert.AreEqual(video.Duration.Days, outputVideo.Duration.Days); + Assert.AreEqual(video.Duration.Hours, outputVideo.Duration.Hours); + Assert.AreEqual(video.Duration.Minutes, outputVideo.Duration.Minutes); + Assert.AreEqual(video.Duration.Seconds - 5, outputVideo.Duration.Seconds); } finally { if (File.Exists(output.FullName)) output.Delete(); diff --git a/FFMpegCore/FFMPEG/FFProbe.cs b/FFMpegCore/FFMPEG/FFProbe.cs index a3c1d08..2c69aff 100644 --- a/FFMpegCore/FFMPEG/FFProbe.cs +++ b/FFMpegCore/FFMPEG/FFProbe.cs @@ -85,10 +85,9 @@ private VideoInfo ParseVideoInfoInternal(VideoInfo info, string probeOutput) else { sDuration = (video ?? audio).Tags.Duration; - TimeSpan.TryParse(sDuration.Remove(sDuration.LastIndexOf('.')), CultureInfo.InvariantCulture, out duration); + TimeSpan.TryParse(sDuration.Remove(sDuration.LastIndexOf('.') + 8), CultureInfo.InvariantCulture, out duration); // TimeSpan fractions only allow up to 7 digits } - // Strip milliseconds and additional ticks - info.Duration = new TimeSpan(duration.Days, duration.Hours, duration.Minutes, duration.Seconds); + info.Duration = duration; if (video != null) { From 703da64f44dfba1411fd681c2ac9145c6e29ad95 Mon Sep 17 00:00:00 2001 From: Crydsch Date: Thu, 13 Feb 2020 20:58:14 +0100 Subject: [PATCH 4/4] Fix NullReferenceException if no duration exists Former-commit-id: 5daef17b4443b93c1759fb39256cf2befdbeb873 --- FFMpegCore/FFMPEG/FFProbe.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/FFMpegCore/FFMPEG/FFProbe.cs b/FFMpegCore/FFMPEG/FFProbe.cs index 2c69aff..b4e0012 100644 --- a/FFMpegCore/FFMPEG/FFProbe.cs +++ b/FFMpegCore/FFMPEG/FFProbe.cs @@ -77,7 +77,7 @@ private VideoInfo ParseVideoInfoInternal(VideoInfo info, string probeOutput) double audioSize = 0d; string sDuration = (video ?? audio).Duration; - TimeSpan duration; + TimeSpan duration = TimeSpan.Zero; if (sDuration != null) { duration = TimeSpan.FromSeconds(double.TryParse(sDuration, NumberStyles.Any, CultureInfo.InvariantCulture, out var output) ? output : 0); @@ -85,7 +85,8 @@ private VideoInfo ParseVideoInfoInternal(VideoInfo info, string probeOutput) else { sDuration = (video ?? audio).Tags.Duration; - TimeSpan.TryParse(sDuration.Remove(sDuration.LastIndexOf('.') + 8), CultureInfo.InvariantCulture, out duration); // TimeSpan fractions only allow up to 7 digits + if (sDuration != null) + TimeSpan.TryParse(sDuration.Remove(sDuration.LastIndexOf('.') + 8), CultureInfo.InvariantCulture, out duration); // TimeSpan fractions only allow up to 7 digits } info.Duration = duration;