FFMpegVideoImageProvider.cs 6.5 KB

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