From d79bbaef97f7bc4b043a4b4266db5d4d395b5852 Mon Sep 17 00:00:00 2001 From: alex6dj Date: Tue, 7 Sep 2021 12:48:05 -0400 Subject: [PATCH] Use boolean instead on generic int for disposition state value --- FFMpegCore.Test/FFProbeTests.cs | 5 +++-- FFMpegCore/FFProbe/MediaAnalysis.cs | 31 ++++++++++++++++++++++++++--- FFMpegCore/FFProbe/MediaStream.cs | 2 +- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/FFMpegCore.Test/FFProbeTests.cs b/FFMpegCore.Test/FFProbeTests.cs index 91411d4..aaadd4c 100644 --- a/FFMpegCore.Test/FFProbeTests.cs +++ b/FFMpegCore.Test/FFProbeTests.cs @@ -120,8 +120,9 @@ public async Task Probe_Success_Disposition_Async() { var info = await FFProbe.AnalyseAsync(TestResources.Mp4Video); Assert.IsNotNull(info.PrimaryAudioStream); - Assert.AreEqual(1, info.PrimaryAudioStream.Disposition["default"]); - Assert.AreEqual(0, info.PrimaryAudioStream.Disposition["forced"]); + Assert.IsNotNull(info.PrimaryAudioStream.Disposition); + Assert.AreEqual(true, info.PrimaryAudioStream.Disposition["default"]); + Assert.AreEqual(false, info.PrimaryAudioStream.Disposition["forced"]); } } } \ No newline at end of file diff --git a/FFMpegCore/FFProbe/MediaAnalysis.cs b/FFMpegCore/FFProbe/MediaAnalysis.cs index 174eb7e..f7338cc 100644 --- a/FFMpegCore/FFProbe/MediaAnalysis.cs +++ b/FFMpegCore/FFProbe/MediaAnalysis.cs @@ -67,7 +67,7 @@ private VideoStream ParseVideoStream(FFProbeStream stream) PixelFormat = stream.PixelFormat, Rotation = (int)float.Parse(stream.GetRotate() ?? "0"), Language = stream.GetLanguage(), - Disposition = stream.Disposition, + Disposition = MediaAnalysisUtils.FormatDisposition(stream.Disposition), Tags = stream.Tags, }; } @@ -88,7 +88,7 @@ private AudioStream ParseAudioStream(FFProbeStream stream) SampleRateHz = !string.IsNullOrEmpty(stream.SampleRate) ? MediaAnalysisUtils.ParseIntInvariant(stream.SampleRate) : default, Profile = stream.Profile, Language = stream.GetLanguage(), - Disposition = stream.Disposition, + Disposition = MediaAnalysisUtils.FormatDisposition(stream.Disposition), Tags = stream.Tags, }; } @@ -103,7 +103,7 @@ private SubtitleStream ParseSubtitleStream(FFProbeStream stream) CodecLongName = stream.CodecLongName, Duration = MediaAnalysisUtils.ParseDuration(stream), Language = stream.GetLanguage(), - Disposition = stream.Disposition, + Disposition = MediaAnalysisUtils.FormatDisposition(stream.Disposition), Tags = stream.Tags, }; } @@ -172,5 +172,30 @@ public static TimeSpan ParseDuration(FFProbeStream ffProbeStream) { return ParseDuration(ffProbeStream.Duration); } + + public static Dictionary? FormatDisposition(Dictionary? disposition) + { + if (disposition == null) + { + return null; + } + + var result = new Dictionary(disposition.Count); + + foreach (var pair in disposition) + { + result.Add(pair.Key, ToBool(pair.Value)); + } + + static bool ToBool(int value) => value switch + { + 0 => false, + 1 => true, + _ => throw new ArgumentOutOfRangeException(nameof(value), + $"Not expected disposition state value: {value}") + }; + + return result; + } } } \ No newline at end of file diff --git a/FFMpegCore/FFProbe/MediaStream.cs b/FFMpegCore/FFProbe/MediaStream.cs index 9681ac9..7d6ad20 100644 --- a/FFMpegCore/FFProbe/MediaStream.cs +++ b/FFMpegCore/FFProbe/MediaStream.cs @@ -15,7 +15,7 @@ public class MediaStream public int BitRate { get; internal set; } public TimeSpan Duration { get; internal set; } public string? Language { get; internal set; } - public Dictionary? Disposition { get; internal set; } + public Dictionary? Disposition { get; internal set; } public Dictionary? Tags { get; internal set; } public Codec GetCodecInfo() => FFMpeg.GetCodec(CodecName);