VideoInfoProvider.cs 6.1 KB

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