VideoInfoProvider.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. using System.Collections.Generic;
  10. namespace MediaBrowser.Controller.Providers
  11. {
  12. [Export(typeof(BaseMetadataProvider))]
  13. public class VideoInfoProvider : BaseMetadataProvider
  14. {
  15. public override bool Supports(BaseEntity item)
  16. {
  17. return item is Video;
  18. }
  19. public override MetadataProviderPriority Priority
  20. {
  21. // Give this second priority
  22. // Give metadata xml providers a chance to fill in data first, so that we can skip this whenever possible
  23. get { return MetadataProviderPriority.Second; }
  24. }
  25. public override async Task FetchAsync(BaseEntity item, ItemResolveEventArgs args)
  26. {
  27. Video video = item as Video;
  28. if (video.VideoType != VideoType.VideoFile)
  29. {
  30. // Not supported yet
  31. return;
  32. }
  33. if (CanSkip(video))
  34. {
  35. return;
  36. }
  37. Fetch(video, await FFProbe.Run(video, GetFFProbeOutputPath(video)).ConfigureAwait(false));
  38. }
  39. private string GetFFProbeOutputPath(Video item)
  40. {
  41. string outputDirectory = Path.Combine(Kernel.Instance.ApplicationPaths.FFProbeVideoCacheDirectory, item.Id.ToString().Substring(0, 1));
  42. return Path.Combine(outputDirectory, item.Id + "-" + item.DateModified.Ticks + ".js");
  43. }
  44. private void Fetch(Video video, FFProbeResult data)
  45. {
  46. if (!string.IsNullOrEmpty(data.format.duration))
  47. {
  48. video.RunTimeTicks = TimeSpan.FromSeconds(double.Parse(data.format.duration)).Ticks;
  49. }
  50. if (!string.IsNullOrEmpty(data.format.bit_rate))
  51. {
  52. video.BitRate = int.Parse(data.format.bit_rate);
  53. }
  54. // For now, only read info about first video stream
  55. // Files with multiple video streams are possible, but extremely rare
  56. bool foundVideo = false;
  57. foreach (MediaStream stream in data.streams)
  58. {
  59. if (stream.codec_type.Equals("video", StringComparison.OrdinalIgnoreCase))
  60. {
  61. if (!foundVideo)
  62. {
  63. FetchFromVideoStream(video, stream);
  64. }
  65. foundVideo = true;
  66. }
  67. else if (stream.codec_type.Equals("audio", StringComparison.OrdinalIgnoreCase))
  68. {
  69. FetchFromAudioStream(video, stream);
  70. }
  71. }
  72. }
  73. private void FetchFromVideoStream(Video video, MediaStream stream)
  74. {
  75. video.Codec = stream.codec_name;
  76. video.Width = stream.width;
  77. video.Height = stream.height;
  78. video.AspectRatio = stream.display_aspect_ratio;
  79. if (!string.IsNullOrEmpty(stream.avg_frame_rate))
  80. {
  81. string[] parts = stream.avg_frame_rate.Split('/');
  82. if (parts.Length == 2)
  83. {
  84. video.FrameRate = float.Parse(parts[0]) / float.Parse(parts[1]);
  85. }
  86. else
  87. {
  88. video.FrameRate = float.Parse(parts[0]);
  89. }
  90. }
  91. }
  92. private void FetchFromAudioStream(Video video, MediaStream stream)
  93. {
  94. AudioStream audio = new AudioStream();
  95. audio.Codec = stream.codec_name;
  96. if (!string.IsNullOrEmpty(stream.bit_rate))
  97. {
  98. audio.BitRate = int.Parse(stream.bit_rate);
  99. }
  100. audio.Channels = stream.channels;
  101. if (!string.IsNullOrEmpty(stream.sample_rate))
  102. {
  103. audio.SampleRate = int.Parse(stream.sample_rate);
  104. }
  105. audio.Language = AudioInfoProvider.GetDictionaryValue(stream.tags, "language");
  106. List<AudioStream> streams = (video.AudioStreams ?? new AudioStream[] { }).ToList();
  107. streams.Add(audio);
  108. video.AudioStreams = streams;
  109. }
  110. /// <summary>
  111. /// Determines if there's already enough info in the Video object to allow us to skip running ffprobe
  112. /// </summary>
  113. private bool CanSkip(Video video)
  114. {
  115. if (video.AudioStreams == null || !video.AudioStreams.Any())
  116. {
  117. return false;
  118. }
  119. if (string.IsNullOrEmpty(video.AspectRatio))
  120. {
  121. return false;
  122. }
  123. if (string.IsNullOrEmpty(video.Codec))
  124. {
  125. return false;
  126. }
  127. if (string.IsNullOrEmpty(video.ScanType))
  128. {
  129. return false;
  130. }
  131. if (!video.RunTimeTicks.HasValue || video.RunTimeTicks.Value == 0)
  132. {
  133. return false;
  134. }
  135. if (video.FrameRate == 0 || video.Height == 0 || video.Width == 0 || video.BitRate == 0)
  136. {
  137. return false;
  138. }
  139. return true;
  140. }
  141. public override void Init()
  142. {
  143. base.Init();
  144. AudioInfoProvider.EnsureCacheSubFolders(Kernel.Instance.ApplicationPaths.FFProbeVideoCacheDirectory);
  145. }
  146. }
  147. }