VideoInfoProvider.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.ComponentModel.Composition;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using MediaBrowser.Controller.Events;
  7. using MediaBrowser.Controller.FFMpeg;
  8. using MediaBrowser.Model.Entities;
  9. namespace MediaBrowser.Controller.Providers
  10. {
  11. [Export(typeof(BaseMetadataProvider))]
  12. public class VideoInfoProvider : BaseMetadataProvider
  13. {
  14. public override bool Supports(BaseEntity item)
  15. {
  16. return item is Video;
  17. }
  18. public override MetadataProviderPriority Priority
  19. {
  20. // Give this second priority
  21. // Give metadata xml providers a chance to fill in data first, so that we can skip this whenever possible
  22. get { return MetadataProviderPriority.Second; }
  23. }
  24. public override async Task FetchAsync(BaseEntity item, ItemResolveEventArgs args)
  25. {
  26. Video video = item as Video;
  27. if (video.VideoType != VideoType.VideoFile)
  28. {
  29. // Not supported yet
  30. return;
  31. }
  32. if (CanSkip(video))
  33. {
  34. return;
  35. }
  36. Fetch(video, await FFProbe.Run(video, GetFFProbeOutputPath(video)).ConfigureAwait(false));
  37. }
  38. private string GetFFProbeOutputPath(Video item)
  39. {
  40. string outputDirectory = Path.Combine(Kernel.Instance.ApplicationPaths.FFProbeVideoCacheDirectory, item.Id.ToString().Substring(0, 1));
  41. return Path.Combine(outputDirectory, item.Id + "-" + item.DateModified.Ticks + ".js");
  42. }
  43. private void Fetch(Video video, FFProbeResult data)
  44. {
  45. if (!string.IsNullOrEmpty(data.format.duration))
  46. {
  47. video.RunTimeTicks = TimeSpan.FromSeconds(double.Parse(data.format.duration)).Ticks;
  48. }
  49. if (!string.IsNullOrEmpty(data.format.bit_rate))
  50. {
  51. video.BitRate = int.Parse(data.format.bit_rate);
  52. }
  53. MediaStream videoStream = data.streams.FirstOrDefault(s => s.codec_type.Equals("video", StringComparison.OrdinalIgnoreCase));
  54. if (videoStream != null)
  55. {
  56. FetchFromVideoStream(video, videoStream);
  57. }
  58. }
  59. private void FetchFromVideoStream(Video video, MediaStream stream)
  60. {
  61. video.Codec = stream.codec_name;
  62. video.Width = stream.width;
  63. video.Height = stream.height;
  64. video.AspectRatio = stream.display_aspect_ratio;
  65. }
  66. /// <summary>
  67. /// Determines if there's already enough info in the Video object to allow us to skip running ffprobe
  68. /// </summary>
  69. private bool CanSkip(Video video)
  70. {
  71. if (video.AudioStreams == null || !video.AudioStreams.Any())
  72. {
  73. return false;
  74. }
  75. if (string.IsNullOrEmpty(video.AspectRatio))
  76. {
  77. return false;
  78. }
  79. if (string.IsNullOrEmpty(video.Codec))
  80. {
  81. return false;
  82. }
  83. if (string.IsNullOrEmpty(video.ScanType))
  84. {
  85. return false;
  86. }
  87. if (!video.RunTimeTicks.HasValue || video.RunTimeTicks.Value == 0)
  88. {
  89. return false;
  90. }
  91. if (video.FrameRate == 0 || video.Height == 0 || video.Width == 0 || video.BitRate == 0)
  92. {
  93. return false;
  94. }
  95. return true;
  96. }
  97. public override void Init()
  98. {
  99. base.Init();
  100. AudioInfoProvider.EnsureCacheSubFolders(Kernel.Instance.ApplicationPaths.FFProbeVideoCacheDirectory);
  101. }
  102. }
  103. }