2
0

FFMpegVideoImageProvider.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
  56. /// </summary>
  57. /// <param name="item">The item.</param>
  58. /// <param name="force">if set to <c>true</c> [force].</param>
  59. /// <param name="cancellationToken">The cancellation token.</param>
  60. /// <returns>Task{System.Boolean}.</returns>
  61. protected override Task<bool> FetchAsyncInternal(BaseItem item, bool force, CancellationToken cancellationToken)
  62. {
  63. if (string.IsNullOrEmpty(item.PrimaryImagePath))
  64. {
  65. var video = (Video)item;
  66. var filename = item.Id + "_" + item.DateModified.Ticks + "_primary";
  67. var path = Kernel.Instance.FFMpegManager.VideoImageCache.GetResourcePath(filename, ".jpg");
  68. if (!Kernel.Instance.FFMpegManager.VideoImageCache.ContainsFilePath(path))
  69. {
  70. return ExtractImage(video, path, cancellationToken);
  71. }
  72. // Image is already in the cache
  73. item.PrimaryImagePath = path;
  74. }
  75. SetLastRefreshed(item, DateTime.UtcNow);
  76. return TrueTaskResult;
  77. }
  78. /// <summary>
  79. /// Mounts the iso if needed.
  80. /// </summary>
  81. /// <param name="item">The item.</param>
  82. /// <param name="cancellationToken">The cancellation token.</param>
  83. /// <returns>IsoMount.</returns>
  84. protected Task<IIsoMount> MountIsoIfNeeded(Video item, CancellationToken cancellationToken)
  85. {
  86. if (item.VideoType == VideoType.Iso)
  87. {
  88. return _isoManager.Mount(item.Path, cancellationToken);
  89. }
  90. return NullMountTaskResult;
  91. }
  92. /// <summary>
  93. /// Extracts the image.
  94. /// </summary>
  95. /// <param name="video">The video.</param>
  96. /// <param name="path">The path.</param>
  97. /// <param name="cancellationToken">The cancellation token.</param>
  98. /// <returns>Task{System.Boolean}.</returns>
  99. private async Task<bool> ExtractImage(Video video, string path, CancellationToken cancellationToken)
  100. {
  101. var isoMount = await MountIsoIfNeeded(video, cancellationToken).ConfigureAwait(false);
  102. try
  103. {
  104. // If we know the duration, grab it from 10% into the video. Otherwise just 10 seconds in.
  105. // Always use 10 seconds for dvd because our duration could be out of whack
  106. var imageOffset = video.VideoType != VideoType.Dvd && video.RunTimeTicks.HasValue && video.RunTimeTicks.Value > 0
  107. ? TimeSpan.FromTicks(Convert.ToInt64(video.RunTimeTicks.Value * .1))
  108. : TimeSpan.FromSeconds(10);
  109. var inputPath = isoMount == null ?
  110. Kernel.Instance.FFMpegManager.GetInputArgument(video) :
  111. Kernel.Instance.FFMpegManager.GetInputArgument(video, isoMount);
  112. var success = await Kernel.Instance.FFMpegManager.ExtractImage(inputPath, imageOffset, path, cancellationToken).ConfigureAwait(false);
  113. if (success)
  114. {
  115. video.PrimaryImagePath = path;
  116. SetLastRefreshed(video, DateTime.UtcNow);
  117. }
  118. else
  119. {
  120. SetLastRefreshed(video, DateTime.UtcNow, ProviderRefreshStatus.Failure);
  121. }
  122. }
  123. finally
  124. {
  125. if (isoMount != null)
  126. {
  127. isoMount.Dispose();
  128. }
  129. }
  130. return true;
  131. }
  132. }
  133. }