FFMpegVideoImageProvider.cs 6.0 KB

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