Merge pull request #250 from alex6dj/feature/get-default-and-force-value

Get extra disposition data in MediaStream

Former-commit-id: e901009774
This commit is contained in:
Malte Rosenbjerg 2021-09-09 18:50:57 +02:00 committed by GitHub
commit 5e0e9bab8d
4 changed files with 64 additions and 4 deletions

View file

@ -114,5 +114,15 @@ public async Task Probe_Success_Subtitle_Async()
Assert.AreEqual(0, info.AudioStreams.Count);
Assert.AreEqual(0, info.VideoStreams.Count);
}
[TestMethod, Timeout(10000)]
public async Task Probe_Success_Disposition_Async()
{
var info = await FFProbe.AnalyseAsync(TestResources.Mp4Video);
Assert.IsNotNull(info.PrimaryAudioStream);
Assert.IsNotNull(info.PrimaryAudioStream.Disposition);
Assert.AreEqual(true, info.PrimaryAudioStream.Disposition["default"]);
Assert.AreEqual(false, info.PrimaryAudioStream.Disposition["forced"]);
}
}
}

View file

@ -12,7 +12,7 @@ public class FFProbeAnalysis
public Format Format { get; set; } = null!;
}
public class FFProbeStream : ITagsContainer
public class FFProbeStream : ITagsContainer, IDispositionContainer
{
[JsonPropertyName("index")]
public int Index { get; set; }
@ -71,9 +71,13 @@ public class FFProbeStream : ITagsContainer
[JsonPropertyName("sample_rate")]
public string SampleRate { get; set; } = null!;
[JsonPropertyName("disposition")]
public Dictionary<string, int> Disposition { get; set; } = null!;
[JsonPropertyName("tags")]
public Dictionary<string, string> Tags { get; set; } = null!;
}
public class Format : ITagsContainer
{
[JsonPropertyName("filename")]
@ -110,10 +114,16 @@ public class Format : ITagsContainer
public Dictionary<string, string> Tags { get; set; } = null!;
}
public interface IDispositionContainer
{
Dictionary<string, int> Disposition { get; set; }
}
public interface ITagsContainer
{
Dictionary<string, string> Tags { get; set; }
}
public static class TagExtensions
{
private static string? TryGetTagValue(ITagsContainer tagsContainer, string key)
@ -127,7 +137,18 @@ public static class TagExtensions
public static string? GetCreationTime(this ITagsContainer tagsContainer) => TryGetTagValue(tagsContainer, "creation_time ");
public static string? GetRotate(this ITagsContainer tagsContainer) => TryGetTagValue(tagsContainer, "rotate");
public static string? GetDuration(this ITagsContainer tagsContainer) => TryGetTagValue(tagsContainer, "duration");
}
public static class DispositionExtensions
{
private static int? TryGetDispositionValue(IDispositionContainer dispositionContainer, string key)
{
if (dispositionContainer.Disposition != null && dispositionContainer.Disposition.TryGetValue(key, out var dispositionValue))
return dispositionValue;
return null;
}
public static int? GetDefault(this IDispositionContainer tagsContainer) => TryGetDispositionValue(tagsContainer, "default");
public static int? GetForced(this IDispositionContainer tagsContainer) => TryGetDispositionValue(tagsContainer, "forced");
}
}

View file

@ -67,6 +67,7 @@ private VideoStream ParseVideoStream(FFProbeStream stream)
PixelFormat = stream.PixelFormat,
Rotation = (int)float.Parse(stream.GetRotate() ?? "0"),
Language = stream.GetLanguage(),
Disposition = MediaAnalysisUtils.FormatDisposition(stream.Disposition),
Tags = stream.Tags,
};
}
@ -87,6 +88,7 @@ private AudioStream ParseAudioStream(FFProbeStream stream)
SampleRateHz = !string.IsNullOrEmpty(stream.SampleRate) ? MediaAnalysisUtils.ParseIntInvariant(stream.SampleRate) : default,
Profile = stream.Profile,
Language = stream.GetLanguage(),
Disposition = MediaAnalysisUtils.FormatDisposition(stream.Disposition),
Tags = stream.Tags,
};
}
@ -101,6 +103,7 @@ private SubtitleStream ParseSubtitleStream(FFProbeStream stream)
CodecLongName = stream.CodecLongName,
Duration = MediaAnalysisUtils.ParseDuration(stream),
Language = stream.GetLanguage(),
Disposition = MediaAnalysisUtils.FormatDisposition(stream.Disposition),
Tags = stream.Tags,
};
}
@ -169,5 +172,30 @@ public static TimeSpan ParseDuration(FFProbeStream ffProbeStream)
{
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,8 +15,9 @@ public class MediaStream
public int BitRate { get; internal set; }
public TimeSpan Duration { get; internal set; }
public string? Language { get; internal set; }
public Dictionary<string, bool>? Disposition { get; internal set; }
public Dictionary<string, string>? Tags { get; internal set; }
public Codec GetCodecInfo() => FFMpeg.GetCodec(CodecName);
}
}