FFMpegVideoImageProvider.cs 6.2 KB

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