FFProbeHelpers.cs 3.5 KB

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