From f01d514542df5c5673f7f4ddbcb38aab6528dc4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=91=D0=B0=D0=B3?= =?UTF-8?q?=D1=80=D1=8F=D0=BD=D1=86=D0=B5=D0=B2?= Date: Sun, 10 May 2020 18:55:42 +0300 Subject: [PATCH] Fix FormatException when parsing doubles on systems whith ',' whole and fractional parts separator Former-commit-id: 54c30c0e1e55c6807633ed3753431cd3f1efb0ae --- FFMpegCore/FFProbe/MediaAnalysis.cs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/FFMpegCore/FFProbe/MediaAnalysis.cs b/FFMpegCore/FFProbe/MediaAnalysis.cs index 223abef..4b638b2 100644 --- a/FFMpegCore/FFProbe/MediaAnalysis.cs +++ b/FFMpegCore/FFProbe/MediaAnalysis.cs @@ -35,8 +35,8 @@ private VideoStream ParseVideoStream(Stream stream) { Index = stream.Index, AvgFrameRate = DivideRatio(ParseRatioDouble(stream.AvgFrameRate, '/')), - BitRate = !string.IsNullOrEmpty(stream.BitRate) ? int.Parse(stream.BitRate) : default, - BitsPerRawSample = !string.IsNullOrEmpty(stream.BitsPerRawSample) ? int.Parse(stream.BitsPerRawSample) : default, + BitRate = !string.IsNullOrEmpty(stream.BitRate) ? ParseIntInvariant(stream.BitRate) : default, + BitsPerRawSample = !string.IsNullOrEmpty(stream.BitsPerRawSample) ? ParseIntInvariant(stream.BitsPerRawSample) : default, CodecName = stream.CodecName, CodecLongName = stream.CodecLongName, DisplayAspectRatio = ParseRatioInt(stream.DisplayAspectRatio, ':'), @@ -52,7 +52,7 @@ private VideoStream ParseVideoStream(Stream stream) private static TimeSpan ParseDuration(Stream stream) { return stream.Duration != null - ? TimeSpan.FromSeconds(double.Parse(stream.Duration)) + ? TimeSpan.FromSeconds(ParseDoubleInvariant(stream.Duration)) : TimeSpan.Parse(stream.Tags.Duration ?? "0"); } @@ -61,13 +61,13 @@ private AudioStream ParseAudioStream(Stream stream) return new AudioStream { Index = stream.Index, - BitRate = !string.IsNullOrEmpty(stream.BitRate) ? int.Parse(stream.BitRate) : default, + BitRate = !string.IsNullOrEmpty(stream.BitRate) ? ParseIntInvariant(stream.BitRate) : default, CodecName = stream.CodecName, CodecLongName = stream.CodecLongName, Channels = stream.Channels ?? default, ChannelLayout = stream.ChannelLayout, - Duration = TimeSpan.FromSeconds(double.Parse(stream.Duration ?? stream.Tags.Duration ?? "0")), - SampleRateHz = !string.IsNullOrEmpty(stream.SampleRate) ? int.Parse(stream.SampleRate) : default + Duration = TimeSpan.FromSeconds(ParseDoubleInvariant(stream.Duration ?? stream.Tags.Duration ?? "0")), + SampleRateHz = !string.IsNullOrEmpty(stream.SampleRate) ? ParseIntInvariant(stream.SampleRate) : default }; } @@ -76,13 +76,18 @@ private static (int, int) ParseRatioInt(string input, char separator) { if (string.IsNullOrEmpty(input)) return (0, 0); var ratio = input.Split(separator); - return (int.Parse(ratio[0]), int.Parse(ratio[1])); + 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 ? double.Parse(ratio[0]) : 0, ratio.Length > 1 ? double.Parse(ratio[1]) : 0); + 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