VideoInfoProvider.cs 5.4 KB

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