MediaInfoLib.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. namespace MediaBrowser.MediaInfo
  6. {
  7. public class MediaInfoLib
  8. {
  9. public MediaInfoResult GetVideoInfo(string path)
  10. {
  11. var lib = new MediaInfo();
  12. lib.Open(path);
  13. var result = new MediaInfoResult();
  14. // TODO: Don't hardcode
  15. var videoStreamIndex = 0;
  16. var text = GetValue(lib, videoStreamIndex, new[] { "ScanType", "Scan type", "ScanType/String" });
  17. if (!string.IsNullOrWhiteSpace(text))
  18. {
  19. result.IsInterlaced = text.IndexOf("interlac", StringComparison.OrdinalIgnoreCase) != -1;
  20. }
  21. text = GetValue(lib, videoStreamIndex, new[] { "Format_Settings_CABAC", "Format_Settings_CABAC/String" });
  22. if (!string.IsNullOrWhiteSpace(text))
  23. {
  24. result.IsCabac = string.Equals(text, "yes", StringComparison.OrdinalIgnoreCase);
  25. }
  26. int bitDepth;
  27. text = GetValue(lib, videoStreamIndex, new[] { "BitDepth", "BitDepth/String" });
  28. if (!string.IsNullOrWhiteSpace(text) && int.TryParse(text.Split(' ').First(), NumberStyles.Any, CultureInfo.InvariantCulture, out bitDepth))
  29. {
  30. result.BitDepth = bitDepth;
  31. }
  32. int refFrames;
  33. text = GetValue(lib, videoStreamIndex, new[] { "Format_Settings_RefFrames", "Format_Settings_RefFrames/String" });
  34. if (!string.IsNullOrWhiteSpace(text) && int.TryParse(text.Split(' ').First(), NumberStyles.Any, CultureInfo.InvariantCulture, out refFrames))
  35. {
  36. result.RefFrames = refFrames;
  37. }
  38. return result;
  39. }
  40. private string GetValue(MediaInfo lib, int index, IEnumerable<string> names)
  41. {
  42. return names.Select(i => lib.Get(StreamKind.Video, index, i)).FirstOrDefault(i => !string.IsNullOrWhiteSpace(i));
  43. }
  44. }
  45. public class MediaInfoResult
  46. {
  47. public bool? IsCabac { get; set; }
  48. public bool? IsInterlaced { get; set; }
  49. public int? BitDepth { get; set; }
  50. public int? RefFrames { get; set; }
  51. }
  52. }