VideoInfoProvider.cs 5.9 KB

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