2
0

FFProbeHelpers.cs 3.6 KB

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