VideoImageProvider.cs 5.6 KB

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