FFProbeHelpers.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections.Generic;
  3. namespace MediaBrowser.MediaEncoding.Probing
  4. {
  5. public static class FFProbeHelpers
  6. {
  7. /// <summary>
  8. /// Normalizes the FF probe result.
  9. /// </summary>
  10. /// <param name="result">The result.</param>
  11. public static void NormalizeFFProbeResult(InternalMediaInfoResult result)
  12. {
  13. if (result == null)
  14. {
  15. throw new ArgumentNullException(nameof(result));
  16. }
  17. if (result.Format != null && result.Format.Tags != null)
  18. {
  19. result.Format.Tags = ConvertDictionaryToCaseInsensitive(result.Format.Tags);
  20. }
  21. if (result.Streams != null)
  22. {
  23. // Convert all dictionaries to case insensitive
  24. foreach (var stream in result.Streams)
  25. {
  26. if (stream.Tags != null)
  27. {
  28. stream.Tags = ConvertDictionaryToCaseInsensitive(stream.Tags);
  29. }
  30. }
  31. }
  32. }
  33. /// <summary>
  34. /// Gets a string from an FFProbeResult tags dictionary.
  35. /// </summary>
  36. /// <param name="tags">The tags.</param>
  37. /// <param name="key">The key.</param>
  38. /// <returns>System.String.</returns>
  39. public static string GetDictionaryValue(IReadOnlyDictionary<string, string> tags, string key)
  40. {
  41. if (tags == null)
  42. {
  43. return null;
  44. }
  45. tags.TryGetValue(key, out var val);
  46. return val;
  47. }
  48. /// <summary>
  49. /// Gets an int from an FFProbeResult tags dictionary.
  50. /// </summary>
  51. /// <param name="tags">The tags.</param>
  52. /// <param name="key">The key.</param>
  53. /// <returns>System.Nullable{System.Int32}.</returns>
  54. public static int? GetDictionaryNumericValue(Dictionary<string, string> tags, string key)
  55. {
  56. var val = GetDictionaryValue(tags, key);
  57. if (!string.IsNullOrEmpty(val))
  58. {
  59. if (int.TryParse(val, out var i))
  60. {
  61. return i;
  62. }
  63. }
  64. return null;
  65. }
  66. /// <summary>
  67. /// Gets a DateTime from an FFProbeResult tags dictionary.
  68. /// </summary>
  69. /// <param name="tags">The tags.</param>
  70. /// <param name="key">The key.</param>
  71. /// <returns>System.Nullable{DateTime}.</returns>
  72. public static DateTime? GetDictionaryDateTime(Dictionary<string, string> tags, string key)
  73. {
  74. var val = GetDictionaryValue(tags, key);
  75. if (!string.IsNullOrEmpty(val))
  76. {
  77. if (DateTime.TryParse(val, out var i))
  78. {
  79. return i.ToUniversalTime();
  80. }
  81. }
  82. return null;
  83. }
  84. /// <summary>
  85. /// Converts a dictionary to case insensitive.
  86. /// </summary>
  87. /// <param name="dict">The dict.</param>
  88. /// <returns>Dictionary{System.StringSystem.String}.</returns>
  89. private static Dictionary<string, string> ConvertDictionaryToCaseInsensitive(IReadOnlyDictionary<string, string> dict)
  90. {
  91. return new Dictionary<string, string>(dict, StringComparer.OrdinalIgnoreCase);
  92. }
  93. }
  94. }