FFProbeHelpers.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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?.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 an int from an FFProbeResult tags dictionary.
  39. /// </summary>
  40. /// <param name="tags">The tags.</param>
  41. /// <param name="key">The key.</param>
  42. /// <returns>System.Nullable{System.Int32}.</returns>
  43. public static int? GetDictionaryNumericValue(IReadOnlyDictionary<string, string> tags, string key)
  44. {
  45. if (tags.TryGetValue(key, out var val) && int.TryParse(val, out var i))
  46. {
  47. return i;
  48. }
  49. return null;
  50. }
  51. /// <summary>
  52. /// Gets a DateTime 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{DateTime}.</returns>
  57. public static DateTime? GetDictionaryDateTime(IReadOnlyDictionary<string, string> tags, string key)
  58. {
  59. if (tags.TryGetValue(key, out var val)
  60. && (DateTime.TryParse(val, DateTimeFormatInfo.CurrentInfo, DateTimeStyles.AssumeUniversal, out var dateTime)
  61. || DateTime.TryParseExact(val, "yyyy", DateTimeFormatInfo.CurrentInfo, DateTimeStyles.AssumeUniversal, out dateTime)))
  62. {
  63. return dateTime.ToUniversalTime();
  64. }
  65. return null;
  66. }
  67. /// <summary>
  68. /// Converts a dictionary to case insensitive.
  69. /// </summary>
  70. /// <param name="dict">The dict.</param>
  71. /// <returns>Dictionary{System.StringSystem.String}.</returns>
  72. private static Dictionary<string, string> ConvertDictionaryToCaseInsensitive(IReadOnlyDictionary<string, string> dict)
  73. {
  74. return new Dictionary<string, string>(dict, StringComparer.OrdinalIgnoreCase);
  75. }
  76. }
  77. }