FFProbeHelpers.cs 3.6 KB

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