FFProbeHelpers.cs 3.5 KB

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