FFMpegVideoImageProvider.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. public FfMpegVideoImageProvider(IIsoManager isoManager, ILogManager logManager, IServerConfigurationManager configurationManager, IMediaEncoder mediaEncoder)
  29. : base(logManager, configurationManager, mediaEncoder)
  30. {
  31. _isoManager = isoManager;
  32. }
  33. /// <summary>
  34. /// Gets the priority.
  35. /// </summary>
  36. /// <value>The priority.</value>
  37. public override MetadataProviderPriority Priority
  38. {
  39. get { return MetadataProviderPriority.Last; }
  40. }
  41. /// <summary>
  42. /// Supportses the specified item.
  43. /// </summary>
  44. /// <param name="item">The item.</param>
  45. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  46. public override bool Supports(BaseItem item)
  47. {
  48. if (item.LocationType != LocationType.FileSystem)
  49. {
  50. return false;
  51. }
  52. var video = item as Video;
  53. if (video != null)
  54. {
  55. if (video.VideoType == VideoType.Iso && _isoManager.CanMount(item.Path))
  56. {
  57. return true;
  58. }
  59. // We can only extract images from folder rips if we know the largest stream path
  60. return video.VideoType == VideoType.VideoFile || video.VideoType == VideoType.BluRay || video.VideoType == VideoType.Dvd;
  61. }
  62. return false;
  63. }
  64. /// <summary>
  65. /// The true task result
  66. /// </summary>
  67. protected static readonly Task<bool> TrueTaskResult = Task.FromResult(true);
  68. /// <summary>
  69. /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
  70. /// </summary>
  71. /// <param name="item">The item.</param>
  72. /// <param name="force">if set to <c>true</c> [force].</param>
  73. /// <param name="cancellationToken">The cancellation token.</param>
  74. /// <returns>Task{System.Boolean}.</returns>
  75. public override Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
  76. {
  77. if (force || string.IsNullOrEmpty(item.PrimaryImagePath))
  78. {
  79. var video = (Video)item;
  80. // We can only extract images from videos if we know there's an embedded video stream
  81. if (video.MediaStreams != null && video.MediaStreams.Any(m => m.Type == MediaStreamType.Video))
  82. {
  83. var filename = item.Id + "_" + item.DateModified.Ticks + "_primary";
  84. var path = Kernel.Instance.FFMpegManager.VideoImageCache.GetResourcePath(filename, ".jpg");
  85. if (!Kernel.Instance.FFMpegManager.VideoImageCache.ContainsFilePath(path))
  86. {
  87. return ExtractImage(video, path, cancellationToken);
  88. }
  89. // Image is already in the cache
  90. item.PrimaryImagePath = path;
  91. }
  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 &&
  125. video.RunTimeTicks.Value > 0
  126. ? TimeSpan.FromTicks(Convert.ToInt64(video.RunTimeTicks.Value * .1))
  127. : TimeSpan.FromSeconds(10);
  128. InputType type;
  129. var inputPath = MediaEncoderHelpers.GetInputArgument(video, isoMount, out type);
  130. await MediaEncoder.ExtractImage(inputPath, type, imageOffset, path, cancellationToken).ConfigureAwait(false);
  131. video.PrimaryImagePath = path;
  132. SetLastRefreshed(video, DateTime.UtcNow);
  133. }
  134. catch
  135. {
  136. SetLastRefreshed(video, DateTime.UtcNow, ProviderRefreshStatus.Failure);
  137. }
  138. finally
  139. {
  140. if (isoMount != null)
  141. {
  142. isoMount.Dispose();
  143. }
  144. }
  145. return true;
  146. }
  147. }
  148. }