VideoInfoProvider.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.Composition;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using MediaBrowser.Common.Logging;
  7. using MediaBrowser.Controller.FFMpeg;
  8. using MediaBrowser.Controller.Library;
  9. using MediaBrowser.Model.Entities;
  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. await Task.Run(() =>
  28. {
  29. Video video = item as Video;
  30. if (video.VideoType != VideoType.VideoFile)
  31. {
  32. // Not supported yet
  33. return;
  34. }
  35. if (CanSkip(video))
  36. {
  37. return;
  38. }
  39. Fetch(video, FFProbe.Run(video));
  40. });
  41. }
  42. private void Fetch(Video video, FFProbeResult data)
  43. {
  44. if (data == null)
  45. {
  46. Logger.LogInfo("Null FFProbeResult for {0} {1}", video.Id, video.Name);
  47. return;
  48. }
  49. if (!string.IsNullOrEmpty(data.format.duration))
  50. {
  51. video.RunTimeTicks = TimeSpan.FromSeconds(double.Parse(data.format.duration)).Ticks;
  52. }
  53. if (!string.IsNullOrEmpty(data.format.bit_rate))
  54. {
  55. video.BitRate = int.Parse(data.format.bit_rate);
  56. }
  57. // For now, only read info about first video stream
  58. // Files with multiple video streams are possible, but extremely rare
  59. bool foundVideo = false;
  60. foreach (MediaStream stream in data.streams)
  61. {
  62. if (stream.codec_type.Equals("video", StringComparison.OrdinalIgnoreCase))
  63. {
  64. if (!foundVideo)
  65. {
  66. FetchFromVideoStream(video, stream);
  67. }
  68. foundVideo = true;
  69. }
  70. else if (stream.codec_type.Equals("audio", StringComparison.OrdinalIgnoreCase))
  71. {
  72. FetchFromAudioStream(video, stream);
  73. }
  74. }
  75. }
  76. private void FetchFromVideoStream(Video video, MediaStream stream)
  77. {
  78. video.Codec = stream.codec_name;
  79. video.Width = stream.width;
  80. video.Height = stream.height;
  81. video.AspectRatio = stream.display_aspect_ratio;
  82. if (!string.IsNullOrEmpty(stream.avg_frame_rate))
  83. {
  84. string[] parts = stream.avg_frame_rate.Split('/');
  85. if (parts.Length == 2)
  86. {
  87. video.FrameRate = float.Parse(parts[0]) / float.Parse(parts[1]);
  88. }
  89. else
  90. {
  91. video.FrameRate = float.Parse(parts[0]);
  92. }
  93. }
  94. }
  95. private void FetchFromAudioStream(Video video, MediaStream stream)
  96. {
  97. AudioStream audio = new AudioStream();
  98. audio.Codec = stream.codec_name;
  99. if (!string.IsNullOrEmpty(stream.bit_rate))
  100. {
  101. audio.BitRate = int.Parse(stream.bit_rate);
  102. }
  103. audio.Channels = stream.channels;
  104. if (!string.IsNullOrEmpty(stream.sample_rate))
  105. {
  106. audio.SampleRate = int.Parse(stream.sample_rate);
  107. }
  108. audio.Language = AudioInfoProvider.GetDictionaryValue(stream.tags, "language");
  109. List<AudioStream> streams = video.AudioStreams ?? new List<AudioStream>();
  110. streams.Add(audio);
  111. video.AudioStreams = streams;
  112. }
  113. /// <summary>
  114. /// Determines if there's already enough info in the Video object to allow us to skip running ffprobe
  115. /// </summary>
  116. private bool CanSkip(Video video)
  117. {
  118. if (video.AudioStreams == null || !video.AudioStreams.Any())
  119. {
  120. return false;
  121. }
  122. if (string.IsNullOrEmpty(video.AspectRatio))
  123. {
  124. return false;
  125. }
  126. if (string.IsNullOrEmpty(video.Codec))
  127. {
  128. return false;
  129. }
  130. if (string.IsNullOrEmpty(video.ScanType))
  131. {
  132. return false;
  133. }
  134. if (!video.RunTimeTicks.HasValue || video.RunTimeTicks.Value == 0)
  135. {
  136. return false;
  137. }
  138. if (video.FrameRate == 0 || video.Height == 0 || video.Width == 0 || video.BitRate == 0)
  139. {
  140. return false;
  141. }
  142. return true;
  143. }
  144. public override void Init()
  145. {
  146. base.Init();
  147. AudioInfoProvider.EnsureCacheSubFolders(Kernel.Instance.ApplicationPaths.FFProbeVideoCacheDirectory);
  148. // This is an optimzation. Do this now so that it doesn't have to be done upon first serialization.
  149. ProtoBuf.Meta.RuntimeTypeModel.Default.Add(typeof(FFProbeResult), true);
  150. ProtoBuf.Meta.RuntimeTypeModel.Default.Add(typeof(MediaStream), true);
  151. ProtoBuf.Meta.RuntimeTypeModel.Default.Add(typeof(MediaFormat), true);
  152. }
  153. }
  154. }