FFMpegVideoImageProvider.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Common.MediaInfo;
  3. using MediaBrowser.Controller.Configuration;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Model.Entities;
  6. using MediaBrowser.Model.Logging;
  7. using System;
  8. using System.Linq;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. namespace MediaBrowser.Controller.Providers.MediaInfo
  12. {
  13. /// <summary>
  14. /// Uses ffmpeg to create video images
  15. /// </summary>
  16. public class FfMpegVideoImageProvider : BaseFFMpegProvider<Video>
  17. {
  18. /// <summary>
  19. /// The _iso manager
  20. /// </summary>
  21. private readonly IIsoManager _isoManager;
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="FfMpegVideoImageProvider" /> class.
  24. /// </summary>
  25. /// <param name="isoManager">The iso manager.</param>
  26. /// <param name="logManager">The log manager.</param>
  27. /// <param name="configurationManager">The configuration manager.</param>
  28. public FfMpegVideoImageProvider(IIsoManager isoManager, ILogManager logManager, IServerConfigurationManager configurationManager, IMediaEncoder mediaEncoder)
  29. : base(logManager, configurationManager, mediaEncoder)
  30. {
  31. _isoManager = isoManager;
  32. }
  33. /// <summary>
  34. /// Gets the priority.
  35. /// </summary>
  36. /// <value>The priority.</value>
  37. public override MetadataProviderPriority Priority
  38. {
  39. get { return MetadataProviderPriority.Last; }
  40. }
  41. /// <summary>
  42. /// Supportses the specified item.
  43. /// </summary>
  44. /// <param name="item">The item.</param>
  45. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  46. public override bool Supports(BaseItem item)
  47. {
  48. if (item.LocationType != LocationType.FileSystem)
  49. {
  50. return false;
  51. }
  52. var video = item as Video;
  53. if (video != null)
  54. {
  55. if (video.VideoType == VideoType.Iso && _isoManager.CanMount(item.Path))
  56. {
  57. return true;
  58. }
  59. // We can only extract images from folder rips if we know the largest stream path
  60. return video.VideoType == VideoType.VideoFile || video.VideoType == VideoType.BluRay || video.VideoType == VideoType.Dvd;
  61. }
  62. return false;
  63. }
  64. /// <summary>
  65. /// Needses the refresh internal.
  66. /// </summary>
  67. /// <param name="item">The item.</param>
  68. /// <param name="providerInfo">The provider info.</param>
  69. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  70. protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
  71. {
  72. if (!string.IsNullOrEmpty(item.PrimaryImagePath))
  73. {
  74. return false;
  75. }
  76. return base.NeedsRefreshInternal(item, providerInfo);
  77. }
  78. /// <summary>
  79. /// The true task result
  80. /// </summary>
  81. protected static readonly Task<bool> TrueTaskResult = Task.FromResult(true);
  82. /// <summary>
  83. /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
  84. /// </summary>
  85. /// <param name="item">The item.</param>
  86. /// <param name="force">if set to <c>true</c> [force].</param>
  87. /// <param name="cancellationToken">The cancellation token.</param>
  88. /// <returns>Task{System.Boolean}.</returns>
  89. public override Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
  90. {
  91. if (force || string.IsNullOrEmpty(item.PrimaryImagePath))
  92. {
  93. var video = (Video)item;
  94. // We can only extract images from videos if we know there's an embedded video stream
  95. if (video.MediaStreams != null && video.MediaStreams.Any(m => m.Type == MediaStreamType.Video))
  96. {
  97. var filename = item.Id + "_" + item.DateModified.Ticks + "_primary";
  98. var path = Kernel.Instance.FFMpegManager.VideoImageCache.GetResourcePath(filename, ".jpg");
  99. if (!Kernel.Instance.FFMpegManager.VideoImageCache.ContainsFilePath(path))
  100. {
  101. return ExtractImage(video, path, cancellationToken);
  102. }
  103. // Image is already in the cache
  104. item.PrimaryImagePath = path;
  105. }
  106. }
  107. SetLastRefreshed(item, DateTime.UtcNow);
  108. return TrueTaskResult;
  109. }
  110. /// <summary>
  111. /// Mounts the iso if needed.
  112. /// </summary>
  113. /// <param name="item">The item.</param>
  114. /// <param name="cancellationToken">The cancellation token.</param>
  115. /// <returns>IsoMount.</returns>
  116. protected Task<IIsoMount> MountIsoIfNeeded(Video item, CancellationToken cancellationToken)
  117. {
  118. if (item.VideoType == VideoType.Iso)
  119. {
  120. return _isoManager.Mount(item.Path, cancellationToken);
  121. }
  122. return NullMountTaskResult;
  123. }
  124. /// <summary>
  125. /// Extracts the image.
  126. /// </summary>
  127. /// <param name="video">The video.</param>
  128. /// <param name="path">The path.</param>
  129. /// <param name="cancellationToken">The cancellation token.</param>
  130. /// <returns>Task{System.Boolean}.</returns>
  131. private async Task<bool> ExtractImage(Video video, string path, CancellationToken cancellationToken)
  132. {
  133. var isoMount = await MountIsoIfNeeded(video, cancellationToken).ConfigureAwait(false);
  134. try
  135. {
  136. // If we know the duration, grab it from 10% into the video. Otherwise just 10 seconds in.
  137. // Always use 10 seconds for dvd because our duration could be out of whack
  138. var imageOffset = video.VideoType != VideoType.Dvd && video.RunTimeTicks.HasValue &&
  139. video.RunTimeTicks.Value > 0
  140. ? TimeSpan.FromTicks(Convert.ToInt64(video.RunTimeTicks.Value * .1))
  141. : TimeSpan.FromSeconds(10);
  142. InputType type;
  143. var inputPath = MediaEncoderHelpers.GetInputArgument(video, isoMount, out type);
  144. await MediaEncoder.ExtractImage(inputPath, type, imageOffset, path, cancellationToken).ConfigureAwait(false);
  145. video.PrimaryImagePath = path;
  146. SetLastRefreshed(video, DateTime.UtcNow);
  147. }
  148. catch
  149. {
  150. SetLastRefreshed(video, DateTime.UtcNow, ProviderRefreshStatus.Failure);
  151. }
  152. finally
  153. {
  154. if (isoMount != null)
  155. {
  156. isoMount.Dispose();
  157. }
  158. }
  159. return true;
  160. }
  161. }
  162. }