FFMpegVideoImageProvider.cs 5.6 KB

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