From a4c7d87e62ef5dce7c88c92c51cb09b004424e8a Mon Sep 17 00:00:00 2001 From: Cry dsch Date: Wed, 12 Feb 2020 21:26:27 +0100 Subject: [PATCH] Fix duration parsing of .mkv and .webm files --- 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);