diff options
| author | Mathieu Velten <matmaul@gmail.com> | 2018-12-14 10:40:55 +0100 |
|---|---|---|
| committer | Mathieu Velten <matmaul@gmail.com> | 2018-12-14 17:32:54 +0100 |
| commit | 1d7d52ff9e42c3efb4bb2c65e82a4a82faf9decb (patch) | |
| tree | 00a3f529458b5e3afa42c97ec4f46e1b65c3cf8e /MediaBrowser.MediaEncoding/Probing/FFProbeHelpers.cs | |
| parent | 64805410c21b1e4717a7f030f619bb2e7bd33d2a (diff) | |
Port MediaEncoding and Api.Playback from 10e57ce8d21b4516733894075001819f3cd6db6b
Diffstat (limited to 'MediaBrowser.MediaEncoding/Probing/FFProbeHelpers.cs')
| -rw-r--r-- | MediaBrowser.MediaEncoding/Probing/FFProbeHelpers.cs | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/MediaBrowser.MediaEncoding/Probing/FFProbeHelpers.cs b/MediaBrowser.MediaEncoding/Probing/FFProbeHelpers.cs new file mode 100644 index 000000000..396c85e21 --- /dev/null +++ b/MediaBrowser.MediaEncoding/Probing/FFProbeHelpers.cs @@ -0,0 +1,117 @@ +using System; +using System.Collections.Generic; + +namespace MediaBrowser.MediaEncoding.Probing +{ + public static class FFProbeHelpers + { + /// <summary> + /// Normalizes the FF probe result. + /// </summary> + /// <param name="result">The result.</param> + public static void NormalizeFFProbeResult(InternalMediaInfoResult result) + { + if (result == null) + { + throw new ArgumentNullException("result"); + } + + if (result.format != null && result.format.tags != null) + { + result.format.tags = ConvertDictionaryToCaseInSensitive(result.format.tags); + } + + if (result.streams != null) + { + // Convert all dictionaries to case insensitive + foreach (var stream in result.streams) + { + if (stream.tags != null) + { + stream.tags = ConvertDictionaryToCaseInSensitive(stream.tags); + } + + if (stream.disposition != null) + { + stream.disposition = ConvertDictionaryToCaseInSensitive(stream.disposition); + } + } + } + } + + /// <summary> + /// Gets a string from an FFProbeResult tags dictionary + /// </summary> + /// <param name="tags">The tags.</param> + /// <param name="key">The key.</param> + /// <returns>System.String.</returns> + public static string GetDictionaryValue(Dictionary<string, string> tags, string key) + { + if (tags == null) + { + return null; + } + + string val; + + tags.TryGetValue(key, out val); + return val; + } + + /// <summary> + /// Gets an int from an FFProbeResult tags dictionary + /// </summary> + /// <param name="tags">The tags.</param> + /// <param name="key">The key.</param> + /// <returns>System.Nullable{System.Int32}.</returns> + public static int? GetDictionaryNumericValue(Dictionary<string, string> tags, string key) + { + var val = GetDictionaryValue(tags, key); + + if (!string.IsNullOrEmpty(val)) + { + int i; + + if (int.TryParse(val, out i)) + { + return i; + } + } + + return null; + } + + /// <summary> + /// Gets a DateTime from an FFProbeResult tags dictionary + /// </summary> + /// <param name="tags">The tags.</param> + /// <param name="key">The key.</param> + /// <returns>System.Nullable{DateTime}.</returns> + public static DateTime? GetDictionaryDateTime(Dictionary<string, string> tags, string key) + { + var val = GetDictionaryValue(tags, key); + + if (!string.IsNullOrEmpty(val)) + { + DateTime i; + + if (DateTime.TryParse(val, out i)) + { + return i.ToUniversalTime(); + } + } + + return null; + } + + /// <summary> + /// Converts a dictionary to case insensitive + /// </summary> + /// <param name="dict">The dict.</param> + /// <returns>Dictionary{System.StringSystem.String}.</returns> + private static Dictionary<string, string> ConvertDictionaryToCaseInSensitive(Dictionary<string, string> dict) + { + return new Dictionary<string, string>(dict, StringComparer.OrdinalIgnoreCase); + } + } +} |
