From 8a314f02ae9b0ce037998b66764c150ed0a1afc8 Mon Sep 17 00:00:00 2001 From: Malte Rosenbjerg Date: Mon, 15 Mar 2021 23:17:56 +0100 Subject: [PATCH] Move MediaAnalysis parsing helper methods to static class --- FFMpegCore/FFProbe/MediaAnalysis.cs | 105 +++++++++++++++------------- 1 file changed, 56 insertions(+), 49 deletions(-) diff --git a/FFMpegCore/FFProbe/MediaAnalysis.cs b/FFMpegCore/FFProbe/MediaAnalysis.cs index f1b2f82..1ee7b18 100644 --- a/FFMpegCore/FFProbe/MediaAnalysis.cs +++ b/FFMpegCore/FFProbe/MediaAnalysis.cs @@ -7,8 +7,6 @@ namespace FFMpegCore { internal class MediaAnalysis : IMediaAnalysis { - private static readonly Regex DurationRegex = new Regex("^(\\d{1,2}:\\d{1,2}:\\d{1,2}(.\\d{1,7})?)", RegexOptions.Compiled); - internal MediaAnalysis(FFProbeAnalysis analysis) { Format = ParseFormat(analysis.Format); @@ -50,14 +48,14 @@ private VideoStream ParseVideoStream(FFProbeStream stream) return new VideoStream { Index = stream.Index, - AvgFrameRate = DivideRatio(ParseRatioDouble(stream.AvgFrameRate, '/')), - BitRate = !string.IsNullOrEmpty(stream.BitRate) ? ParseIntInvariant(stream.BitRate) : default, - BitsPerRawSample = !string.IsNullOrEmpty(stream.BitsPerRawSample) ? ParseIntInvariant(stream.BitsPerRawSample) : default, + AvgFrameRate = MediaAnalysisUtils.DivideRatio(MediaAnalysisUtils.ParseRatioDouble(stream.AvgFrameRate, '/')), + BitRate = !string.IsNullOrEmpty(stream.BitRate) ? MediaAnalysisUtils.ParseIntInvariant(stream.BitRate) : default, + BitsPerRawSample = !string.IsNullOrEmpty(stream.BitsPerRawSample) ? MediaAnalysisUtils.ParseIntInvariant(stream.BitsPerRawSample) : default, CodecName = stream.CodecName, CodecLongName = stream.CodecLongName, - DisplayAspectRatio = ParseRatioInt(stream.DisplayAspectRatio, ':'), - Duration = ParseDuration(stream), - FrameRate = DivideRatio(ParseRatioDouble(stream.FrameRate, '/')), + DisplayAspectRatio = MediaAnalysisUtils.ParseRatioInt(stream.DisplayAspectRatio, ':'), + Duration = MediaAnalysisUtils.ParseDuration(stream), + FrameRate = MediaAnalysisUtils.DivideRatio(MediaAnalysisUtils.ParseRatioDouble(stream.FrameRate, '/')), Height = stream.Height ?? 0, Width = stream.Width ?? 0, Profile = stream.Profile, @@ -68,7 +66,56 @@ private VideoStream ParseVideoStream(FFProbeStream stream) }; } - private static TimeSpan ParseDuration(FFProbeStream ffProbeStream) + + private AudioStream ParseAudioStream(FFProbeStream stream) + { + return new AudioStream + { + Index = stream.Index, + BitRate = !string.IsNullOrEmpty(stream.BitRate) ? MediaAnalysisUtils.ParseIntInvariant(stream.BitRate) : default, + CodecName = stream.CodecName, + CodecLongName = stream.CodecLongName, + Channels = stream.Channels ?? default, + ChannelLayout = stream.ChannelLayout, + Duration = MediaAnalysisUtils.ParseDuration(stream), + SampleRateHz = !string.IsNullOrEmpty(stream.SampleRate) ? MediaAnalysisUtils.ParseIntInvariant(stream.SampleRate) : default, + Profile = stream.Profile, + Language = stream.GetLanguage(), + Tags = stream.Tags, + }; + } + + + } + + 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); + + public static double DivideRatio((double, double) ratio) => ratio.Item1 / ratio.Item2; + + public static (int, int) ParseRatioInt(string input, char separator) + { + if (string.IsNullOrEmpty(input)) return (0, 0); + var ratio = input.Split(separator); + return (ParseIntInvariant(ratio[0]), ParseIntInvariant(ratio[1])); + } + + public static (double, double) ParseRatioDouble(string input, char separator) + { + if (string.IsNullOrEmpty(input)) return (0, 0); + var ratio = input.Split(separator); + return (ratio.Length > 0 ? ParseDoubleInvariant(ratio[0]) : 0, ratio.Length > 1 ? ParseDoubleInvariant(ratio[1]) : 0); + } + + public static double ParseDoubleInvariant(string line) => + double.Parse(line, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture); + + public static int ParseIntInvariant(string line) => + int.Parse(line, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture); + + + public static TimeSpan ParseDuration(FFProbeStream ffProbeStream) { return !string.IsNullOrEmpty(ffProbeStream.Duration) ? TimeSpan.Parse(ffProbeStream.Duration) @@ -80,45 +127,5 @@ private static TimeSpan ParseDuration(FFProbeStream ffProbeStream) var durationMatch = DurationRegex.Match(durationTag ?? ""); return durationMatch.Success ? durationMatch.Groups[1].Value : null; } - - private AudioStream ParseAudioStream(FFProbeStream stream) - { - return new AudioStream - { - Index = stream.Index, - BitRate = !string.IsNullOrEmpty(stream.BitRate) ? ParseIntInvariant(stream.BitRate) : default, - CodecName = stream.CodecName, - CodecLongName = stream.CodecLongName, - Channels = stream.Channels ?? default, - ChannelLayout = stream.ChannelLayout, - Duration = ParseDuration(stream), - SampleRateHz = !string.IsNullOrEmpty(stream.SampleRate) ? ParseIntInvariant(stream.SampleRate) : default, - Profile = stream.Profile, - Language = stream.GetLanguage(), - Tags = stream.Tags, - }; - } - - private static double DivideRatio((double, double) ratio) => ratio.Item1 / ratio.Item2; - - private static (int, int) ParseRatioInt(string input, char separator) - { - if (string.IsNullOrEmpty(input)) return (0, 0); - var ratio = input.Split(separator); - return (ParseIntInvariant(ratio[0]), ParseIntInvariant(ratio[1])); - } - - private static (double, double) ParseRatioDouble(string input, char separator) - { - if (string.IsNullOrEmpty(input)) return (0, 0); - var ratio = input.Split(separator); - return (ratio.Length > 0 ? ParseDoubleInvariant(ratio[0]) : 0, ratio.Length > 1 ? ParseDoubleInvariant(ratio[1]) : 0); - } - - private static double ParseDoubleInvariant(string line) => - double.Parse(line, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture); - - private static int ParseIntInvariant(string line) => - int.Parse(line, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture); } } \ No newline at end of file