2
0

FFProbeHelpers.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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("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. if (stream.disposition != null)
  31. {
  32. stream.disposition = ConvertDictionaryToCaseInSensitive(stream.disposition);
  33. }
  34. }
  35. }
  36. }
  37. /// <summary>
  38. /// Gets a string from an FFProbeResult tags dictionary
  39. /// </summary>
  40. /// <param name="tags">The tags.</param>
  41. /// <param name="key">The key.</param>
  42. /// <returns>System.String.</returns>
  43. public static string GetDictionaryValue(Dictionary<string, string> tags, string key)
  44. {
  45. if (tags == null)
  46. {
  47. return null;
  48. }
  49. string val;
  50. tags.TryGetValue(key, out val);
  51. return val;
  52. }
  53. /// <summary>
  54. /// Gets an int from an FFProbeResult tags dictionary
  55. /// </summary>
  56. /// <param name="tags">The tags.</param>
  57. /// <param name="key">The key.</param>
  58. /// <returns>System.Nullable{System.Int32}.</returns>
  59. public static int? GetDictionaryNumericValue(Dictionary<string, string> tags, string key)
  60. {
  61. var val = GetDictionaryValue(tags, key);
  62. if (!string.IsNullOrEmpty(val))
  63. {
  64. int i;
  65. if (int.TryParse(val, out i))
  66. {
  67. return i;
  68. }
  69. }
  70. return null;
  71. }
  72. /// <summary>
  73. /// Gets a DateTime from an FFProbeResult tags dictionary
  74. /// </summary>
  75. /// <param name="tags">The tags.</param>
  76. /// <param name="key">The key.</param>
  77. /// <returns>System.Nullable{DateTime}.</returns>
  78. public static DateTime? GetDictionaryDateTime(Dictionary<string, string> tags, string key)
  79. {
  80. var val = GetDictionaryValue(tags, key);
  81. if (!string.IsNullOrEmpty(val))
  82. {
  83. DateTime i;
  84. if (DateTime.TryParse(val, out i))
  85. {
  86. return i.ToUniversalTime();
  87. }
  88. }
  89. return null;
  90. }
  91. /// <summary>
  92. /// Converts a dictionary to case insensitive
  93. /// </summary>
  94. /// <param name="dict">The dict.</param>
  95. /// <returns>Dictionary{System.StringSystem.String}.</returns>
  96. private static Dictionary<string, string> ConvertDictionaryToCaseInSensitive(Dictionary<string, string> dict)
  97. {
  98. return new Dictionary<string, string>(dict, StringComparer.OrdinalIgnoreCase);
  99. }
  100. }
  101. }