Make tags more flexible (dictionary)

Former-commit-id: 0c19874be3
This commit is contained in:
Malte Rosenbjerg 2020-08-11 00:46:08 +02:00
parent 385e50bcee
commit e264c93f67
3 changed files with 29 additions and 19 deletions

View file

@ -12,7 +12,7 @@ public class FFProbeAnalysis
public Format Format { get; set; } = null!; public Format Format { get; set; } = null!;
} }
public class FFProbeStream public class FFProbeStream : ITagsContainer
{ {
[JsonPropertyName("index")] [JsonPropertyName("index")]
public int Index { get; set; } public int Index { get; set; }
@ -66,21 +66,9 @@ public class FFProbeStream
public string SampleRate { get; set; } = null!; public string SampleRate { get; set; } = null!;
[JsonPropertyName("tags")] [JsonPropertyName("tags")]
public Tags Tags { get; set; } = null!; public Dictionary<string, string> Tags { get; set; } = null!;
} }
public class Format : ITagsContainer
public class Tags
{
[JsonPropertyName("DURATION")]
public string Duration { get; set; } = null!;
[JsonPropertyName("language")]
public string Language { get; set; } = null!;
[JsonPropertyName("encoder")]
public string Encoder { get; set; } = null!;
}
public class Format
{ {
[JsonPropertyName("filename")] [JsonPropertyName("filename")]
public string Filename { get; set; } = null!; public string Filename { get; set; } = null!;
@ -113,6 +101,26 @@ public class Format
public int ProbeScore { get; set; } public int ProbeScore { get; set; }
[JsonPropertyName("tags")] [JsonPropertyName("tags")]
public Tags Tags { get; set; } = null!; public Dictionary<string, string> Tags { get; set; } = null!;
}
public interface ITagsContainer
{
Dictionary<string, string> Tags { get; set; }
}
public static class TagExtensions
{
private static string? TryGetTagValue(ITagsContainer tagsContainer, string key)
{
if (tagsContainer.Tags != null && tagsContainer.Tags.TryGetValue(key, out var tagValue))
return tagValue;
return null;
}
public static string? GetLanguage(this ITagsContainer tagsContainer) => TryGetTagValue(tagsContainer, "language");
public static string? GetRotate(this ITagsContainer tagsContainer) => TryGetTagValue(tagsContainer, "rotate");
public static string? GetDuration(this ITagsContainer tagsContainer) => TryGetTagValue(tagsContainer, "duration");
} }
} }

View file

@ -66,7 +66,8 @@ private VideoStream ParseVideoStream(FFProbeStream stream)
Width = stream.Width ?? 0, Width = stream.Width ?? 0,
Profile = stream.Profile, Profile = stream.Profile,
PixelFormat = stream.PixelFormat, PixelFormat = stream.PixelFormat,
Language = stream.Tags?.Language Rotation = (int)float.Parse(stream.GetRotate() ?? "0"),
Language = stream.GetLanguage()
}; };
} }
@ -74,7 +75,7 @@ private static TimeSpan ParseDuration(FFProbeStream ffProbeStream)
{ {
return !string.IsNullOrEmpty(ffProbeStream.Duration) return !string.IsNullOrEmpty(ffProbeStream.Duration)
? TimeSpan.Parse(ffProbeStream.Duration) ? TimeSpan.Parse(ffProbeStream.Duration)
: TimeSpan.Parse(TrimTimeSpan(ffProbeStream.Tags?.Duration) ?? "0"); : TimeSpan.Parse(TrimTimeSpan(ffProbeStream.GetDuration()) ?? "0");
} }
private static string? TrimTimeSpan(string? durationTag) private static string? TrimTimeSpan(string? durationTag)
{ {
@ -95,7 +96,7 @@ private AudioStream ParseAudioStream(FFProbeStream stream)
Duration = ParseDuration(stream), Duration = ParseDuration(stream),
SampleRateHz = !string.IsNullOrEmpty(stream.SampleRate) ? ParseIntInvariant(stream.SampleRate) : default, SampleRateHz = !string.IsNullOrEmpty(stream.SampleRate) ? ParseIntInvariant(stream.SampleRate) : default,
Profile = stream.Profile, Profile = stream.Profile,
Language = stream.Tags?.Language Language = stream.GetLanguage()
}; };
} }

View file

@ -12,6 +12,7 @@ public class VideoStream : MediaStream
public int Height { get; internal set; } public int Height { get; internal set; }
public double FrameRate { get; internal set; } public double FrameRate { get; internal set; }
public string PixelFormat { get; internal set; } = null!; public string PixelFormat { get; internal set; } = null!;
public int Rotation { get; set; }
public PixelFormat GetPixelFormatInfo() => FFMpeg.GetPixelFormat(PixelFormat); public PixelFormat GetPixelFormatInfo() => FFMpeg.GetPixelFormat(PixelFormat);
} }