VideoImageProvider.cs 6.0 KB

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