VideoInfoProvider.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 Fetch(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. string outputDirectory = Path.Combine(Kernel.Instance.ApplicationPaths.FFProbeVideoCacheDirectory, item.Id.ToString().Substring(0, 1));
  37. string outputPath = Path.Combine(outputDirectory, item.Id + "-" + item.DateModified.Ticks + ".js");
  38. FFProbeResult data = await FFProbe.Run(video, outputPath).ConfigureAwait(false);
  39. }
  40. private void Fetch(Video video, FFProbeResult data)
  41. {
  42. if (!string.IsNullOrEmpty(data.format.duration))
  43. {
  44. video.RunTimeTicks = TimeSpan.FromSeconds(double.Parse(data.format.duration)).Ticks;
  45. }
  46. if (!string.IsNullOrEmpty(data.format.bit_rate))
  47. {
  48. video.BitRate = int.Parse(data.format.bit_rate);
  49. }
  50. MediaStream videoStream = data.streams.FirstOrDefault(s => s.codec_type.Equals("video", StringComparison.OrdinalIgnoreCase));
  51. if (videoStream != null)
  52. {
  53. FetchFromVideoStream(video, videoStream);
  54. }
  55. }
  56. private void FetchFromVideoStream(Video video, MediaStream stream)
  57. {
  58. video.Codec = stream.codec_name;
  59. video.Width = stream.width;
  60. video.Height = stream.height;
  61. video.AspectRatio = stream.display_aspect_ratio;
  62. }
  63. /// <summary>
  64. /// Determines if there's already enough info in the Video object to allow us to skip running ffprobe
  65. /// </summary>
  66. private bool CanSkip(Video video)
  67. {
  68. if (video.AudioStreams == null || !video.AudioStreams.Any())
  69. {
  70. return false;
  71. }
  72. if (string.IsNullOrEmpty(video.AspectRatio))
  73. {
  74. return false;
  75. }
  76. if (string.IsNullOrEmpty(video.Codec))
  77. {
  78. return false;
  79. }
  80. if (string.IsNullOrEmpty(video.ScanType))
  81. {
  82. return false;
  83. }
  84. if (!video.RunTimeTicks.HasValue || video.RunTimeTicks.Value == 0)
  85. {
  86. return false;
  87. }
  88. if (video.FrameRate == 0 || video.Height == 0 || video.Width == 0 || video.BitRate == 0)
  89. {
  90. return false;
  91. }
  92. return true;
  93. }
  94. public override void Init()
  95. {
  96. base.Init();
  97. AudioInfoProvider.EnsureCacheSubFolders(Kernel.Instance.ApplicationPaths.FFProbeVideoCacheDirectory);
  98. }
  99. }
  100. }