FFMpegVideoImageProvider.cs 5.6 KB

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