VideoInfoProvider.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 : BaseMediaInfoProvider<Video>
  14. {
  15. public override MetadataProviderPriority Priority
  16. {
  17. // Give this second priority
  18. // Give metadata xml providers a chance to fill in data first, so that we can skip this whenever possible
  19. get { return MetadataProviderPriority.Second; }
  20. }
  21. protected override string CacheDirectory
  22. {
  23. get { return Kernel.Instance.ApplicationPaths.FFProbeVideoCacheDirectory; }
  24. }
  25. protected override void Fetch(Video video, FFProbeResult data)
  26. {
  27. if (data.format != null)
  28. {
  29. if (!string.IsNullOrEmpty(data.format.duration))
  30. {
  31. video.RunTimeTicks = TimeSpan.FromSeconds(double.Parse(data.format.duration)).Ticks;
  32. }
  33. if (!string.IsNullOrEmpty(data.format.bit_rate))
  34. {
  35. video.BitRate = int.Parse(data.format.bit_rate);
  36. }
  37. }
  38. if (data.streams != null)
  39. {
  40. // For now, only read info about first video stream
  41. // Files with multiple video streams are possible, but extremely rare
  42. bool foundVideo = false;
  43. foreach (MediaStream stream in data.streams)
  44. {
  45. if (stream.codec_type.Equals("video", StringComparison.OrdinalIgnoreCase))
  46. {
  47. if (!foundVideo)
  48. {
  49. FetchFromVideoStream(video, stream);
  50. }
  51. foundVideo = true;
  52. }
  53. else if (stream.codec_type.Equals("audio", StringComparison.OrdinalIgnoreCase))
  54. {
  55. FetchFromAudioStream(video, stream);
  56. }
  57. }
  58. }
  59. }
  60. private void FetchFromVideoStream(Video video, MediaStream stream)
  61. {
  62. video.Codec = stream.codec_name;
  63. video.Width = stream.width;
  64. video.Height = stream.height;
  65. video.AspectRatio = stream.display_aspect_ratio;
  66. if (!string.IsNullOrEmpty(stream.avg_frame_rate))
  67. {
  68. string[] parts = stream.avg_frame_rate.Split('/');
  69. if (parts.Length == 2)
  70. {
  71. video.FrameRate = float.Parse(parts[0]) / float.Parse(parts[1]);
  72. }
  73. else
  74. {
  75. video.FrameRate = float.Parse(parts[0]);
  76. }
  77. }
  78. }
  79. private void FetchFromAudioStream(Video video, MediaStream stream)
  80. {
  81. AudioStream audio = new AudioStream();
  82. audio.Codec = stream.codec_name;
  83. if (!string.IsNullOrEmpty(stream.bit_rate))
  84. {
  85. audio.BitRate = int.Parse(stream.bit_rate);
  86. }
  87. audio.Channels = stream.channels;
  88. if (!string.IsNullOrEmpty(stream.sample_rate))
  89. {
  90. audio.SampleRate = int.Parse(stream.sample_rate);
  91. }
  92. audio.Language = GetDictionaryValue(stream.tags, "language");
  93. List<AudioStream> streams = video.AudioStreams ?? new List<AudioStream>();
  94. streams.Add(audio);
  95. video.AudioStreams = streams;
  96. }
  97. private void FetchFromSubtitleStream(Video video, MediaStream stream)
  98. {
  99. SubtitleStream subtitle = new SubtitleStream();
  100. subtitle.Language = GetDictionaryValue(stream.tags, "language");
  101. List<SubtitleStream> streams = video.Subtitles ?? new List<SubtitleStream>();
  102. streams.Add(subtitle);
  103. video.Subtitles = streams;
  104. }
  105. /// <summary>
  106. /// Determines if there's already enough info in the Video object to allow us to skip running ffprobe
  107. /// </summary>
  108. protected override bool CanSkipFFProbe(Video video)
  109. {
  110. if (video.VideoType != VideoType.VideoFile)
  111. {
  112. // Not supported yet
  113. return true;
  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. // This is an optimzation. Do this now so that it doesn't have to be done upon first serialization.
  145. ProtoBuf.Meta.RuntimeTypeModel.Default.Add(typeof(FFProbeResult), true);
  146. ProtoBuf.Meta.RuntimeTypeModel.Default.Add(typeof(MediaStream), true);
  147. ProtoBuf.Meta.RuntimeTypeModel.Default.Add(typeof(MediaFormat), true);
  148. }
  149. }
  150. }