FFProbeHelpers.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #nullable disable
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. namespace MediaBrowser.MediaEncoding.Probing
  6. {
  7. /// <summary>
  8. /// Class containing helper methods for working with FFprobe output.
  9. /// </summary>
  10. public static class FFProbeHelpers
  11. {
  12. /// <summary>
  13. /// Normalizes the FF probe result.
  14. /// </summary>
  15. /// <param name="result">The result.</param>
  16. public static void NormalizeFFProbeResult(InternalMediaInfoResult result)
  17. {
  18. if (result == null)
  19. {
  20. throw new ArgumentNullException(nameof(result));
  21. }
  22. if (result.Format != null && result.Format.Tags != null)
  23. {
  24. result.Format.Tags = ConvertDictionaryToCaseInsensitive(result.Format.Tags);
  25. }
  26. if (result.Streams != null)
  27. {
  28. // Convert all dictionaries to case insensitive
  29. foreach (var stream in result.Streams)
  30. {
  31. if (stream.Tags != null)
  32. {
  33. stream.Tags = ConvertDictionaryToCaseInsensitive(stream.Tags);
  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(IReadOnlyDictionary<string, string> tags, string key)
  45. {
  46. if (tags == null)
  47. {
  48. return null;
  49. }
  50. tags.TryGetValue(key, out var 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. if (int.TryParse(val, out var i))
  65. {
  66. return i;
  67. }
  68. }
  69. return null;
  70. }
  71. /// <summary>
  72. /// Gets a DateTime from an FFProbeResult tags dictionary.
  73. /// </summary>
  74. /// <param name="tags">The tags.</param>
  75. /// <param name="key">The key.</param>
  76. /// <returns>System.Nullable{DateTime}.</returns>
  77. public static DateTime? GetDictionaryDateTime(Dictionary<string, string> tags, string key)
  78. {
  79. var val = GetDictionaryValue(tags, key);
  80. if (string.IsNullOrEmpty(val))
  81. {
  82. return null;
  83. }
  84. if (DateTime.TryParse(val, DateTimeFormatInfo.CurrentInfo, DateTimeStyles.AssumeUniversal, out var i))
  85. {
  86. return i.ToUniversalTime();
  87. }
  88. return null;
  89. }
  90. /// <summary>
  91. /// Converts a dictionary to case insensitive.
  92. /// </summary>
  93. /// <param name="dict">The dict.</param>
  94. /// <returns>Dictionary{System.StringSystem.String}.</returns>
  95. private static Dictionary<string, string> ConvertDictionaryToCaseInsensitive(IReadOnlyDictionary<string, string> dict)
  96. {
  97. return new Dictionary<string, string>(dict, StringComparer.OrdinalIgnoreCase);
  98. }
  99. }
  100. }