FFMpegVideoImageProvider.cs 5.5 KB

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