VideoImageProvider.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using MediaBrowser.Controller.Configuration;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Library;
  4. using MediaBrowser.Controller.MediaEncoding;
  5. using MediaBrowser.Controller.Providers;
  6. using MediaBrowser.Model.Drawing;
  7. using MediaBrowser.Model.Entities;
  8. using MediaBrowser.Model.IO;
  9. using MediaBrowser.Model.Logging;
  10. using MediaBrowser.Model.MediaInfo;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. namespace MediaBrowser.Providers.MediaInfo
  16. {
  17. public class VideoImageProvider : IDynamicImageProvider, IHasItemChangeMonitor, IHasOrder
  18. {
  19. private readonly IIsoManager _isoManager;
  20. private readonly IMediaEncoder _mediaEncoder;
  21. private readonly IServerConfigurationManager _config;
  22. private readonly ILibraryManager _libraryManager;
  23. private readonly ILogger _logger;
  24. public VideoImageProvider(IIsoManager isoManager, IMediaEncoder mediaEncoder, IServerConfigurationManager config, ILibraryManager libraryManager, ILogger logger)
  25. {
  26. _isoManager = isoManager;
  27. _mediaEncoder = mediaEncoder;
  28. _config = config;
  29. _libraryManager = libraryManager;
  30. _logger = logger;
  31. }
  32. /// <summary>
  33. /// The null mount task result
  34. /// </summary>
  35. protected readonly Task<IIsoMount> NullMountTaskResult = Task.FromResult<IIsoMount>(null);
  36. /// <summary>
  37. /// Mounts the iso if needed.
  38. /// </summary>
  39. /// <param name="item">The item.</param>
  40. /// <param name="cancellationToken">The cancellation token.</param>
  41. /// <returns>Task{IIsoMount}.</returns>
  42. protected Task<IIsoMount> MountIsoIfNeeded(Video item, CancellationToken cancellationToken)
  43. {
  44. if (item.VideoType == VideoType.Iso)
  45. {
  46. return _isoManager.Mount(item.Path, cancellationToken);
  47. }
  48. return NullMountTaskResult;
  49. }
  50. public IEnumerable<ImageType> GetSupportedImages(IHasImages item)
  51. {
  52. return new List<ImageType> { ImageType.Primary };
  53. }
  54. public Task<DynamicImageResponse> GetImage(IHasImages item, ImageType type, CancellationToken cancellationToken)
  55. {
  56. var video = (Video)item;
  57. // No support for this
  58. if (video.VideoType == VideoType.HdDvd || video.IsPlaceHolder)
  59. {
  60. return Task.FromResult(new DynamicImageResponse { HasImage = false });
  61. }
  62. // Can't extract from iso's if we weren't unable to determine iso type
  63. if (video.VideoType == VideoType.Iso && !video.IsoType.HasValue)
  64. {
  65. return Task.FromResult(new DynamicImageResponse { HasImage = false });
  66. }
  67. // Can't extract if we didn't find a video stream in the file
  68. if (!video.DefaultVideoStreamIndex.HasValue)
  69. {
  70. _logger.Debug("Skipping image extraction due to missing DefaultVideoStreamIndex for {0}.", video.Path ?? string.Empty);
  71. return Task.FromResult(new DynamicImageResponse { HasImage = false });
  72. }
  73. return GetVideoImage(video, cancellationToken);
  74. }
  75. public async Task<DynamicImageResponse> GetVideoImage(Video item, CancellationToken cancellationToken)
  76. {
  77. var isoMount = await MountIsoIfNeeded(item, cancellationToken).ConfigureAwait(false);
  78. try
  79. {
  80. // If we know the duration, grab it from 10% into the video. Otherwise just 10 seconds in.
  81. // Always use 10 seconds for dvd because our duration could be out of whack
  82. var imageOffset = item.VideoType != VideoType.Dvd && item.RunTimeTicks.HasValue &&
  83. item.RunTimeTicks.Value > 0
  84. ? TimeSpan.FromTicks(Convert.ToInt64(item.RunTimeTicks.Value * .1))
  85. : TimeSpan.FromSeconds(10);
  86. var protocol = item.LocationType == LocationType.Remote
  87. ? MediaProtocol.Http
  88. : MediaProtocol.File;
  89. var inputPath = MediaEncoderHelpers.GetInputArgument(item.Path, protocol, isoMount, item.PlayableStreamFileNames);
  90. var stream = await _mediaEncoder.ExtractVideoImage(inputPath, protocol, item.Video3DFormat, imageOffset, cancellationToken).ConfigureAwait(false);
  91. return new DynamicImageResponse
  92. {
  93. Format = ImageFormat.Jpg,
  94. HasImage = true,
  95. Stream = stream
  96. };
  97. }
  98. finally
  99. {
  100. if (isoMount != null)
  101. {
  102. isoMount.Dispose();
  103. }
  104. }
  105. }
  106. public string Name
  107. {
  108. get { return "Screen Grabber"; }
  109. }
  110. public bool Supports(IHasImages item)
  111. {
  112. var video = item as Video;
  113. if (item.LocationType == LocationType.FileSystem && video != null && !video.IsPlaceHolder &&
  114. !video.IsShortcut && !video.IsArchive)
  115. {
  116. return true;
  117. }
  118. return false;
  119. }
  120. public int Order
  121. {
  122. get
  123. {
  124. // Make sure this comes after internet image providers
  125. return 100;
  126. }
  127. }
  128. public bool HasChanged(IHasMetadata item, MetadataStatus status, IDirectoryService directoryService)
  129. {
  130. if (status.ItemDateModified.HasValue)
  131. {
  132. if (status.ItemDateModified.Value != item.DateModified)
  133. {
  134. return true;
  135. }
  136. }
  137. return false;
  138. }
  139. }
  140. }