Use boolean instead on generic int for disposition state value

This commit is contained in:
alex6dj 2021-09-07 12:48:05 -04:00
parent 41ec1a10dd
commit d79bbaef97
3 changed files with 32 additions and 6 deletions

View file

@ -120,8 +120,9 @@ public async Task Probe_Success_Disposition_Async()
{ {
var info = await FFProbe.AnalyseAsync(TestResources.Mp4Video); var info = await FFProbe.AnalyseAsync(TestResources.Mp4Video);
Assert.IsNotNull(info.PrimaryAudioStream); Assert.IsNotNull(info.PrimaryAudioStream);
Assert.AreEqual(1, info.PrimaryAudioStream.Disposition["default"]); Assert.IsNotNull(info.PrimaryAudioStream.Disposition);
Assert.AreEqual(0, info.PrimaryAudioStream.Disposition["forced"]); Assert.AreEqual(true, info.PrimaryAudioStream.Disposition["default"]);
Assert.AreEqual(false, info.PrimaryAudioStream.Disposition["forced"]);
} }
} }
} }

View file

@ -67,7 +67,7 @@ private VideoStream ParseVideoStream(FFProbeStream stream)
PixelFormat = stream.PixelFormat, PixelFormat = stream.PixelFormat,
Rotation = (int)float.Parse(stream.GetRotate() ?? "0"), Rotation = (int)float.Parse(stream.GetRotate() ?? "0"),
Language = stream.GetLanguage(), Language = stream.GetLanguage(),
Disposition = stream.Disposition, Disposition = MediaAnalysisUtils.FormatDisposition(stream.Disposition),
Tags = stream.Tags, Tags = stream.Tags,
}; };
} }
@ -88,7 +88,7 @@ private AudioStream ParseAudioStream(FFProbeStream stream)
SampleRateHz = !string.IsNullOrEmpty(stream.SampleRate) ? MediaAnalysisUtils.ParseIntInvariant(stream.SampleRate) : default, SampleRateHz = !string.IsNullOrEmpty(stream.SampleRate) ? MediaAnalysisUtils.ParseIntInvariant(stream.SampleRate) : default,
Profile = stream.Profile, Profile = stream.Profile,
Language = stream.GetLanguage(), Language = stream.GetLanguage(),
Disposition = stream.Disposition, Disposition = MediaAnalysisUtils.FormatDisposition(stream.Disposition),
Tags = stream.Tags, Tags = stream.Tags,
}; };
} }
@ -103,7 +103,7 @@ private SubtitleStream ParseSubtitleStream(FFProbeStream stream)
CodecLongName = stream.CodecLongName, CodecLongName = stream.CodecLongName,
Duration = MediaAnalysisUtils.ParseDuration(stream), Duration = MediaAnalysisUtils.ParseDuration(stream),
Language = stream.GetLanguage(), Language = stream.GetLanguage(),
Disposition = stream.Disposition, Disposition = MediaAnalysisUtils.FormatDisposition(stream.Disposition),
Tags = stream.Tags, Tags = stream.Tags,
}; };
} }
@ -172,5 +172,30 @@ public static TimeSpan ParseDuration(FFProbeStream ffProbeStream)
{ {
return ParseDuration(ffProbeStream.Duration); return ParseDuration(ffProbeStream.Duration);
} }
public static Dictionary<string, bool>? FormatDisposition(Dictionary<string, int>? disposition)
{
if (disposition == null)
{
return null;
}
var result = new Dictionary<string, bool>(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;
}
} }
} }

View file

@ -15,7 +15,7 @@ public class MediaStream
public int BitRate { get; internal set; } public int BitRate { get; internal set; }
public TimeSpan Duration { get; internal set; } public TimeSpan Duration { get; internal set; }
public string? Language { get; internal set; } public string? Language { get; internal set; }
public Dictionary<string, int>? Disposition { get; internal set; } public Dictionary<string, bool>? Disposition { get; internal set; }
public Dictionary<string, string>? Tags { get; internal set; } public Dictionary<string, string>? Tags { get; internal set; }
public Codec GetCodecInfo() => FFMpeg.GetCodec(CodecName); public Codec GetCodecInfo() => FFMpeg.GetCodec(CodecName);