FFMpegVideoImageProvider.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. if (video.VideoType == VideoType.Iso && video.IsoType.HasValue && _isoManager.CanMount(item.Path))
  55. {
  56. return true;
  57. }
  58. // We can only extract images from folder rips if we know the largest stream path
  59. return video.VideoType == VideoType.VideoFile || video.VideoType == VideoType.BluRay || video.VideoType == VideoType.Dvd;
  60. }
  61. return false;
  62. }
  63. /// <summary>
  64. /// The true task result
  65. /// </summary>
  66. protected static readonly Task<bool> TrueTaskResult = Task.FromResult(true);
  67. /// <summary>
  68. /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
  69. /// </summary>
  70. /// <param name="item">The item.</param>
  71. /// <param name="force">if set to <c>true</c> [force].</param>
  72. /// <param name="cancellationToken">The cancellation token.</param>
  73. /// <returns>Task{System.Boolean}.</returns>
  74. public override Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
  75. {
  76. if (force || string.IsNullOrEmpty(item.PrimaryImagePath))
  77. {
  78. var video = (Video)item;
  79. // We can only extract images from videos if we know there's an embedded video stream
  80. if (video.MediaStreams != null && video.MediaStreams.Any(m => m.Type == MediaStreamType.Video))
  81. {
  82. var filename = item.Id + "_" + item.DateModified.Ticks + "_primary";
  83. var path = Kernel.Instance.FFMpegManager.VideoImageCache.GetResourcePath(filename, ".jpg");
  84. if (!Kernel.Instance.FFMpegManager.VideoImageCache.ContainsFilePath(path))
  85. {
  86. return ExtractImage(video, path, cancellationToken);
  87. }
  88. // Image is already in the cache
  89. item.PrimaryImagePath = path;
  90. }
  91. }
  92. SetLastRefreshed(item, DateTime.UtcNow);
  93. return TrueTaskResult;
  94. }
  95. /// <summary>
  96. /// Mounts the iso if needed.
  97. /// </summary>
  98. /// <param name="item">The item.</param>
  99. /// <param name="cancellationToken">The cancellation token.</param>
  100. /// <returns>IsoMount.</returns>
  101. protected Task<IIsoMount> MountIsoIfNeeded(Video item, CancellationToken cancellationToken)
  102. {
  103. if (item.VideoType == VideoType.Iso)
  104. {
  105. return _isoManager.Mount(item.Path, cancellationToken);
  106. }
  107. return NullMountTaskResult;
  108. }
  109. /// <summary>
  110. /// Extracts the image.
  111. /// </summary>
  112. /// <param name="video">The video.</param>
  113. /// <param name="path">The path.</param>
  114. /// <param name="cancellationToken">The cancellation token.</param>
  115. /// <returns>Task{System.Boolean}.</returns>
  116. private async Task<bool> ExtractImage(Video video, string path, CancellationToken cancellationToken)
  117. {
  118. var isoMount = await MountIsoIfNeeded(video, cancellationToken).ConfigureAwait(false);
  119. try
  120. {
  121. // If we know the duration, grab it from 10% into the video. Otherwise just 10 seconds in.
  122. // Always use 10 seconds for dvd because our duration could be out of whack
  123. var imageOffset = video.VideoType != VideoType.Dvd && video.RunTimeTicks.HasValue && video.RunTimeTicks.Value > 0
  124. ? TimeSpan.FromTicks(Convert.ToInt64(video.RunTimeTicks.Value * .1))
  125. : TimeSpan.FromSeconds(10);
  126. var inputPath = isoMount == null ?
  127. Kernel.Instance.FFMpegManager.GetInputArgument(video) :
  128. Kernel.Instance.FFMpegManager.GetInputArgument(video, isoMount);
  129. var success = await Kernel.Instance.FFMpegManager.ExtractImage(inputPath, imageOffset, path, cancellationToken).ConfigureAwait(false);
  130. if (success)
  131. {
  132. video.PrimaryImagePath = path;
  133. SetLastRefreshed(video, DateTime.UtcNow);
  134. }
  135. else
  136. {
  137. SetLastRefreshed(video, DateTime.UtcNow, ProviderRefreshStatus.Failure);
  138. }
  139. }
  140. finally
  141. {
  142. if (isoMount != null)
  143. {
  144. isoMount.Dispose();
  145. }
  146. }
  147. return true;
  148. }
  149. }
  150. }