2
0

FFMpegVideoImageProvider.cs 7.0 KB

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