From 5ee26d52add8e601308c8418518af6687e7e6063 Mon Sep 17 00:00:00 2001 From: Malte Rosenbjerg Date: Tue, 16 Jun 2020 07:42:14 +0200 Subject: [PATCH] trim excess fractions of duration tag Former-commit-id: 9be3fc5a0773f9f63df097e1d25ce5a1dc6447b1 --- FFMpegCore/FFProbe/MediaAnalysis.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/FFMpegCore/FFProbe/MediaAnalysis.cs b/FFMpegCore/FFProbe/MediaAnalysis.cs index f314c26..d8661fb 100644 --- a/FFMpegCore/FFProbe/MediaAnalysis.cs +++ b/FFMpegCore/FFProbe/MediaAnalysis.cs @@ -1,11 +1,13 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Text.RegularExpressions; namespace FFMpegCore { public class MediaAnalysis { + private static readonly Regex DurationRegex = new Regex("^(\\d{1,2}:\\d{1,2}:\\d{1,2}(.\\d{1,7})?)", RegexOptions.Compiled); internal MediaAnalysis(string path, FFProbeAnalysis analysis) { VideoStreams = analysis.Streams.Where(stream => stream.CodecType == "video").Select(ParseVideoStream).ToList(); @@ -54,7 +56,12 @@ private static TimeSpan ParseDuration(FFProbeStream ffProbeStream) { return ffProbeStream.Duration != null ? TimeSpan.FromSeconds(ParseDoubleInvariant(ffProbeStream.Duration)) - : TimeSpan.Parse(ffProbeStream.Tags?.Duration ?? "0"); + : TimeSpan.Parse(TrimTimeSpan(ffProbeStream.Tags?.Duration) ?? "0"); + } + private static string? TrimTimeSpan(string? durationTag) + { + var durationMatch = DurationRegex.Match(durationTag ?? ""); + return durationMatch.Success ? durationMatch.Groups[1].Value : null; } private AudioStream ParseAudioStream(FFProbeStream stream) @@ -67,7 +74,7 @@ private AudioStream ParseAudioStream(FFProbeStream stream) CodecLongName = stream.CodecLongName, Channels = stream.Channels ?? default, ChannelLayout = stream.ChannelLayout, - Duration = TimeSpan.FromSeconds(ParseDoubleInvariant(stream.Duration ?? stream.Tags?.Duration ?? "0")), + Duration = ParseDuration(stream), SampleRateHz = !string.IsNullOrEmpty(stream.SampleRate) ? ParseIntInvariant(stream.SampleRate) : default, Language = stream.Tags?.Language };